Android的缩放画布位图 [英] Android Scaling Canvas Bitmap

查看:213
本文介绍了Android的缩放画布位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绘图应用程序,允许用户绘制到一张空白的画布。我试图绘制当前位图的缩放缩略图,这样,当用户在查看已缩放的,它们可以参考缩略图获取感,以他们在哪里,在总的拉伸帆布。我有缩放工作,以及正在显示在正确的位置的缩略图,但现在看来,缩略图不被更新时新行/形状添加后续onDraws

让我有机会获得潜在的位图这个观点(显示缩略图,能够将位图轻松保存到一个文件,等等)我在onSizeChanged下列()的观点:

  @覆盖
保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
    super.onSizeChanged(W,H,oldw,oldh);

    //设置画布高度,平移等的基础上,现在充气查看
    mWidth =的getWidth();
    mHeight =的getHeight();
    mAspectRatio = mWidth / mHeight;
    mPanX = 0;
    mPanY = 0;

    //创建位图,将其设置为画布
mBitmap = Bitmap.createBitmap(mWidth,mHeight,Bitmap.Config.ARGB_8888);
mCanvas.setBitmap(mBitmap);
画(mCanvas);
}
 

然后,当用户绘制和无效()被调用时,我做了以下的的OnDraw()来生成缩略图:

  @覆盖
保护无效的OnDraw(帆布油画){
    <剪断code,吸引到画布&GT的路径,形状;

    如果(mScaled){
        位图了= Bitmap.createScaledBitmap(mBitmap,(INT)thumbWidth,(INT)thumbHeight,假);
        canvas.drawBitmap(满分,空,thumbnailRectF,thumbCanvasPaint);
    }
}
 

缩略图获取显示在由thumbnailRectF使用thumbCanvasPaint限定的空间,但在随后的的OnDraw()调用,经缩放的位图并没有改变由它的原始状态,即使全尺寸的活动画布显示所有附图中,等等。根据一些测试,在我看来,虽然我设置的位图与初始调用绘制(mCanvas);,随后onDraws被写入底层位图,而不是onSizeChanged()指定的。

所以,我想我试图找出我怎么绑的OnDraw画布一个位图,我可以readliy访问进行重新的大小,保存等等看的这个问题,我认为平局(mCanvas);通话将平的OnDraw在mCanvas指定的位图(在我的情况,mBitmap),但在实践中,它似乎并不奏效,在只要updats到画布上的关注。

谢谢

解决方案

  canvas.drawBitmap(满分,空,thumbnailRectF,thumbCanvasPaint);
 

应更改为

  canvas.drawBitmap(满分,新的矩形(0,0,mBitmap.getWidht,mBitmap.getheight),thumbnailRectF,thumbCanvasPaint);
 

没有必要对

 位图了= Bitmap.createScaledBitmap(mBitmap,(INT)thumbWidth,(INT)....
 

此外,还应检查mScaled是真实的一切的时候,变焦大于1

I have a drawing app that allows the user to draw to a blank canvas. I am attempting to draw a scaled 'thumbnail' of the current bitmap so that when the user has scaled in the View, they can reference the thumbnail to get a sense as to where they are in the overall draw canvas. I have the scaling working, and am displaying the thumbnail in the correct location, but it appears that the thumbnail is not being updated on subsequent onDraws when new lines/shapes are added.

So that I have access to the underlying bitmap for this view (to show the thumbnail, be able to easily save the bitmap to a file, etc) I do the following in onSizeChanged() for the View:

@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);
}

Then, when the user draws and invalidate() is called, I do the following in onDraw() to generate the thumbnail:

@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);
    }
}

The thumbnail gets displayed in the space defined by thumbnailRectF using the thumbCanvasPaint, but in subsequent onDraw() calls, the scaled bitmap has not changed from it's original state, even though the full-sized active canvas shows all of the drawings, etc. Based on some testing, it seems to me that while I am setting the Bitmap with the initial call to draw(mCanvas);, subsequent onDraws are writing to the underlying Bitmap rather than the one specified in onSizeChanged().

So, I guess I am trying to figure out how I tie the onDraw canvas to a Bitmap that I can readliy access to perform re-sizes, save, etc. Looking at this question, I thought that the draw(mCanvas); call would tie the onDraw to the bitmap specified in the mCanvas (in my case, mBitmap), but in practice, it doesn't seem to be working, in so far as updats to the canvas are concerned.

Thanks,

Paul

解决方案

canvas.drawBitmap(out, null, thumbnailRectF, thumbCanvasPaint); 

should change to

canvas.drawBitmap(out, new Rect(0,0,mBitmap.getWidht, mBitmap.getheight), thumbnailRectF, thumbCanvasPaint);

There is no need for

Bitmap out = Bitmap.createScaledBitmap(mBitmap, (int) thumbWidth, (int)....

Also check that mScaled is true all the time when zoom is greater than 1

这篇关于Android的缩放画布位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆