使用外部资源动态填充 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用外部资源动态填充 Android ImageView

基础教程推荐