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

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

问题描述

我想借鉴的ImageViewTouch,使捏缩放库。我能画在画布用的形象,但是当我缩放图像,绘图消失。

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此相同的观点。这里的code:

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 致电图纸通常显示在图像上。当我尝试做的图像的任何相互作用(例如放大或缩小),在图中消失,remaing只有背景图像,这是一个图像。

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.

---编辑---

这里的一些code放在 loadBitmapFromView 调用之后。此案是:我有一个单选按钮组listenner当我检查一些单选按钮,我必须加载图像和画一些图纸符合候选了它..然后我想一切(的形象和图纸)转换成唯一一个位图。

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.

这里的code的ohter部分:

Here's the ohter part of the code:

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.

德codeSampledBitmapFromResource 方法我从Android开发者这个环节得到了<一个href=\"http://developer.android.com/training/displaying-bitmaps/load-bitmap.html\">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.

我使用了查看,这只会认为测量和布点,这样的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.

这里的code样品:<​​/ P>

Here's the code sample:

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

希望它也可以帮助别人:)

Hope it also helps someone else :)

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

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