我有一个视图,它有一个方法setBackgroundImage(BitmapDrawable)
,并且我imgbg
在 res 的 drawable 文件夹中有一个图像。
如何将此图像设置为背景图像?
view.setBackgroundImage(R.drawable.imgbg);
不能像R.drawable.imgbg
整数一样工作,但方法需要可绘制。
我知道这很愚蠢,但即使经过密集的谷歌搜索,我也无法解决。
任何帮助表示赞赏。
编辑
此代码段不属于 Activity 类,所以我没有context
对象或getResources()
或getApplicationContext()
...
编辑2(解决方案):
public class PieChartDemo01View extends View
{
public PieChartDemo01View(Context context)
{
super(context);
Drawable drawable = super.getResources().getDrawable(
R.drawable.bg2);
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
}
}