Android plotting mechanism: canvas initial solution

Source: Internet
Author: User

Android plotting mechanism: canvas initial solution

 

Canvas refers to "Canvas", which is used for 2D painting in Android.
When using canvas for drawing, a View is usually customized to override its onDraw method:

public class CustomCanvas extends View {    public CustomCanvas(Context context) {        super(context);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);    }}

Since it is a drawing, the Paint brush: paint is indispensable, and canvas uses Paint to draw an image.
In the canvas, there are methods such as rotate, scale, and translate, that is, rotation, scaling, and translation. For these methods, you must note that the operation is an imaginary canvas, the canvas is rotated, scaled, and translated, and the coordinate parameters in the canvas are relative to the canvas rather than the screen itself. The single pass language may not be easy to understand. The following example is provided.
CanvasDemo:

Step 1 customize the View and override the onDraw Method

package com.noyet.practice.canvasdemo;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.graphics.RectF;import android.view.View;/** * package: com.noyet.practice.canvasdemo * Created by noyet on 2015/11/23. */public class CustomCanvas extends View {    private Paint mPaint;    private RectF mRect;    public CustomCanvas(Context context) {        super(context);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        mPaint = new Paint();        mPaint.setAntiAlias(true);        mPaint.setColor(Color.RED);        mRect = new RectF(100, 100, 150, 150);        canvas.drawRect(mRect, mPaint);//        canvas.save();        canvas.scale(2f, 2f);        canvas.rotate(10, 100, 100);        mPaint.setColor(Color.GRAY);        canvas.drawRect(mRect, mPaint);//        canvas.restore();    }}

Step 2 is displayed on the screen

package com.noyet.practice.canvasdemo;import android.app.Activity;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Rect;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(new CustomCanvas(this));    }}

Drawing result:

As you can see, when you call the canvas. scale (2f, 2f) method and, the time weight of the concatenation subject is twice the original.

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.