Android中Bitmap、Drawable、byte[]转换(hdp)

来源:互联网 发布:作图软件哪个好 编辑:程序博客网 时间:2024/05/17 21:58

1.Drawable—>Bitmap

Resources res=getResources();Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.sample_0);

2.Bitmap---->Drawable

Drawable drawable =new BitmapDrawable(bmp);


1.Drawable—>Bitmap

public static Bitmap drawableToBitmap(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的宽高 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap;}

2、从资源中获取Bitmap

Resources res=getResources();Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);

3、Bitmap → byte[]

private byte[] Bitmap2Bytes(Bitmap bm){    ByteArrayOutputStream baos = new ByteArrayOutputStream();    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);    return baos.toByteArray();   }

4、 byte[] → Bitmap

 

  private Bitmap Bytes2Bimap(byte[] b){                    if(b.length!=0){                            return BitmapFactory.decodeByteArray(b, 0, b.length);                    }                    else {                            return null;                    }          }
hdp.li.fans.bitmapcache;

构造函数:protected ImageWorker(Context context) {mResources = context.getResources();}       /xlh_addpublic void loadImage(Object data, ImageView imageView) {if (data == null) {Log.v("xulongheng*moredetails*bitmap", "moredetails*");Bitmap bitmap = BitmapFactory.decodeResource( mResources, R.drawable.moredetails);imageView.setImageBitmap(createSaledBitmap(bitmap));return;}if(data.toString().indexOf("://")<0){imageView.setImageResource(R.drawable.moren);return;}Bitmap bitmap = null;if (mImageCache != null) {bitmap = mImageCache.getBitmapFromMemCache(String.valueOf(data));}       if (bitmap != null && !bitmap.isRecycled()) {// Bitmap found in memory cacheimageView.setImageBitmap(createSaledBitmap(bitmap));if(mImageLoadCompletedListener!=null){mImageLoadCompletedListener.setCustomImageView(imageView, bitmap);}} else if (cancelPotentialWork(data, imageView)) {final BitmapWorkerTask task = new BitmapWorkerTask(imageView);/*if (mLoadingBitmap != null && !mLoadingBitmap.isRecycled()) {*/final AsyncDrawable asyncDrawable;if (mImageLoadCompletedListener != null) {asyncDrawable = new AsyncDrawable(mResources,mImageLoadCompletedListener.setCustomBitmap(createSaledBitmap(mLoadingBitmap)),task);} else {asyncDrawable = new AsyncDrawable(mResources,createSaledBitmap(mLoadingBitmap), task);//xlh_?}imageView.setImageDrawable(asyncDrawable);//}// NOTE: This uses a custom version of AsyncTask that has been// pulled from the// framework and slightly modified. Refer to the docs at the top of// the class// for more info on what was changed.task.executeOnExecutor(AsyncTask.DUAL_THREAD_EXECUTOR, data);}}


原创粉丝点击