canvas教程

双缓冲技术实现Android 画板应用

字号+ 作者:H5之家 来源:H5之家 2017-01-24 15:03 我要评论( )

这篇文章主要介绍了Android 采用双缓存技术实现画板应用的相关资料,并附有代码实例,有需要的小伙伴可以参考下

package com.example.phonegaptest; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class DrawView extends View { float preX; float preY; private Path path; public Paint paint = null; final int VIEW_WIDTH = 320; final int VIEW_HEIGHT = 480; Bitmap cacheBitmap = null; Canvas cacheCanvas = null; public DrawView(Context context, AttributeSet set) { super(context, set); cacheBitmap = Bitmap.createBitmap(VIEW_WIDTH, VIEW_HEIGHT, Config.ARGB_8888); cacheCanvas = new Canvas(); path = new Path(); cacheCanvas.setBitmap(cacheBitmap); paint = new Paint(Paint.DITHER_FLAG); paint.setColor(Color.RED); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(1); paint.setAntiAlias(true); paint.setDither(true); } @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: path.moveTo(x, y); preX = x; preY = y; break; case MotionEvent.ACTION_MOVE: path.quadTo(preX, preY, x, y); preX = x; preY = y; break; case MotionEvent.ACTION_UP: cacheCanvas.drawPath(path, paint); path.reset(); break; } invalidate(); return true; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint bmpPaint = new Paint(); canvas.drawBitmap(cacheBitmap, 0, 0, bmpPaint); canvas.drawPath(path, paint); } }

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 试图清除 Android SurfaceView/Canvas 时闪烁

    试图清除 Android SurfaceView/Canvas 时闪烁

    2017-01-22 09:14

  • Android 自定义View学习(七)

    Android 自定义View学习(七)

    2017-01-20 18:09

  • Android开发之50个常见实用技巧——添加悦目的动画效果

    Android开发之50个常见实用技巧——添加悦目的动画效果

    2017-01-15 08:02

  • 用Shader实现特殊效果

    用Shader实现特殊效果

    2017-01-14 17:00

网友点评
l