Easy Tutorial
❮ Home Android Tutorial Broadcastreceiver ❯

8.3.1 Detailed Explanation of Three Drawing Tool Classes

Category Android Basic Tutorial

Introduction to This Section:

>

In the previous two sections, we studied Drawable and Bitmap, which are both about loading images, and in this section, we are going to learn about some drawing-related APIs, namely Canvas, Paint, and Path. This section is very important and also the foundation for our custom Views. Alright, without further ado, let's start the content of this section.

Official API documentation: Canvas; Paint; Path;


1. Detailed Explanation of Related Methods


1) Paint (Brush):

>

It is a brush used to set the drawing style, such as line width (stroke thickness), color, transparency, and fill style, etc. You can create an instance of Paint directly with a no-argument constructor: Paint paint = new Paint( );

We can set the relevant attributes of Paint (brush) through the following methods, in addition, there are two types of attributes, related to graphic drawing and text drawing:

For more advanced effects, you can use the PathEffect class!

Several To:


2. Try it yourself:

>

There are so many properties, we definitely need to try them out to deepen our impression, right~ Hehe, drawing can either be done on a View or a SurfaceView, let's draw on a View here, We define a View class, and then complete the drawing work in onDraw().

/**
 * Created by Jay on 2015/10/15 0015.
 */
public class MyView extends View{

    private Paint mPaint;

    public MyView(Context context) {
        super(context);
        init();
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        mPaint = new Paint();
        mPaint.setAntiAlias(true);          //Anti-aliasing
        mPaint.setColor(getResources().getColor(R.color.puple));//Brush color
        mPaint.setStyle(Paint.Style.FILL);  //Brush style
        mPaint.setTextSize(36);             //Text size for drawing, unit px
        mPaint.setStrokeWidth(5);           //Brush thickness
    }

    //Override this method to draw here
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

}

Then set this View in the layout, the following code is all written in onDrawable~


1) Set canvas color:

canvas.drawColor(getResources().getColor(R.color.yellow));   //Set canvas background color

2) Draw a circle:

canvas.drawCircle(200, 200, 100, mPaint);           //Draw a solid circle

3) Draw a rectangle:

canvas.drawRect(0, 0, 200, 100, mPaint);            //Draw a rectangle

4) Draw a Bitmap:

canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher), 0, 0, mPaint);

5) Draw an arc area:

canvas.drawArc(new RectF(0, 0, 100, 100),0,90,true,mPaint);  //Draw an arc area

If true is changed to false:


6) Draw a rounded rectangle

``` canvas.drawRoundRect(new RectF

❮ Home Android Tutorial Broadcastreceiver ❯