Dynamically populate Android ImageView with outside resources(使用外部资源动态填充 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
基础教程推荐
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
