How to show image using ImageView in Android(如何在 Android 中使用 ImageView 显示图像)
问题描述
我正在寻找将图像 src 分配给图像视图控件的方法.我读了几个例子,他们说了一些 src="@drawableimage" 但不明白这一点,我也想在运行时通过 java 代码分配图像 src 也想在 XML 中应用默认图像.
I am looking for the way to assign image src to image view control. I read few example and they says something src="@drawableimage" but didn't understand this, also I want to assign image src at runtime by java code also want to apply default image in XML.
推荐答案
如果要在手机上显示图片文件,可以这样:
If you want to display an image file on the phone, you can do this:
private ImageView mImageView;
mImageView = (ImageView) findViewById(R.id.imageViewId);
mImageView.setImageBitmap(BitmapFactory.decodeFile("pathToImageFile"));
如果您想从可绘制资源中显示图像,请执行以下操作:
If you want to display an image from your drawable resources, do this:
private ImageView mImageView;
mImageView = (ImageView) findViewById(R.id.imageViewId);
mImageView.setImageResource(R.drawable.imageFileId);
您将在项目 res 文件夹中找到 drawable 文件夹.你可以把你的图片文件放在那里.
You'll find the drawable folder(s) in the project res folder. You can put your image files there.
这篇关于如何在 Android 中使用 ImageView 显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Android 中使用 ImageView 显示图像
基础教程推荐
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
