问题描述
我正在寻找将图像 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 显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!