使用外部资源动态填充 Android ImageView

Dynamically populate Android ImageView with outside resources(使用外部资源动态填充 Android ImageView)
本文介绍了使用外部资源动态填充 Android ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何转静态图片

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.67"
        android:src="@drawable/static_image" />

到一个 ImageView 中,它的源可以动态设置为不在 res 文件夹中的数据?

into an ImageView whose source can be dynamically set to data that is not already in the res folder?

也就是说,我的应用程序在屏幕上有一个图标,但图标的实际图像是从外部服务器下载的,并且可以动态更改.如何在下载时使用所需的图像更新 ImageView?我想要一些功能上的东西:

That is, my application has an icon on the screen but the actual image for the icon is downloaded from an outside server and can change dynamically. How do I update the ImageView with the desired image upon download? I want something functionally like:

Image selectedImage = //get from server

myImageView.setImage(selectedImage);

推荐答案

你的问题不清楚.如果您只想将图像(在某个 url 中)设置为图像视图,

Ur question is not clear. If u just wanna have an image(that is in some url) set to an image view,

Bitmap bmp=getBitmapFromURL(ur url here);
imgview.setImageBitmap(bmp);

并编写这个函数:

public static Bitmap getBitmapFromURL(String src) {  
        try {

            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap mybitmap = BitmapFactory.decodeStream(input);

            return mybitmap;

        } catch (Exception ex) {

            return null;
        }

这篇关于使用外部资源动态填充 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中同步两个平面列表滚动位置)
appearance().setBackgroundImage Not Working On Custom Class(外观().setBackoundImage在自定义类上不起作用)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)