本文介绍了如何获取图像资源名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何得到已被动态地设置一个ImageView的资源名称?
这是图像适配器code:
公共类ImageAdapter延伸BaseAdapter {
私人语境mContext; 公共ImageAdapter(上下文C){
mContext = C;
} 公众诠释的getCount(){
返回mThumbIds.length;
} 公共对象的getItem(INT位置){
返回null;
} 众长getItemId(INT位置){
返回0;
} //创建由适配器引用的每个项目的新的ImageView
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
视图V; 如果(convertView == NULL){//如果它没有回收,初始化一些
//属性 LayoutInflater李= getLayoutInflater();
V = li.inflate(R.layout.gridxml,NULL);
ImageView的=(ImageView的)v.findViewById(R.id.icon_image); imageView.setLayoutParams(新GridView.LayoutParams(85,85));
//imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5,5,5,5);
}其他{
ImageView的=(ImageView的)convertView;
} imageView.setImageResource(mThumbIds [位置]);
imageView.setTag(mThumbIds [位置]); 的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]);
返回ImageView的;
} //我们的图像的引用
私人整数[] = 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};
}
}
解决方案
您可以使用 setTag()
和 getTag()
设置或imgae相处图像资源名称
当你设置图像动态,您可以添加以下行来设置imageresource名称与图像
imageView.setTag(图像资源名称);
如果你想获取图像资源名称可以使用
字符串imageName =(字符串)imageView.getTag();
How do I get an imageview resource name which has been set dynamically?
This is the image Adapter code:
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 };
}
}
解决方案
you can use setTag()
and getTag()
to set or get image resource name along with imgae
when you are setting image dynamically you can add the following line to set imageresource name with the image
imageView.setTag("image resource name");
if you want to retrieve image resource name you can use
String imageName = (String) imageView.getTag();
这篇关于如何获取图像资源名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!