Android drawing mechanism: Canvas Initial solution

Source: Internet
Author: User

Reprint please specify transfer from: noyet12 's Blog
Blog site: http://blog.csdn.net/u012975705/article/details/49992947

Canvas is the meaning of "canvas", which is used for 2D painting in Android.
When using canvas for drawing, you will typically customize a view to override its OnDraw method:

publicclass CustomCanvas extends View {    publicCustomCanvas(Context context) {        super(context);    }    @Override    protectedvoidonDraw(Canvas canvas) {        super.onDraw(canvas);    }}

Since it's a drawing, it's natural to have brushes: Paint,canvas draws images by borrowing paint.
Canvas, there are rotate, scale, translate and so on, such as rotation, scaling, translation, for these methods must be noted: the operation of the imagination is the canvas, rotation, scaling, panning is the canvas, And when drawing with canvas, the coordinate parameters are relative to the canvas rather than the screen itself. The single language may not be well understood, given an example below.
Canvasdemo:

First step customizing View and overriding the OnDraw method

 PackageCom.noyet.practice.canvasdemo;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.graphics.Rect;ImportAndroid.graphics.RectF;ImportAndroid.view.View;/** * Package:com.noyet.practice.canvasdemo * Created by Noyet on 2015/11/23. */ Public  class customcanvas extends View {    PrivatePaint Mpaint;PrivateRECTF Mrect; Public Customcanvas(Context context) {Super(context); }@Override    protected void OnDraw(Canvas canvas) {Super. OnDraw (canvas); Mpaint =NewPaint (); Mpaint.setantialias (true);        Mpaint.setcolor (color.red); Mrect =NewRECTF ( -, -, Max, Max); Canvas.drawrect (Mrect, mpaint);//Canvas.save ();Canvas.scale (2F2f); Canvas.rotate (Ten, -, -);        Mpaint.setcolor (Color.gray); Canvas.drawrect (Mrect, mpaint);//Canvas.restore ();}}

The second step is shown in the screen.

 PackageCom.noyet.practice.canvasdemo;Importandroid.app.Activity;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Paint;ImportAndroid.graphics.Rect;ImportAndroid.os.Bundle;ImportAndroid.view.View; Public  class mainactivity extends Activity {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (NewCustomcanvas ( This)); }}

Drawing results:

As you can see, when calling the Canvas.scale (2f, 2f) method and the time weights of the even coordinates are twice times the original

Android drawing mechanism: Canvas First solution

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.