Canvas与Bitmap画图的问题

逐Ls梦 2012-09-14 11:06:50
这是自定义的一个View.做为涂鸦用的,两个问题,1.怎么把mBitmap放到画板中间,并且整个画板都可以执行画的操作,因为目前来说R.drawable.bg在左上角并且只能在图片上做画的操作,空白处不能。2.另一个是怎么作清除画板上的已经画的东西和撤销功能。知道的请说下,成分感谢
代码如下:
public class CanvasView extends View {

private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
private Paint mPaint;

public CanvasView(Context c) {
super(c);
mPaint = new Paint();

mBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.bg).copy(Bitmap.Config.ARGB_8888, true);
// mBitmap = Bitmap.createBitmap(300, 400, Bitmap.Config.ARGB_8888);
// mBitmap = Bitmap.createBitmap(mBitmap);
//Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
mBitmap = Bitmap.createBitmap(mBitmap , 0, 0, 300, 400);

mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);

mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xff000000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(5);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}

@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFFFFFFF);// set background color

canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

canvas.drawPath(mPath, mPaint);
}
...全文
338 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
今夜不谈风月 2012-09-14
  • 打赏
  • 举报
回复
Canvas进行new的时候不要直接用Bitmap,而是等new好了以后在drawBitmap,这样就能控制它的位置了。

清除的话直接 drawColor成你的背景色
逐Ls梦 2012-09-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

使用 canvas.DrawBitmap()
[/Quote]
能具体说下吗?
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFFFFFFF);// set background color
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint)是改这里么?把0,0设大一点是吧?担是这样的话涂鸦的时候画完后也就跟把把画的东西x,y轴上加了相应的值。
avcodec-decoder 2012-09-14
  • 打赏
  • 举报
回复
使用 canvas.DrawBitmap()
逐Ls梦 2012-09-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

Canvas进行new的时候不要直接用Bitmap,而是等new好了以后在drawBitmap,这样就能控制它的位置了。

清除的话直接 drawColor成你的背景色
[/Quote]


mBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.bg).copy(Bitmap.Config.ARGB_8888, true);
// mBitmap = Bitmap.createBitmap(300, 400, Bitmap.Config.ARGB_8888);
// mBitmap = Bitmap.createBitmap(mBitmap);
//Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
mBitmap = Bitmap.createBitmap(mBitmap , 0, 0, 300, 400);

mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
这段代码和onDraw方法里面该怎么改,能说下吗?

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧