如何获取时的ImageView在Android是完全加载加载、ImageView、Android

2023-09-08 09:42:03 作者:姐的霸气归属

我开发一个应用,该应用在一堆图像绘制线条。要选择这些图片,我有一个单选按钮组和,每当用户点击一个单选按钮,图像负载自己的所有图纸。

I'm developing an App which draws lines over a bunch of Images. To choose these images, I have a radio group and, whenever the user clicks in a radio button, the image is load with all its own drawings.

在我的收音机listenner我有以下的code:

In my radio listenner I have the following code:

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

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

德codeSampledBitmapFromResource 方法我从Android开发者这个环节得到了(它加载位图更effitiently)的 http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

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

和这里的方法,我称之为得到一个视图的位图

And here's the method I call to get a Bitmap of a View

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

我设置 mImage 的位图图像因为我使用ImageViewTouch库(这使得捏缩放通过一个ImageView的),如果我不这样做,所有的绘图画布与在图像任何交互删除(如放大/缩小)。

I'm setting the image Bitmap of mImage because I'm using ImageViewTouch library (which enables pinch zooming over an ImageView) and if I don't do it, all the canvas drawing is deleted with any interaction over the image (like zooming in/out).

错误日志以下

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)

我几乎可以肯定,这个错误是发生因为当我打电话 getBitmapFromView 方法的位图图像不完全加载。

I'm almost sure that this error is occuring cause the image bitmap is not completely loaded when I call getBitmapFromView method.

我怎么能知道当视图载入?

How can I know when the view is loaded completely?

推荐答案

呼叫 loadBitmapFromView 以这样的方式:

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

的Runnable 提供的后()方法将视图的测量和布点后执行,因此,的getWidth()的getHeight()将返回实际宽度和高度。

Runnable provided to post() method will be executed after view measuring and layouting, so getWidth() and getHeight() will return actual width and height.

您还有什么可以做,是手工测量查看,通过调用测量,然后取结果从 getMeasuredWidth() getMeasuredHeight()。但我不推荐这种方式。

What else can you do, is measuring View manually, by invoking measure, and then taking result from getMeasuredWidth() and getMeasuredHeight(). But I do not recommend this way.

 
精彩推荐
图片推荐