android实现一个简单的画布,可以用手指画线条。
1个回答
2014-06-10
展开全部
public class DrawView extends View {
Context mycontext;
int toasttime = 1000*60;
boolean enabletoast = true;
//之前的坐标
float preX;
float preY;
//路径
private Path path;
//画笔
public Paint paint = null;
//默认画布大小
public static int VIEW_WIDTH = 500;
public static int VIEW_HEIGHT = 600;
//
Bitmap cacheBitmap = null;
//
Canvas cacheCanvas = null;
public DrawView(Context context, AttributeSet set) {
super(context, set);
mycontext = context;
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.WHITE);
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();
if(enabletoast == false){
}
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);
}
}
Context mycontext;
int toasttime = 1000*60;
boolean enabletoast = true;
//之前的坐标
float preX;
float preY;
//路径
private Path path;
//画笔
public Paint paint = null;
//默认画布大小
public static int VIEW_WIDTH = 500;
public static int VIEW_HEIGHT = 600;
//
Bitmap cacheBitmap = null;
//
Canvas cacheCanvas = null;
public DrawView(Context context, AttributeSet set) {
super(context, set);
mycontext = context;
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.WHITE);
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();
if(enabletoast == false){
}
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);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
博思aippt
2024-07-20 广告
2024-07-20 广告
博思AIPPT是基于ai制作PPT的智能在线工具,它提供了4种AI制作PPT的方式,包括AI生成大纲、AI直接生成PPT、文本生成PPT、AI提炼文档生成PPT,一站式集成多种AI生成PPT的方式,可满足办公用户的不同需求和使用场景。ai生...
点击进入详情页
本回答由博思aippt提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询