Android 更改 ImageView 源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2974862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing ImageView source
提问by nourdine
I have an ImageView
with a source image set in the xml using the following syntax:
我ImageView
使用以下语法在 xml 中设置了一个源图像:
<ImageView
android:id="@+id/articleImg"
style="@style/articleImgSmall_2"
android:src="@drawable/default_m" />
Now I need to change this image programmatically. What I need to do is delete the old image and add a new one though. What I have done is this:
现在我需要以编程方式更改此图像。我需要做的是删除旧图像并添加一个新图像。我所做的是这样的:
myImgView.setBackgroundResource(R.drawable.monkey);
It works but I noticed android stacks the new image on top of the old one (dont ask me how I found out it's not relevant for the discussion :). I definitely need to get rid of the old one before setting the new image.
它有效,但我注意到 android 将新图像堆叠在旧图像之上(不要问我是如何发现它与讨论无关的:)。在设置新图像之前,我绝对需要摆脱旧图像。
How can I achieve that?
我怎样才能做到这一点?
回答by Jorgesys
Changing ImageView source:
更改 ImageView 源:
Using setBackgroundResource()
method:
myImgView.setBackgroundResource(R.drawable.monkey);
you are putting that monkey in the background.
你把那只猴子放在背景中。
I suggest the use of setImageResource()
method:
我建议使用setImageResource()
方法:
myImgView.setImageResource(R.drawable.monkey);
or with setImageDrawable()
method:
或使用setImageDrawable()
方法:
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.monkey));
*** With new android API 22 getResources().getDrawable()
is now deprecated. This is an example how to use now:
*** 使用新的 android API 22getResources().getDrawable()
现在已弃用。这是现在如何使用的示例:
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.monkey, getApplicationContext().getTheme()));
and how to validate for old API versions:
以及如何验证旧 API 版本:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.monkey, getApplicationContext().getTheme()));
} else {
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.monkey));
}
回答by dxh
You're supposed to use setImageResource
instead of setBackgroundResource
.
你应该使用setImageResource
而不是setBackgroundResource
.
回答by PIR FAHIM SHAH
myImgView.setImageResource(R.drawable.monkey);
is used for setting image in the current image view, but if want to delete this image then you can use this code like:
用于在当前图像视图中设置图像,但如果要删除此图像,则可以使用以下代码:
((ImageView) v.findViewById(R.id.ImageView1)).setImageResource(0);
now this will delete the image from your image view, because it has set the resources value to zero.
现在这将从您的图像视图中删除图像,因为它已将资源值设置为零。
回答by Neha Shukla
get ID of ImageView as
获取 ImageView 的 ID 为
ImageView imgFp = (ImageView) findViewById(R.id.imgFp);
then Use
然后使用
imgFp.setImageResource(R.drawable.fpscan);
to set source image programatically instead from XML.
以编程方式而不是从 XML 设置源图像。
回答by radu_paun
Or try this one. For me it's working fine:
或者试试这个。对我来说它工作正常:
imageView.setImageDrawable(ContextCompat.getDrawable(this, image));
回答by Suragch
回答by CommonSenseCode
If you want to set in imageview an image that is inside the mipmap dirsyou can do it like this:
如果你想在 imageview 中设置一个位于mipmap 目录中的图像,你可以这样做:
myImageView.setImageDrawable(getResources().getDrawable(R.mipmap.my_picture)
myImageView.setImageDrawable(getResources().getDrawable(R.mipmap.my_picture)
回答by LOW
Just write a method for changing imageview
只写一个改变imageview的方法
public void setImage(final Context mContext, final ImageView imageView, int picture)
{
if (mContext != null && imageView != null)
{
try
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
imageView.setImageDrawable(mContext.getResources().getDrawable(picture, mContext.getApplicationContext().getTheme()));
} else
{
imageView.setImageDrawable(mContext.getResources().getDrawable(picture));
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}