Android View how to draw _android

Source: Internet
Author: User

That's how Android is measured , but a pretty control I just know where you are and that's not going to work. Simply rewrite the OnDraw method and invoke the colorful brush on the canvas (Canvas) object to draw the "sexy" appearance of the control. So how does the view draw?

To understand how the view is drawn, you need to know what the canvas (canvas) is? What paint (brushes) can do.

Ⅰ, canvas means a canvas, where you can paint what you are dreaming of. When we rewrite the OnDraw method, we can get a canvas object, this is your stage, draw what you think. On the code.

@Override
  protected void OnDraw (Canvas Canvas) {
    super.ondraw (Canvas);
  }

When you need to repaint a certain control, we only need to call the Canvas.drawxxx method on this large stage (Canvas) to draw a variety of graphics.

Ⅱ and paint are the aplomb brushes on the canvas. To paint, you have to adjust the brush, after the brush is adjusted, and then draw the image to the canvas, so that it can be displayed on the phone screen. Paint provides a number of properties and methods to set the brush.

1, Setantialias: Set the brush's sawtooth effect.
2, SetColor: Set brush color
3, Setargb: Set the a,r,p,g value of the brush.
4, Setalpha: Set alpha value
5, Settextsize: Set the font size.
6, SetStyle: Set brush style, hollow or solid.
7, Setstrokewidth: Set the outline width of the hollow.
8, GetColor: Get the color of the brush
9, Getalpha: Get the alpha value of the brush.

Now that we know the brushes and the canvas, let's look at how the canvas and the brush work together to create the desired shape with a dapper example. As usual, on the code:

public class MyView extends View {private Paint Paint;
    Public MyView (context, AttributeSet attrs) {Super (context, attrs);
  Init ();
    private void Init () {paint=new paint (paint.anti_alias_flag);
    Paint.setcolor (color.red);
    Paint.setstrokewidth (5);
    
  Paint.settextsize (28); @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {setmeasureddimension (measurew
  Idth (Widthmeasurespec), Measurewidth (Heightmeasurespec));
    public int measurewidth (int measurespec) {int = 0;
    int specmode = Measurespec.getmode (Measurespec);
    int specsize = measurespec.getsize (Measurespec);
    if (Specmode = = measurespec.exactly) {result = Specsize;
      else {result = 200;
      if (Specmode = = measurespec.at_most) {result = Math.min (specsize, result);
  } return result; @Override protected void OnDraw (Canvas Canvas) {canvas.drawtext ("China", 0, 80, paint);
  Super.ondraw (canvas);


 }
}

The above code, we have done so several things:

Ⅰ, we use the knowledge of the previous article, rewrite the Onmeasure method, let this simple custom control adaptive wrap_content properties.

Ⅱ, we call the OnDraw method, get canvas and paint, wrote "China" text in view.

The final run effect is:

That's how I draw a little insights on Android View, and I hope you'll make some suggestions.

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.