Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中常用的bitmap處理方法

Android中常用的bitmap處理方法

編輯:關於Android編程

收集了很多bitmap相關的處理方法,幾乎全部應用在項目中,所以特記錄下!

 

package com.tmacsky.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.MeasureSpec;

public class ImageUtils {

	//--->bitmap相關
	//參考網站http://www.cnblogs.com/fighter/archive/2012/02/20/android-bitmap-drawable.html
	// 見博客:http://blog.sina.com.cn/s/blog_afb547c60101j7qn.html
	/**
	 * View轉成bitmap
	 * @param view
	 * @return
	 */
	public static Bitmap convertViewToBitmap(View view) {
		view.setDrawingCacheEnabled(true);
		view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
				MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
		view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
		view.buildDrawingCache();
		return view.getDrawingCache();
	}
	/**
	 * 縮放Drawable
	 * @param drawable
	 * @param w  縮放後需要的寬度
	 * @param h  縮放後需要的高度
	 * @return
	 */
	public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        // drawable轉換成bitmap
        Bitmap oldbmp = drawableToBitmap(drawable);
        // 創建操作圖片用的Matrix對象
        Matrix matrix = new Matrix();
        // 計算縮放比例
        float sx = ((float) w / width);
        float sy = ((float) h / height);
        // 設置縮放比例
        matrix.postScale(sx, sy);
        // 建立新的bitmap,其內容是對原bitmap的縮放後的圖
        Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
                matrix, true);
        return new BitmapDrawable(newbmp);
    }
	
	/**
	 * 縮放bitmap
	 * @param oldBitmap 輸入bitmap
	 * @param newWidth 
	 * @param newHeight
	 * @return
	 */
	public static Bitmap zoomBitmap(Bitmap oldBitmap, int newWidth, int newHeight) {
		// 獲得圖片的寬高
		int width = oldBitmap.getWidth();
		int height = oldBitmap.getHeight();
		// 計算縮放比例
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		// 取得想要縮放的matrix參數
		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		// 得到新的圖片
		Bitmap newbm = Bitmap.createBitmap(oldBitmap, 0, 0, width, height, matrix,
				true);
		return newbm;
	}
	/**
	 * 縮放網絡圖片 依賴於zoomBitmap
	 * @param img
	 * @param newWidth
	 * @param newHeight
	 * @return
	 */
	public static Bitmap zoomImg(String img, int newWidth, int newHeight) {
		// 圖片源
		Bitmap bm = BitmapFactory.decodeFile(img);
		if (null != bm) {
			return zoomBitmap(bm, newWidth, newHeight);
		}
		return null;
	}
	/**
	 * 縮放網絡圖片 依賴於zoomBitmap
	 * @param context
	 * @param img
	 * @param newWidth
	 * @param newHeight
	 * @return
	 */
	public static Bitmap zoomImg(Context context, String img, int newWidth,
			int newHeight) {
		// 圖片源
		try {
			Bitmap bm = BitmapFactory.decodeStream(context.getAssets()
					.open(img));
			if (null != bm) {
				return zoomBitmap(bm, newWidth, newHeight);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 判斷bitmap是否存在
	 * @param bitmap
	 * @return
	 */
	public static boolean bitmapAvailable(Bitmap bitmap) {
		return bitmap != null && bitmap.getWidth() > 0 && bitmap.getHeight() > 0;
	}
	/**
	 * drawable 轉成bitmap
	 * @param drawable
	 * @return
	 */
	public static Bitmap drawableToBitmap(Drawable drawable) {
        // 取 drawable 的長寬
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();
        // 取 drawable 的顏色格式
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565;
        // 建立對應 bitmap
        Bitmap bitmap = Bitmap.createBitmap(w, h, config);
        // 建立對應 bitmap 的畫布
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        // 把 drawable 內容畫到畫布中
        drawable.draw(canvas);
        return bitmap;
    }
	/**
	 * Bitmap轉換成Drawable
	 * @param context
	 * @param bitmap
	 * @return
	 */
	public static Drawable bitmapToDrawable(Context context,Bitmap bitmap){
		//因為BtimapDrawable是Drawable的子類,最終直接使用bd對象即可。
		BitmapDrawable bd= new BitmapDrawable(context.getResources(), bitmap);
		return bd;
	}
	
	/**
	 * 從資源中獲取Bitmap
	 * @param context
	 * @param req  R.drawable.icon(eg.)
	 * @return
	 */
	public Bitmap getBitmapFromResources(Context context,int req){
		  Resources res = context.getResources();
		  Bitmap bmp = BitmapFactory.decodeResource(res, req);
		  return bmp;
	}
	
	/**
	 * Byte[] -> Bitmap的轉換
	 */
	public Bitmap Bytes2Bimap(byte[] b) {
        if (b.length != 0) {
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        } else {
            return null;
        }
    }
	/**
	 * Bitmap->Byte[]的轉換
	 * @param bm
	 * @return
	 */
	 public byte[] Bitmap2Bytes(Bitmap bm) {
		 ByteArrayOutputStream baos = new ByteArrayOutputStream();
		 bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
		 return baos.toByteArray();
    }
	/**
	 * 獲取圓角圖片
	 * @param bitmap
	 * @param roundPx 圓角的弧度
	 * @return
	 */
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, w, h);
        final RectF rectF = new RectF(rect);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }
}


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved