Pau*_*ega 5 android bitmap android-canvas
我有一个绘图应用程序,允许用户绘制到空白画布.我正在尝试绘制当前位图的缩放"缩略图",以便当用户在视图中缩放时,他们可以引用缩略图以了解它们在整个绘制画布中的位置.我有缩放工作,并在正确的位置显示缩略图,但是当添加新的线条/形状时,似乎在后续的onDraw上没有更新缩略图.
这样我就可以访问这个视图的底层位图(显示缩略图,能够轻松地将位图保存到文件中等).我在视图的onSizeChanged()中执行以下操作:
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// set the canvas height, panning, etc, based on the now inflated View
mWidth = getWidth();
mHeight = getHeight();
mAspectRatio = mWidth / mHeight;
mPanX = 0;
mPanY = 0;
// create the Bitmap, set it to the canvas
mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
mCanvas.setBitmap(mBitmap);
draw(mCanvas);
}
Run Code Online (Sandbox Code Playgroud)
然后,当用户绘制并且调用invalidate()时,我在onDraw()中执行以下操作以生成缩略图:
@Override
protected void onDraw(Canvas canvas) {
<snipped code that draws paths, shapes to canvas>
if (mScaled) {
Bitmap out = Bitmap.createScaledBitmap(mBitmap, (int) thumbWidth, (int) thumbHeight, false);
canvas.drawBitmap(out, null, thumbnailRectF, thumbCanvasPaint);
}
}
Run Code Online (Sandbox Code Playgroud)
缩略图使用thumbCanvasPaint显示在thumbnailRectF定义的空间中,但在后续的onDraw()调用中,缩放的位图没有从其原始状态更改,即使全尺寸活动画布显示所有绘图等.在一些测试中,在我看来,当我使用draw(mCanvas);的初始调用设置Bitmap时,后续onDraws正在写入底层Bitmap而不是onSizeChanged()中指定的Bitmap.
所以,我想我想弄清楚我是如何将onDraw画布绑定到一个Bitmap,我可以读取它来执行重新调整大小,保存等等.看着这个问题,我认为是绘制(mCanvas); call会将onDraw绑定到mCanvas中指定的位图(在我的例子中是mBitmap),但实际上,就canvas的更新而言,它似乎不起作用.
谢谢,
保罗
canvas.drawBitmap(out, null, thumbnailRectF, thumbCanvasPaint);
Run Code Online (Sandbox Code Playgroud)
应该改为
canvas.drawBitmap(out, new Rect(0,0,mBitmap.getWidht, mBitmap.getheight), thumbnailRectF, thumbCanvasPaint);
Run Code Online (Sandbox Code Playgroud)
没有必要
Bitmap out = Bitmap.createScaledBitmap(mBitmap, (int) thumbWidth, (int)....
Run Code Online (Sandbox Code Playgroud)
同时检查缩放大于1时mScaled是否为真
| 归档时间: |
|
| 查看次数: |
20044 次 |
| 最近记录: |