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

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_add
	public 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 cache
			imageView.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);
		}
	}


你可能感兴趣的:(Android中Bitmap、Drawable、byte[]转换(hdp))