2014-02-21 18 views
3

我不知道有关的代码,但我试着这样做:如何从ImageButton获取图像?

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

但是,这并没有工作,ImageButton的收缩是由于某种原因,没有图像,只是一个灰色的矩形。

+0

也许[这](http://stackoverflow.com/questions/3988753/copy-bitmap-contents-of-one-imageview-to-anoher)帮助。 –

回答

1
stuff.getId(); 

这将返回视图的id而不是与其关联的图像资源。所以你不会有与此相关的资源,这就是为什么你没有看到Image

设置有效的绘制到setImageResource有些东西一样R.drawable.drawable_id

0

你需要设置绘制资源的ID,但你设置ImageButtonID作为图像资源。

这里stuff.getId()返回R.id.stuff

设置像

ImageButton stuff = (ImageButton) createView.findViewById(R.id.stuff); 
stuff.setImageResource(R.drawable.id_of_image); 
7

有效绘制资源试试这个

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

为什么我得到这个错误:'android.graphics.drawable.TransitionDrawable无法转换为android.graphics.drawable.BitmapDrawable' – zihadrizkyef

0

试试这个

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

这将有助于获得绘制对象并设置绘制object.You可以也可以获取图像绘制的位图对象这将设置与图像相关的确切绘制吨

ImageButton mButton = new ImageButton(this); 
Drawable drawable = mButton.getBackground(); 
champs.setImageDrawable(drawable)