view 대상 을 Bitmap 으로 변환

5721 단어 bitmap
직접 테스트 한 결과, 확실히 실행 할 수 있다.
등급: http://blog.csdn.net/hexingzhi/article/details/7598567 
 、Bitmap Drawable 





Bitmap bm=xxx; //xxx         



BitmapDrawable bd=new BitmapDrawable(bm); 

  BtimapDrawable Drawable   ,      bd    。 



 、 Drawable Bitmap 



  Bitmap   ,   Drawable    Android SK          ,         jpg png   。 

Drawable d=xxx; //xxx         drawable 



BitmapDrawable bd = (BitmapDrawable) d; 



Bitmap bm = bd.getBitmap(); 

  bm       Bitmap   。 







//       Bitmap 

public static Bitmap getBitmapFromResources(Activity act, int resId) { 

Resources res = act.getResources(); 

return BitmapFactory.decodeResource(res, resId); 

} 



// byte[] → Bitmap 

public static Bitmap convertBytes2Bimap(byte[] b) { 

if (b.length == 0) { 

return null; 

} 

return BitmapFactory.decodeByteArray(b, 0, b.length); 

} 



// Bitmap → byte[] 

public static byte[] convertBitmap2Bytes(Bitmap bm) { 

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

bm.compress(Bitmap.CompressFormat.PNG, 100, baos); 

return baos.toByteArray(); 

} 



// 1)Drawable → Bitmap 

public static Bitmap convertDrawable2BitmapByCanvas(Drawable drawable) { 

Bitmap bitmap = Bitmap 

.createBitmap( 

drawable.getIntrinsicWidth(), 

drawable.getIntrinsicHeight(), 

drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 

: Bitmap.Config.RGB_565); 

Canvas canvas = new Canvas(bitmap); 

// canvas.setBitmap(bitmap); 

drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), 

drawable.getIntrinsicHeight()); 

drawable.draw(canvas); 

return bitmap; 

} 



// 2)Drawable → Bitmap 

public static Bitmap convertDrawable2BitmapSimple(Drawable drawable){ 

BitmapDrawable bd = (BitmapDrawable) drawable; 

return bd.getBitmap(); 

} 



// Bitmap → Drawable 

public static Drawable convertBitmap2Drawable(Bitmap bitmap) { 

BitmapDrawable bd = new BitmapDrawable(bitmap); 

//   BtimapDrawable Drawable   ,      bd    。 

return bd; 

}

 
        
private
 Bitmap getViewBitmap(View view) {
view.clearFocus();        view.setPressed(false);        boolean willNotCache = view.willNotCacheDrawing();        view.setWillNotCacheDrawing(false);        int color = view.getDrawingCacheBackgroundColor();        view.setDrawingCacheBackgroundColor(0);        if (color != 0) {            view.destroyDrawingCache();        }        view.buildDrawingCache();        Bitmap cacheBitmap = view.getDrawingCache();        if (cacheBitmap == null) {            return null;        }        Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);        view.destroyDrawingCache();        view.setWillNotCacheDrawing(willNotCache);        view.setDrawingCacheBackgroundColor(color);        return bitmap;    }

좋은 웹페이지 즐겨찾기