1,在android的Activity中我们可以直接通过如下代码直接获得图片资源:
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
2、在android自定义类中我们就只能通过Context定义一个context类,在Activity中通过this传到过去,按在Activity中的使用方法得到图片资源。
如在Example类中我们这样定义
public static Context context = null;
在Activity中直接传递过去
Example.context = this;
当然这种方法不好,建议在Example的构造函数中将Activity传递过去。
3、在自定义类中通过context就能得到图片资源,代码如下。
Drawable drawable = context.getResources().getDrawable(R.drawable.icon)
4、要获得位图资源还得转换一次才行,代码如下
Bitmap bitmap = ((BitmapDrawable) context.getResources().getDrawable(R.drawable.icon)).getBitmap();