Android画板的实现及demo
今天给大家带来Android画板功能的简单实现,以下是效果图:
以下是关键源码:
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(); } @Override public void surfaceCreated(SurfaceHolder holder) { draw(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } @Override public 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及其源码下载:http://pan.baidu.com/s/1i33QO9R
欢迎大家关注Android开发者社区微信公众号,官方会不定期给大家带来一些android开发优质试用的demo或框架,提高大家的开发效率,为大家提供保证优质的android demo源码。
扫描一下二维码关注:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。