Android 如何获取 ImageView 的源以更改它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10726519/
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
how to get the source of ImageView in order to change it?
提问by Red Sea
I know that changing the ImageView resource is not big deal just using myImageView.setImageResource(mynewImageDrawable)
我知道仅使用更改 ImageView 资源并不是什么大问题 myImageView.setImageResource(mynewImageDrawable)
but what I want to do is to check the current ImageSource before changing it.
但我想要做的是在更改之前检查当前的 ImageSource。
basically, I want to implement my own group radio buttons using imageViews. so every time any imageView is clicked, the oncliked event method will change the Image Resources of my group.
基本上,我想使用 imageViews 实现我自己的组单选按钮。所以每次单击任何 imageView 时,oncliked 事件方法都会更改我组的图像资源。
and sorry for my poor English.
并为我糟糕的英语感到抱歉。
regards, redsea
问候,红海
回答by Juan Cortés
There is no getDrawableId
function so you'll need to do something like set a tag for the ImageView
when you change its drawable. For instance, set the drawable id as a tag for the ImageView
so you could just get the drawable id from the tag.
没有getDrawableId
函数,所以你需要做一些事情,比如ImageView
在你改变它的可绘制时设置一个标签。例如,将 drawable id 设置为 的标签,ImageView
这样您就可以从标签中获取 drawable id。
How to do that?
怎么做?
I'd say 90% of the time, your views wont have any tag on them, so the easiest way is to assume your tag is the only tag:
我会说 90% 的时间,您的视图不会有任何标签,所以最简单的方法是假设您的标签是唯一的标签:
myImageView.setTag(R.drawable.currentImage); //When you change the drawable
int drawableId = (Integer)myImageView.getTag(); //When you fetch the drawable id
What if I already have a tag on my view
如果我的视图中已经有标签怎么办
Android views can host multiple tags at the same time, as long as they have a unique identifier. You'd need to create a unique idresource and add it as the first argument to the setTag
method call. Leaving the code like this:
Android 视图可以同时托管多个标签,只要它们具有唯一标识符即可。您需要创建一个唯一的 id资源并将其添加为setTag
方法调用的第一个参数。留下这样的代码:
myImageView.setTag(R.id.myTagId, R.drawable.currentImage); //Set
int drawableId = (Integer)myImageView.getTag(R.id.myTagId);
回答by mtmurdock
Are you saying that you intend to check the resource id to determine if a radio button is selected or not? While this could probably work, it isn't a very good design. Generally speaking you want to separate the model from the view (otherwise known as the Model-View-Controllerparadigm). To do this, you might have your radio button group maintain an index for the selected item, and have each of your items stored in a list. When an item is selected, you can determine its index, update the group's model, and then update the UI to reflect that change.
您是说您打算检查资源 ID 以确定是否选择了单选按钮?虽然这可能可行,但它不是一个很好的设计。一般来说,您希望将模型与视图分开(也称为模型-视图-控制器范例)。为此,您可以让单选按钮组维护所选项目的索引,并将每个项目存储在列表中。选择项目后,您可以确定其索引、更新组的模型,然后更新 UI 以反映该更改。
Its not that what you're proposing wontwork, it just isn't good design and leads to poor maintainability (such as if the resource names change).
这并不是说您提出的建议行不通,它只是不是好的设计并导致可维护性差(例如,如果资源名称更改)。
回答by Andrew Carl
Actualy after my try ,I find there is a way to get source of imageView without need of tag property.
实际上,经过我的尝试,我发现有一种方法可以在不需要标签属性的情况下获取 imageView 的源。
Integer src = attrs.getAttributeResourceValue(
"http://schemas.android.com/apk/res/android", "src", -1);
The first parameter is namespace of android
and src
is the property name.
This method return the image resource id if it found or it will return -1;
第一个参数是命名空间android
和src
为属性名称。如果找到,此方法返回图像资源ID,否则将返回-1;
回答by Mickey Tin
If you want get the src
drawable, use the following method of ImageView
:
http://developer.android.com/reference/android/widget/ImageView.html#getDrawable()
如果你想获得src
可绘制的,请使用以下方法ImageView
:http:
//developer.android.com/reference/android/widget/ImageView.html#getDrawable()
回答by VictorManuel LaFra
You can use this.
你可以用这个。
private Drawable colocaImgen(String nombreFile) {
Drawable res1 = null;
String uri1 = null;
try {
//First image
uri1 = "@drawable/" + nombreFile;
int imageResource1 = getResources().getIdentifier(uri1, null,getApplicationContext().getPackageName());
res1 = getResources().getDrawable(imageResource1);
} catch (Exception e) {
// TODO Auto-generated catch block
//int imageResource1 = context.getResources().getIdentifier(uri1, null, context.getApplicationContext().getPackageName());
res1 = getResources().getDrawable(R.drawable.globo2);// Default image
}
return res1;
}
Then
然后
((ImageView) findViewById(R.id.youNameFile)).setImageDrawable(colocaImgen("NameFile"));
回答by Abigail La'Fay
I had same problem and here is my working solutions:
我遇到了同样的问题,这是我的工作解决方案:
public void engLayoutExpand(View view) {
ImageView iv = (ImageView) view;
// One way:
Bitmap bm1 = ((BitmapDrawable) iv.getDrawable()).getBitmap();
Bitmap bm2 = ((BitmapDrawable) getResources().getDrawable(R.drawable.baseline_expand_less_black_36)).getBitmap();
if (bm1 == bm2) {
iv.setImageResource(R.drawable.baseline_expand_more_black_36);
}
// Other way
Drawable.ConstantState cs1 = iv.getDrawable().getConstantState();
Drawable.ConstantState cs2 = getResources().getDrawable(R.drawable.baseline_expand_less_black_36).getConstantState();
if ( cs1 == cs2) {
iv.setImageResource(R.drawable.baseline_expand_more_black_36);
}
}
回答by Sanskar
To get Image source, you can use Drawable.ConstantState
.
For example, in my problem I was implementing a button click to change the ImageView.
I created two Drawable.ConstantState
objects and compared them.
The code is given below:
要获取图像源,您可以使用Drawable.ConstantState
. 例如,在我的问题中,我正在实现一个按钮单击以更改 ImageView。我创建了两个Drawable.ConstantState
对象并比较了它们。代码如下:
ImageView myImg=(ImageView) findViewById(R.id.myImg);
Drawable.ConstantState imgID = myImg.getDrawable().getConstantState();
Drawable.ConstantState imgID2 = getDrawable(R.drawable.otherImage).getConstantState();
if(imgID!=imgID2)
{
// Block of Code
}
else // Other Block of Code