如何将relativelayout.setBackgroundDrawable()与位图一起使用?

e-n*_*ure 9 android android-layout

我有一个RelativeLayout对象,并希望动态更改背景图像与动态创建的Bitmap对象(它动态更改其颜色).

我看到当我想要更新RelativeLayout对象的背景图像时,我只能选择setBackgroundDrawable()需要Drawable对象作为参数的对象.

我的问题是,如何将动态创建的Bitmap对象转换为Drawable对象?

Sit*_*ten 20

BitmapDrawable(obj) 将Bitmap对象转换为可绘制对象.

尝试:

RelativeLayout relative = (RelativeLayout) findViewById(R.id.relative1);
Drawable dr = new BitmapDrawable(bit);
(view).setBackgroundDrawable(drawable);
Run Code Online (Sandbox Code Playgroud)

我希望这能帮到您.


Nik*_*tel 6

你可以这样做

Drawable drawable = new BitmapDrawable(bitmap);
RelativeLayout r;
r = (RelativeLayout) findViewById(R.id.relativelayout1);
ll.setBackgroundDrawable(drawable);
Run Code Online (Sandbox Code Playgroud)