如何在 Android 中完全加载 ImageView

How to get when an ImageView is completely loaded in Android(如何在 Android 中完全加载 ImageView)
本文介绍了如何在 Android 中完全加载 ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个在一堆图像上画线的应用程序.为了选择这些图像,我有一个单选组,每当用户单击单选按钮时,图像都会加载所有自己的绘图.

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.

在我的收音机监听器中,我有以下代码:

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

decodeSampledBitmapFromResource 方法我从这个关于 android 开发者的链接获得(它更有效地加载位图)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 提供给 post() 方法将在视图测量和布局后执行,因此 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.

您还能做什么,通过调用 measure 手动测量 View,然后从 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.

这篇关于如何在 Android 中完全加载 ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)