Custom view (inheriting view)

Source: Internet
Author: User
Preface

Android provides a wide range of controls, but sometimes it cannot meet your needs. At this time, you need to customize the view. There are several types of custom views, one of which is inherited as the view, one is inherited from viewgroup. To inherit from the view, we need to draw the control by ourselves and inherit from the viewgroup which can organize the existing control. Next we will first introduce the situation that inherits from the view.

The following is a simple circular graph to introduce the entire painting process, as shown below:

  

Overview

To draw a widget, You need to draw two parts: one is the size and the other is the content, which are drawn through two methods: one is onmeasure and the other is ondraw. The overall structure is as follows:

  

1 public class customview extends view {2 3 Public customview (context, attributeset attrs) {4 super (context, attrs ); 5} 6 // draw content 7 @ override 8 protected void ondraw (canvas) {9 super. ondraw (canvas); 10} 11 // calculate the size of 12 @ override13 protected void onmeasure (INT widthmeasurespec, int heightmeasurespec) {14 super. onmeasure (widthmeasurespec, heightmeasurespec); 15} 1617}

 

Onmeasure

This function is used to calculate the size and size of the control. The concept of size is the drawing mode.

Measurespec. at_most: specifies the maximum size of the control by setting it to wrap_content.

Measurespec. Exactly: the parent element specifies the precise size of the control. For example, it is set to 100dip or match_parent.

Measurespec. Unspecified: the parent element does not control the control size, and its size is completely controlled internally.

In this mode, you can use int mode = measurespec. getmode (widthmeasurespec); to obtain the mode.

After calculating the size, use setmeasureddimension (width, height); To set the width and height.

Ondraw

This function is used to draw the control content. This function is passed into a canvas object, which is the current canvas object. You just need to draw the content to this canvas, you can use drawcircle, drawline, drawtext, and other methods in the canvas to draw content. If you need to set the stroke content, you can use the paint object to set the stroke content.

Source code

Java

  

1 public class customview extends view {2 3 int Height = 0, width = 0; 4 private int CX; // center x 5 private int Cy; // y 6 private int padding = 5; // control margin 7 8 Public customview (context, attributeset attrs) {9 super (context, attrs ); 10} 11 @ override12 protected void ondraw (canvas) {13 super. ondraw (canvas); 14 // calculates the center position and radius of 15 Cx = width/2; 16 Cy = height/2; 17 int radius = Cx> Cy? Cy: Cx; 18 19 paint = new paint (); 20 paint. setcolor (color. red); 21 canvas. drawcircle (CX, Cy, radius, paint); 22 canvas. drawcolor (color. gray); 23} 24 // calculate the size of 25 @ override26 protected void onmeasure (INT widthmeasurespec, int heightmeasurespec) {27 super. onmeasure (widthmeasurespec, heightmeasurespec); 28 29 widthmeasurespec = measurespec. getsize (widthmeasurespec); 30 heightmeasurespec = measurespec. getsize (heightmeasurespec); 31 // calculates the width of 32 int mode = measurespec. getmode (widthmeasurespec); 33 If (mode = measurespec. exactly) {34 width = widthmeasurespec + padding; 35} else {36 width = widthmeasurespec; 37} 38 39 // calculated height 40 mode = measurespec. getmode (heightmeasurespec); 41 if (mode = measurespec. exactly) {42 Height = heightmeasurespec + padding; 43} else {44 Height = heightmeasurespec; 45} 46 setmeasureddimension (width, height); 47} 4849}

Layout File

  

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:paddingBottom="@dimen/activity_vertical_margin" 6     android:paddingLeft="@dimen/activity_horizontal_margin" 7     android:paddingRight="@dimen/activity_horizontal_margin" 8     android:paddingTop="@dimen/activity_vertical_margin" 9     tools:context=".MainActivity"10     11     >1213     <com.example.customview.CustomView14         android:id="@+id/customView1"15         android:layout_width="50dip"16         android:layout_height="50dip"17         android:layout_alignParentLeft="true"        18         android:layout_alignParentTop="true"19         />2021 </RelativeLayout>

 

Postscript

This article mainly describes the overall process of plotting. The implementation functions are relatively simple, but the principle of complicated content is the same. Through this process, you can use your imagination to draw controls, the following article describes how to draw by using a combination of controls.

Address: http://www.cnblogs.com/luoaz/p/3980651.html

  

 

Custom view (inheriting view)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.