Android custom View

Source: Internet
Author: User

If you want to know about android custom views and design and implement the views you want, we will analyze the implementation mechanism of android custom controls from the basic knowledge.

First, let's take a look at the basic knowledge required for custom views:

The android. graphics package is the core rendering package. It provides some basic graphics tools, such as canvas, color filter, and paint brush, allowing you to process images directly on the screen. This package provides many classes.

1. Canvas-Canvas is a large Canvas. The size of the Canvas is large. All views and implemented images are implemented on the Canvas. The Canvas class provides three constructor Methods: Public Canvas (); construct a Canvas object with no default parameters Public Canvas (Bitmap bitmap ); construct a Canvas object Public Canvas (GL gl) based on a Bitmap; construct an object based on a GL
2. Paint-Paint can be done without a Paint board. All the brushes in the Android kingdom. The Paint class has three constructor Methods: Public Paint () to construct a default Paint object Public Paint (int flags); to construct a Paint object based on the specified flags, after creation, you can use the setFlags () method to change Public Paint (Paint paint) to construct a paint object based on the specified Paint object.
3. Color -- Color. parseColor (String str); this function is too powerful to parse Alpha and normal colors. The color is expressed as an encapsulated value. The value consists of four bytes: alpha, red, green, and blue. These values are not left-multiplied, that is to say, any transparency is only stored in the alpha part, not in the color components. Each part is saved in the following sequence: (alpha <24) | (red <16) | 4. Bitmap-the Bitmap class can manage png and jpg images in the world. Three methods are used to obtain images in the Drawable Folder: 5. android. graphics. Typeface
The ypeface class defines the internal types of fonts and fonts. This class is used when the Paint brush is set. For example, textSize, textSkewX, and textScale are used to specify how text is displayed and measured during painting. Typeface provides some constant values to represent some of its own attributes, such as BOLD, BOLD_ITALIC, ITALIC, etc. 6, Path class android. graphics. pathPath class (a group of regions) is used for plotting. It contains multiple geometric figures, such as line segments, quadratic curves, and cubic curves. Canvas is called. the drawPath () method can be used to draw a Path in the defined paint mode to the drawing board or to fill out the image. It can also be used to draw the image in the method specified by the paint.
7. RectF android. graphics. RectF and Rect android. graphics. RectRectF contain four single-precision floating point coordinates of a rectangle. A rectangle is represented by the coordinates of the top, bottom, and left sides of the rectangle. These coordinate value attributes can be directly accessed, and the width and height of the rectangle can be obtained using the width () and height () methods. Note: Most methods do not check whether these coordinate classifications are incorrect (that is, left <= right and top <= bottom ). rectF has four construction methods: RectF () to construct a non-parametric RectF (float left, float top, float right, float bottom) construct a RectF (Rect F r) with four parameters specified. Construct a RectF object based on the specified RectF object (the left coordinate of the object remains unchanged) RectF (Rect r) many methods are provided to construct a RectF object RectF Based on the given Rect object. The following describes several methods: Public Boolean contain (RectF r); Determine whether a rectangle is inside this rectangle, returns true if it is within or equal to this rectangle. Similarly, public Boolean contain (float left, float top, float right, float bot Tom) and public Boolean contain (float x, float y ). Public void union (float x, float y) updates the rectangle so that it contains the vertex of the rectangle itself and (x, y. The methods provided by the RectF class are simple and easy to understand. I will not go into details about Android here. graphics. rect class, which is similar to android. graphics. rectF is very similar. The difference is that the coordinates of the Rect class are represented by an integer, while the coordinates of the RectF are represented by a single precision Floating Point.
To implement a custom View, follow these steps:

1. inherit View

  1. Public class MyView extends View {

    2. Override some methods of the parent class View, such as onDraw;

    1. @ Override
    2. Protected void onDraw (Canvas canvas ){
    3. Super. onDraw (canvas );
    4. /**
    5. * Draw the outer ring
    6. */
    7. Int center = getWidth ()/2; // obtain the x coordinate of the center
    8. Int radius = (int) (center-roundWidth/2); // circle radius
    9. Paint. setColor (roundColor); // you can specify the color of the ring.
    10. Paint. setStyle (Paint. Style. STROKE); // you can specify
    11. Paint. setStrokeWidth (roundWidth); // you can specify the ring width.
    12. Paint. setAntiAlias (true); // eliminate the Sawtooth
    13. Canvas. drawCircle (center, center, radius, paint); // draw a circle
    14. Log. e ("log", center + "");
    15. }

    16. 3. You can customize the View attributes based on the attributes required by onDraw (create the attrs. xml file under values );

              
                  
                                                                                                                 
                            
                              
                
              


      4. Apply the custom View in the layout or in the code, for example:

                

      5. instantiate the property in the activity and modify the property to generate a custom View;

Related Article

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.