如何获取像getImageResource这样的图像的图像源的id

时间:2012-05-16 07:35:37

标签: android image

我正在处理Application,我想访问imageView使用的R.java中的图像id。 为考试

imageView i;
i=(ImageView)findViewbyid(imageview1);
i.setImageResource(R.drawble.human); 
.....

现在我想知道i在运行应用程序时使用了哪个id。

3 个答案:

答案 0 :(得分:0)

//当你在drawable中放置一个图像时,它会在R.java文件中生成一个Id

当您希望特定图像显示在ImageView

中时

你需要像这样声明

public static Integer[] images = new Integer[]{R.drawable.my_love,R.drawable.first,R.drawable.second,R.drawable.third};

然后像这样设置图像资源

ImageView i = new ImageView(this.context);
i.setImageResource(this.images[1]);

//您已声明为

imageView i=(ImageView)findViewbyid(imageview1);

//这个imageview1是什么?

而不是你需要将你的xml声明ImageView id作为

 imageView i=(ImageView)findViewbyid(R.id.imageview);

答案 1 :(得分:0)

如果您正在寻找获取ImageView图像背景的方法,这将对您有所帮助。

   ImageView imageView=(ImageView)findViewById(R.id.image);
       imageView.setBackgroundResource(R.drawable.ic_launcher);
      imageView.setDrawingCacheEnabled(true);
      Drawable d=imageView.getBackground();

答案 2 :(得分:0)

这将为您提供可绘制图像的ID

String mDrawableName =Integer.toString( R.drawable.human);              
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());