如何获取图像资源名称

时间:2011-06-23 05:18:11

标签: android

如何获取动态设置的imageview资源名称?

这是图像适配器代码:

  public class ImageAdapter extends BaseAdapter {
  private Context mContext;

  public ImageAdapter(Context c) {
    mContext = c;
  }

  public int getCount() {
    return mThumbIds.length;
  }

  public Object getItem(int position) {
    return null;
  }

  public long getItemId(int position) {
    return 0;
  }

  // create a new ImageView for each item referenced by the Adapter
  public View getView(int position, View convertView, ViewGroup parent) {
    View v;

    if (convertView == null) { // if it's not recycled, initialize some
      // attributes

      LayoutInflater li = getLayoutInflater();
      v = li.inflate(R.layout.gridxml, null);
        imageView = (ImageView)v.findViewById(R.id.icon_image);

      imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
      //imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
      imageView.setPadding(5, 5, 5, 5);
    } else {
      imageView = (ImageView) convertView;
  }

    imageView.setImageResource(mThumbIds[position]);
        imageView.setTag(mThumbIds[position]);

    System.out.println(mThumbIds[0]);
          System.out.println(mThumbIds[1]);
          System.out.println(mThumbIds[2]);
          System.out.println(mThumbIds[3]);
          System.out.println(mThumbIds[4]);
    return imageView;
  }

  // references to our images
  private Integer[] mThumbIds = { R.drawable.directory_xml,
      R.drawable.news_xml, R.drawable.calendar_xml,
      R.drawable.facilities_xml, R.drawable.employee_handbook_xml,R.drawable.settings_xml };
}
}

4 个答案:

答案 0 :(得分:6)

您可以使用setTag()getTag()设置或获取图像资源名称以及imgae

当您动态设置图像时,可以添加以下行以使用图像

设置图像资源名称
imageView.setTag("image resource name");

如果要检索图像资源名称,可以使用

String imageName = (String) imageView.getTag();

答案 1 :(得分:2)

您可以使用此代码。这段代码对我来说很好。

String resName = getResourceNameFromClassByID(R.drawable.class, R.drawable.imagename);

,方法是

public String getResourceNameFromClassByID(Class<?> aClass, int resourceID) 
                    throws IllegalArgumentException{
    /* Get all Fields from the class passed. */
    Field[] drawableFields = aClass.getFields();

    /* Loop through all Fields. */
    for(Field f : drawableFields){
        try {
            /* All fields within the subclasses of R 
             * are Integers, so we need no type-check here. */

            /* Compare to the resourceID we are searching. */
            if (resourceID == f.getInt(null))
                return f.getName(); // Return the name.
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /* Throw Exception if nothing was found*/
    throw new IllegalArgumentException();
}

有关详细信息,请参阅http://www.anddev.org/viewtopic.php?t=506

答案 2 :(得分:1)

试试这个

String name = getResources().getResourceEntryName(image[position]);

答案 3 :(得分:-2)

ImageView imgvw = (ImageView)findViewById(R.id.imageview1);

imgvw.setTag("name");

String strImgName = (String) imgvw.getTag();