获取ImageButton的背景作为位图

时间:2015-08-24 05:51:55

标签: android bitmap imagebutton android-imagebutton

我有canvasImageButton。我正在尝试获取ImageButton的背景图像并将其设置为我画布的背景图像。我的画布使用drawBitmap()函数来设置它的背景,因此它需要一个位图。但是,getBackground()的{​​{1}}函数会返回drawable。

如何获取ImageButton的背景并将其设置为画布的背景?

4 个答案:

答案 0 :(得分:1)

canvas.setBackground(drawableFromImageButton.getConstantState().newDrawable());

当canvas是view时,setBackground方法适用。

答案 1 :(得分:0)

您可以按照以下方式从Bitmap获取Drawable

Drawable drawable = imageButton.getBackground();
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();

答案 2 :(得分:0)

试试这样。它可能会有所帮助。

Drawable drawable = imageButton.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();

之后

canvas.setBitmap(bitmap);

答案 3 :(得分:0)

如果你制作自定义画布,你可以直接设置drawable来设置背景。

按照这个 -

customCanvas.setBackground(imageview.getBackground());

文档说像tbis -

void  setBackground(Drawable background) 

将背景设置为给定的Drawable,或删除背景。

绘图仅用于绘制画布的tope。

我希望这会对你有所帮助。