Android canvas implementation and demo, android canvas demo

Source: Internet
Author: User
Tags gety

Android canvas implementation and demo, android canvas demo

Today we bring you a simple implementation of the Android canvas function. The following is:



The following are the key source code:


import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Paint.Style;import android.graphics.PaintFlagsDrawFilter;import android.graphics.Path;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.view.SurfaceHolder;import android.view.SurfaceHolder.Callback;import android.view.SurfaceView;public class MyView extends SurfaceView implements Callback,OnTouchListener{private Paint p = new Paint();private Path path = new Path();public MyView(Context context, AttributeSet attrs) {super(context, attrs);getHolder().addCallback(this);p.setColor(Color.RED);p.setTextSize(10);p.setAntiAlias(true);p.setStyle(Style.STROKE);setOnTouchListener(this);}public void draw(){Canvas canvas = getHolder().lockCanvas();canvas.drawColor(Color.WHITE);canvas.drawPath(path, p);getHolder().unlockCanvasAndPost(canvas);}public void clear(){path.reset();draw();}@Overridepublic void surfaceCreated(SurfaceHolder holder) {draw();}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {// TODO Auto-generated method stub}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {// TODO Auto-generated method stub}@Overridepublic boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:path.moveTo(event.getX(), event.getY());draw();break;case MotionEvent.ACTION_MOVE:path.lineTo(event.getX(), event.getY());draw();break;}return true;}}

Demo Apk and its source code download: http://pan.baidu.com/s/1i33QO9R


You are welcome to pay attention to the Public Account of the Android developer community. The official website will occasionally bring you some demos or frameworks for android development with high quality trials to improve your development efficiency, we provide you with a high quality android demo source code.

Scan the QR code and follow these steps:


 

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.