简体   繁体   English

动态设置ImageView.setImageUri不显示图片

[英]Dynamically setting ImageView.setImageUri not displaying image

I have the following scenario:我有以下情况:

I am dynamically assigning an image URI to an ImageView, and then dynamically adding this ImageView to a HorizontalView.我正在将一个图像 URI 动态分配给一个 ImageView,然后将这个 ImageView 动态添加到一个 HorizontalView。

The problem is, I don't see these images loaded in the ImageView, inside the HorizontalView.问题是,我没有在 HorizontalView 内的 ImageView 中看到这些图像。

Here is my code:这是我的代码:

<HorizontalScrollView
                android:layout_width="wrap_content"
                android:layout_height="100dp">
                <LinearLayout
                    android:id="@+id/imageDisplayer"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal">

                </LinearLayout>
            </HorizontalScrollView>

Here is my code for loading the images:这是我加载图像的代码:

public void LoadImages(ArrayList<PhotoPath> photos){
        LinearLayout layout = (LinearLayout)findViewById(R.id.imageDisplayer);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        if (photos != null) {
            for (PhotoPath photo : photos) {
                Uri photoUri = Uri.parse(photo.getPhotoPath());

                params.setMargins(10, 10, 10, 10);
                params.gravity = Gravity.NO_GRAVITY;

                ImageView imageView = new ImageView(this);
                imageView.setImageURI(photoUri);
                imageView.setLayoutParams(params);

                layout.addView(imageView);
            }
        }
    }

Here is an example of my URI:这是我的 URI 的示例:

content://com.android.providers.media.documents/document/image%3A48内容://com.android.providers.media.documents/document/image%3A48

What am I doing wrong?我究竟做错了什么?

setImageURI(Uri uri) does Bitmap reading and decoding on the UI thread . setImageURI(Uri uri)在UI线程上进行Bitmap读取和解码。 So this may cause delay in MainThread cause image size can be varry and it will take time to load . 因此,这可能会导致MainThread延迟,从而导致图像大小可变,并且需要花费一些时间来加载。 And as its on Main Thread so it can cause letency issue. 并且由于其在主线程上,因此可能导致延迟问题。

From the Documentation 从文档中

setImageURI(Uri uri) does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. setImageURI(Uri uri)在UI线程上进行位图读取和解码,这可能会导致延迟打h。 If that's a concern, consider using setImageDrawable(Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead. 如果这是一个问题,请考虑改为使用setImageDrawable(Drawable)或setImageBitmap(android.graphics.Bitmap)和BitmapFactory。

Effective solution is to use a Optimized ImageLoader library. 有效的解决方案是使用优化的ImageLoader库。 You can use Glide as it is widely recommended. 可以广泛使用Glide

Had the similar, as I understood xml properties like (app:tint="@color/white") or others in your case in the layout file were hiding my image.有类似的,据我所知 xml 属性,如 (app:tint="@color/white") 或布局文件中您的其他属性隐藏了我的图像。

After these lines setImageUri works:在这些行 setImageUri 工作之后:

binding.image.setImageTintMode(null);
binding.image.setImageURI(uri);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM