用android做了一个涂鸦板的小应用,画布设置为白色,但是存储时图片背景不为白色,求大神指正
publicDrawView(Contextcontext,AttributeSetattrs){super(context,attrs);//获取屏幕尺寸view_he...
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
// 获取屏幕尺寸
view_height = context.getResources().getDisplayMetrics().heightPixels;
view_width = context.getResources().getDisplayMetrics().widthPixels;
// 创建一个与该view相同大小的缓冲区
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.BLACK);// 设置默认画笔颜色
// 设置画笔风格
paint.setStyle(Paint.Style.STROKE);// 填充方式为描边
paint.setStrokeJoin(Paint.Join.ROUND);// 笔刷图形样式
paint.setStrokeCap(Paint.Cap.ROUND);// 设置画笔转弯去的连接风格
paint.setAntiAlias(true);// 抗锯齿
paint.setStrokeWidth(1);// 笔触的宽度
paint.setDither(true);// 使用抖动效果
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
Paint bmpPaint = new Paint();
canvas.drawBitmap(cacheBitmap, 0, 0, bmpPaint);
canvas.drawPath(path, paint);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
}
@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:
float dx = Math.abs(x - preX);
float dy = Math.abs(y - preY);
if (dx >= 5 || dy >= 5) {
path.quadTo(preX, preY, (x + preX) / 2, (y + preY) / 2);
preX = x;
preY = y;
}
break;
case MotionEvent.ACTION_UP:
cacheCanvas.drawPath(path, paint);
path.reset();
break;
}
invalidate();
return true;
} 展开
super(context, attrs);
// 获取屏幕尺寸
view_height = context.getResources().getDisplayMetrics().heightPixels;
view_width = context.getResources().getDisplayMetrics().widthPixels;
// 创建一个与该view相同大小的缓冲区
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.BLACK);// 设置默认画笔颜色
// 设置画笔风格
paint.setStyle(Paint.Style.STROKE);// 填充方式为描边
paint.setStrokeJoin(Paint.Join.ROUND);// 笔刷图形样式
paint.setStrokeCap(Paint.Cap.ROUND);// 设置画笔转弯去的连接风格
paint.setAntiAlias(true);// 抗锯齿
paint.setStrokeWidth(1);// 笔触的宽度
paint.setDither(true);// 使用抖动效果
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
Paint bmpPaint = new Paint();
canvas.drawBitmap(cacheBitmap, 0, 0, bmpPaint);
canvas.drawPath(path, paint);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
}
@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:
float dx = Math.abs(x - preX);
float dy = Math.abs(y - preY);
if (dx >= 5 || dy >= 5) {
path.quadTo(preX, preY, (x + preX) / 2, (y + preY) / 2);
preX = x;
preY = y;
}
break;
case MotionEvent.ACTION_UP:
cacheCanvas.drawPath(path, paint);
path.reset();
break;
}
invalidate();
return true;
} 展开
1个回答
logo在线生成器
2024-10-28 广告
2024-10-28 广告
在上海数都信息科技有限公司,我们致力于将创意与技术完美融合,将文字的力量转化为直观生动的图标设计。想象一下,一段精炼的文案——“数据洞察未来,科技引领创新”,在我们的设计师笔下,这不仅仅是一行字,而是化作了一个充满现代感的图标:中心是旋转的...
点击进入详情页
本回答由logo在线生成器提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询