有没有办法获取Drawable资源的ID?例如,我正在使用一个ImageView,可能最初使用icon.png作为它的图像,但后来可能会将图像更改为icon2.png。我想通过代码找出我的ImageView正在使用哪个资源的图像。有没有办法?
有没有办法获取Drawable资源的ID?例如,我正在使用一个ImageView,可能最初使用icon.png作为它的图像,但后来可能会将图像更改为icon2.png。我想通过代码找出我的ImageView正在使用哪个资源的图像。有没有办法?
当您在程序中点击ImageView时,这是找出R.drawable.img1
值的最佳方法。该方法类似于主程序中的以下操作。首先像这样将图像值保存在标签中。
public...activity
{
//-----this resource name is retrieved through the tag value of the drawable of (touched) ImageView //image ontouchlistener event...
ImageView imgview1.setTag("img1"); //as of R.drawable.img1
ImageView imgview2.setTag("img2"); //as of R.drawable.img2
onTouchListnener event... on imageView
{
Object tag = imageView.getTag();
int id = getResources().getIdentifier( tag, "drawable", this.getPackageName() );
switch(id)
{
case R.drawable.img1:
//do someoperation of ur choice
break;
case R.drawable.img2:
//do someoperation of ur choice
break:
}//end of switch
}//end of touch listener event
}//end of main activity
"PIR FAHIM SHAH/kpk uet mardan campus"
ImageView
上的当前图像,以便将其更改为其他图像?如果是这样,我建议您使用代码而不是XML完成所有操作。即在初始化期间使用setImageResource()
设置初始图像,并在代码的某个位置跟踪使用的resource id
。例如,您可以有一个ImageViews
数组和一个相应的包含每个ImageView
的resource id
的int
数组。然后,每当您想要更改图像时,请循环遍历该数组并查看id是什么。public class CustomImageView extends ImageView {
private int resID;
public CustomImageView(Context context) {
super(context);
}
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setImageResource(int resId) {
this.resID = resId;
super.setImageResource(resId);
}
public int getResourceId() {
return resID;
}
}
我知道这个问题已经很老了,但也许有人会觉得它有用。
我有一个带有Drawable的TextView列表,并希望为它们所有设置点击监听器,而无需在布局更改时更改代码。
因此,我将所有的drawable放入哈希映射中,以便稍后获取它们的ID。
main_layout.xml
<LinearLayout android:id="@+id/list" >
<TextView android:drawableLeft="@drawable/d1" />
<TextView android:drawableLeft="@drawable/d2" />
<TextView android:drawableLeft="@drawable/d3" />
<TextView android:drawableLeft="@drawable/d4" />
<!-- ... -->
</LinearLayout>
MyActivity.java
import java.lang.reflect.Field;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Drawable.ConstantState;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyActivity extends Activity {
private final HashMap<ConstantState, Integer> drawables = new HashMap<ConstantState, Integer>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
for (int id : getAllResourceIDs(R.drawable.class)) {
Drawable drawable = getResources().getDrawable(id);
drawables.put(drawable.getConstantState(), id);
}
LinearLayout list = (LinearLayout)findViewById(R.id.list);
for (int i = 0; i < list.getChildCount(); i++) {
TextView textView = (TextView)list.getChildAt(i);
setListener(textView);
}
}
private void setListener(TextView textView) {
// Returns drawables for the left, top, right, and bottom borders.
Drawable[] compoundDrawables = textView.getCompoundDrawables();
Drawable left = compoundDrawables[0];
final int id = drawables.get(left.getConstantState());
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent broadcast = new Intent();
broadcast.setAction("ACTION_NAME");
broadcast.putExtra("ACTION_VALUE", id);
sendBroadcast(broadcast);
}
});
}
/**
* Retrieve all IDs of the Resource-Classes
* (like <code>R.drawable.class</code>) you pass to this function.
* @param aClass : Class from R.X_X_X, like: <br>
* <ul>
* <li><code>R.drawable.class</code></li>
* <li><code>R.string.class</code></li>
* <li><code>R.array.class</code></li>
* <li>and the rest...</li>
* </ul>
* @return array of all IDs of the R.xyz.class passed to this function.
* @throws IllegalArgumentException on bad class passed.
* <br><br>
* <b>Example-Call:</b><br>
* <code>int[] allDrawableIDs = getAllResourceIDs(R.drawable.class);</code><br>
* or<br>
* <code>int[] allStringIDs = getAllResourceIDs(R.string.class);</code>
*/
private int[] getAllResourceIDs(Class<?> aClass) throws IllegalArgumentException {
/* Get all Fields from the class passed. */
Field[] IDFields = aClass.getFields();
/* int-Array capable of storing all ids. */
int[] IDs = new int[IDFields.length];
try {
/* Loop through all Fields and store id to array. */
for(int i = 0; i < IDFields.length; i++){
/* All fields within the subclasses of R
* are Integers, so we need no type-check here. */
// pass 'null' because class is static
IDs[i] = IDFields[i].getInt(null);
}
} catch (Exception e) {
/* Exception will only occur on bad class submitted. */
throw new IllegalArgumentException();
}
return IDs;
}
}
我从这里使用了getAllResourceIDs方法。
这需要几个步骤:
创建一个integer-array的xml文件来保存drawable的名称(例如:"@drawable/icon1" … "@drawable/iconN")
使用上面的getIdentifier方法获取“array”的ID
有了drawable列表的ID,使用getStringArray方法将为您提供在步骤1中指定的drawable名称的数组。
然后再次使用getIdentifier中的任何一个drawable名称来获取drawable ID。 这里使用“drawable”而不是“array”类型。
使用此ID为您的视图设置图像。
希望这能有所帮助。
public class PhoneImageView extends ImageView {
private static final String BACKGROUND="background";
private int imageNormalResourceID;
public PhoneImageView(Context context) {
super(context);
}
public PhoneImageView(Context context, AttributeSet attrs) {
super(context, attrs);
for (int i = 0; i <attrs.getAttributeCount() ; i++) {
if(attrs.getAttributeName(i).equals(BACKGROUND)){
imageNormalResourceID =attrs.getAttributeResourceValue(i,-1);
}
}
}
public PhoneImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
这种方法适用于想要存储初始值的人。Bojan Kseneman提供的解决方案(+1票)是在视图更改时保持对资源ID的引用。
您可以通过以下代码,根据名称获取图像的ID。
int drawableImageId = getResources().getIdentifier(imageName,"drawable", getPackageName());