IllegalArgumentException:宽度和高度必须是 >0 从视图加载位图时 [英] IllegalArgumentException: width and height must be > 0 while loading Bitmap from View

查看:37
本文介绍了IllegalArgumentException:宽度和高度必须是 >0 从视图加载位图时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 ImageViewTouch 上绘图,这是一个启用捏缩放的库.我可以使用 Canvas 在图像上绘图,但是当我缩放图像时,绘图消失了.

I'm trying to draw on an ImageViewTouch, a library which enables pinch zooming. I'm able to draw over the image using Canvas, but when I zoom the image, the drawing disappears.

为此,我正在尝试将视图转换为位图并为同一视图设置 theImageBitmap.代码如下:

For this, I'm trying to convert the view to a bitmap and set theImageBitmap for this same view. Here's the code:

mImage.setDrawPath(true);
mImage.setImageBitmap(loadBitmapFromView(mImage)); 

public static Bitmap loadBitmapFromView(View v) {
        Bitmap b = Bitmap.createBitmap( v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);                
        Canvas c = new Canvas(b);
        v.layout(0, 0, v.getWidth(), v.getHeight());
        v.draw(c);
        return b;
}

当我这样做时,我收到以下日志错误:

When I do this, I get the following log error:

07-11 21:13:41.567: E/AndroidRuntime(20056): java.lang.IllegalArgumentException: width and height must be > 0
07-11 21:13:41.567: E/AndroidRuntime(20056):    at android.graphics.Bitmap.createBitmap(Bitmap.java:638)
07-11 21:13:41.567: E/AndroidRuntime(20056):    at android.graphics.Bitmap.createBitmap(Bitmap.java:620)

如果我删除 loadBitmapFromView 调用,绘图通常会出现在图像上.当我尝试与图像进行任何交互(如放大或缩小)时,绘图会消失,只保留背景图像,即图片.

If I remove the loadBitmapFromView call the drawing normally appears over the image. When I try to do any interaction with the image (like zooming in or out), the drawing disappears, remaing only the background image, which is a picture.

--- 编辑 ---

这是在 loadBitmapFromView 调用之后放置的更多代码.情况是:我有一个广播组监听器,当我检查一些单选按钮时,我必须加载图像并在其上绘制一些可能的图纸.然后我试图将所有内容(图像和图纸)转换为一张位图.

Here's some more code placed after the loadBitmapFromView call. The case is: I have a radio group listenner and when I check some radio button, I have to load the image and draw some possibles drawings over it.. then I'm trying to convert everything (the image and the drawings) into only one bitmap.

这是代码的其他部分:

bitmap = BitmapUtils.decodeSampledBitmapFromResource(root + DefinesAndroid.CAMINHO_SHOPPINGS_SDCARD + nomeImagemAtual, size.x, size.y);
mImage.setImageBitmap(bitmap);

之后,我绘制了我必须绘制的所有内容,并尝试使用我展示的 loadImageBitmap 方法将视图转换为位图.

After that, I draw everything I have to draw and try to convert the view to bitmap using the loadImageBitmap method I have shown.

decodeSampledBitmapFromResource 方法我从这个关于 android 开发者的链接 http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

the decodeSampledBitmapFromResource method I got from this link on android developers http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

推荐答案

我终于找到了解决问题的办法.

I finally figured out a solution for my problem.

我用的是View的post方法,只有在view测量布局后才会执行,这样getWidth()getHeight() 返回实际的宽度和高度.

I'm using the post method of the View, which will be executed only after the view measuring and layouting, this way getWidth() and getHeight() returns the actual width and height.

这是代码示例:

mImage.post(new Runnable() {
    @Override
    public void run() {
        mImage.setImageBitmap(loadBitmapFromView(mImage));
    }
});

希望它也对其他人有所帮助:)

Hope it also helps someone else :)

这篇关于IllegalArgumentException:宽度和高度必须是 >0 从视图加载位图时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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