如何从ImageButton获取图像?

时间:2014-02-21 13:25:21

标签: android imagebutton

我不确定代码,但我尝试这样做:

ImageButton stuff = (ImageButton) createView.findViewById(R.id.stuff);
int stuff2 = stuff.getId();
champs.setImageResource(stuff2);

但是那不起作用,Imagebutton由于某种原因缩小了,没有图像,只有一个灰色的矩形。

5 个答案:

答案 0 :(得分:9)

试试这个

Bitmap bitmap = ((BitmapDrawable)stuff.getDrawable()).getBitmap();

答案 1 :(得分:1)

stuff.getId();

这将返回View的id而不是与之关联的图像资源。因此,您将无法获得与此相关的资源,这就是您没有看到Image

的原因

将有效的drawable设置为setImageResource某些内容,例如R.drawable.drawable_id

答案 2 :(得分:0)

您需要设置可绘制资源的ID,但是您要将ID的{​​{1}}设置为图像资源。

此处ImageButton返回stuff.getId()

设置有效的可绘制资源,如

R.id.stuff

答案 3 :(得分:0)

试试这个

ImageButton stuff = (ImageButton) createView.findViewById(R.id.stuff);
Drawable d = stuff .getBackground();
champs.setBackgroundDrawable(d);

答案 4 :(得分:0)

这将有助于获取可绘制对象并设置可绘制对象。您还可以获取图像绘制的Bitmap对象。这将设置与图像按钮关联的精确可绘制

ImageButton mButton = new ImageButton(this);
Drawable drawable = mButton.getBackground();
champs.setImageDrawable(drawable)
相关问题