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.