Drawable转换为Bitmap
/** * Drawable转换为Bitmap * */ public static Bitmap DrawableToBitmap(Drawable drawable){ if(drawable==null) return null; if(drawable instanceof BitmapDrawable) return ((BitmapDrawable)drawable).getBitmap(); Bitmap bitmap=null; //如果是ColorDrawable随便给一个宽高 if(drawable instanceof ColorDrawable) bitmap=Bitmap.createBitmap(2,2,Bitmap.Config.ARGB_8888); else bitmap=Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(),Bitmap.Config.ARGB_8888); Canvas canvas=new Canvas(bitmap); //设置绘制的矩形区域 drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap; }
/** * Drawable转换为Bitmap * */ public static Bitmap DrawableToBitmap(Context mContext,int ResId){ Bitmap bitmap= BitmapFactory.decodeResource(mContext.getResources(),ResId); return bitmap;}