android 處理圖片之--bitmap處理

-二、從資源中得到bitmap

Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
或者
Bitmap bmp = ((BitmapDrawable)getResources().getDrawable(R.drawable.show)).getBitmap();

    /**
     * 以最省內存的方式讀取本地資源的圖片
     *
     * @param context
     * @param resId
     * @return
     */
    public Bitmap readBitMap(int resId) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        // 獲取資源圖片
        InputStream is = getResources().openRawResource(resId);
        return BitmapFactory.decodeStream(is, null, opt);
    }

-一、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.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
    drawable.draw(canvas);  
    return bitmap;  
}  



0、讀取一個bitmap
    /**
     * 節省內存
     *
     * @Description:
     * @param filePath
     * @param outWidth
     * @param outHeight
     * @return
     * @see:
     * @since:
     * @author: zhuanggy
     * @date:2013-3-12
     */
    public static Bitmap readBitmapAutoSize(String filePath, int outWidth, int outHeight) {
        // outWidth和outHeight是目標圖片的最大寬度和高度,用做限制
        FileInputStream fs = null;
        BufferedInputStream bs = null;
        try {
            fs = new FileInputStream(filePath);
            bs = new BufferedInputStream(fs);
            BitmapFactory.Options options = setBitmapOption(filePath, outWidth, outHeight);
            return BitmapFactory.decodeStream(bs, null, options);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bs.close();
                fs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private static BitmapFactory.Options setBitmapOption(String file, int width, int height) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inJustDecodeBounds = true;
        // 設置只是解碼圖片的邊距,此操做目的是度量圖片的實際寬度和高度
        BitmapFactory.decodeFile(file, opt);

        int outWidth = opt.outWidth; // 得到圖片的實際高和寬
        int outHeight = opt.outHeight;
        opt.inDither = false;
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        // 設置加載圖片的顏色數爲16bit,默認是RGB_8888,表示24bit顏色和透明通道,但通常用不上
        opt.inSampleSize = 1;
        // 設置縮放比,1表示原比例,2表示原來的四分之一....
        // 計算縮放比
        if (outWidth != 0 && outHeight != 0 && width != 0 && height != 0) {
            int sampleSize = (outWidth / width + outHeight / height) / 2;
            opt.inSampleSize = sampleSize;
        }

        opt.inJustDecodeBounds = false;// 最後把標誌復原
        return opt;
    }



一、透明度處理

/**
* 圖片透明度處理
*
* @param sourceImg
* 原始圖片
* @param number
* 透明度
* @return
*/
public static Bitmap setAlpha(Bitmap sourceImg, int number) {
int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];
sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0,sourceImg.getWidth(), sourceImg.getHeight());// 得到圖片的ARGB值
number = number * 255 / 100;
for (int i = 0; i < argb.length; i++) {
argb[i] = (number << 24) | (argb & 0×00FFFFFF);// [/i][i]修改最高2[/i][i]位的值
}
sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg.getHeight(), Config.ARGB_8888);
return sourceImg;
}


二、得到圓角bitmap

    /**
     * 得到圓角圖片
     *
     * @Description:
     * @param bitmap
     * @param roundPx
     * @return
     * @see:
     * @since:
     * @author: zhuanggy
     * @date:2013-3-14
     */
    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
        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, 10, 10, paint);// 圓角平滑度爲10
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }


三、截取 Bitmap 的部分區域

mBitmap = Bitmap.createBitmap(bmp, 100, 100, 120, 120);


四、縮放一個 Bitmap
能夠用 Bitmap.createScaledBitmap() 方 法根據給定的 Bitmap 建立 一個新的,縮放後的 Bitmap 。


Bitmap mBitmap = Bitmap.createScaledBitmap(bmp, mScreenWidth, mScreenHeight, true);  

其中 mScreenWidth 和 mScreenHeight 是屏幕的寬度和高度,這裏就將 bmp 拉伸到整個屏幕。
    每次 createBitmap ,都會分配新的內存,帶來資源的 消耗,因此用 Bitmap 的 createBitmap 雖然簡單方便,可是不是最優方 法。介紹一個比較好點的方法,不用建立新的 Bitmap ,用 Canvas 在畫的時候直接縮放或者剪切


canvas.drawBitmap(mBitmap, null, new Rect(0, 0, 200, 200), null);  

這裏的 Rect 對象表示一個矩形區域,從 (0,0) 到 (200,200) 之間的矩形區域。這段代碼將把 mBitmap 縮放並繪製到屏幕上的(0,0) 到 (200,200) 之間的區域。這個方法還有第二個參數我給的是 null ,其實這個參數也是個 Rect 對象,表示源 Rect 。把圖片的某個區域拿出來畫到屏幕的指定區域,


canvas.drawBitmap(mBitmap, new Rect(100, 100, 300, 300), new Rect(100, 100, 200, 200), null);
這裏將 mBitmap 的 (100,100) 到 (300,300) 區域拿出來,自動縮放並畫到屏幕的 (100,100) 到 (200,200) 區域。



五、圖片平均分割方法,將大圖平均分割爲N行N列,方便用戶使用
/***
* 圖片分割
*
* @param g
* :畫布
* @param paint
* :畫筆
* @param imgBit
* :圖片
* @param x
* :X軸起點座標
* @param y
* :Y軸起點座標
* @param w
* :單一圖片的寬度
* @param h
* :單一圖片的高度
* @param line
* :第幾列
* @param row
* :第幾行
*/

public final void cuteImage(Canvas g, Paint paint, Bitmap imgBit, int x,
int y, int w, int h, int line, int row) {
g.clipRect(x, y, x + w, h + y);
g.drawBitmap(imgBit, x – line * w, y – row * h, paint);
g.restore();
}

六、 圖片縮放,對當前圖片進行縮放處理


public Bitmap zoomImage(Bitmap bgimage, int newWidth, int newHeight) {

// 獲取這個圖片的寬和高

int width = bgimage.getWidth();
int height = bgimage.getHeight();

// 建立操做圖片用的matrix對象
Matrix matrix = new Matrix();

// 計算縮放率,新尺寸除原始尺寸
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;

// 縮放圖片動做
matrix.postScale(scaleWidth, scaleHeight);
Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, width, height,
matrix, true);
return bitmap;

}


七、繪製帶有邊框的文字,通常在遊戲中起文字的美化做用
/***
* 繪製帶有邊框的文字
*
* @param strMsg
* :繪製內容
* @param g
* :畫布
* @param paint
* :畫筆
* @param setx
* ::X軸起始座標
* @param sety
* :Y軸的起始座標
* @param fg
* :前景色
* @param bg
* :背景色
*/


public void drawText(String strMsg, Canvas g, Paint paint, int setx,
int sety, int fg, int bg) {
paint.setColor(bg);
g.drawText(strMsg, setx + 1, sety, paint);
g.drawText(strMsg, setx, sety – 1, paint);
g.drawText(strMsg, setx, sety + 1, paint);
g.drawText(strMsg, setx – 1, sety, paint);
paint.setColor(fg);
g.drawText(strMsg, setx, sety, paint);
g.restore();

}


八、圖片翻轉

Resources res = this.getContext().getResources();
img = BitmapFactory.decodeResource(res, R.drawable.slogo);
Matrix matrix = new Matrix();
matrix.postRotate(90);
 /*翻轉90度*/
int width = img.getWidth();
int height = img.getHeight();
r_img = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true);


九、帶倒影的效果

//得到帶倒影的圖片方法    
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){    
    final int reflectionGap = 4;    
    int width = bitmap.getWidth();    
    int height = bitmap.getHeight();    
            
    Matrix matrix = new Matrix();    
    matrix.preScale(1, -1);    
            
    Bitmap reflectionImage = Bitmap.createBitmap(bitmap,0, height/2, width, height/2, matrix, false);    
                
    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2), Config.ARGB_8888);    
                
    Canvas canvas = new Canvas(bitmapWithReflection);    
    canvas.drawBitmap(bitmap, 0, 0, null);    
    Paint deafalutPaint = new Paint();    
    canvas.drawRect(0, height,width,height + reflectionGap,    
    deafalutPaint);    
       
    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);    
             
    Paint paint = new Paint();    
    LinearGradient shader = new LinearGradient(0,    
    bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);    
    paint.setShader(shader);    
    // Set the Transfer mode to be porter duff and destination in    
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));    
    // Draw a rectangle using the paint with our linear gradient    
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);    
         
    return bitmapWithReflection;    
}  













另外,decodeStream直接拿的圖片來讀取字節碼了, 不會根據機器的各類分辨率來自動適應, 使用了decodeStream以後,須要在hdpi和mdpi,ldpi中配置相應的圖片資源, 不然在不一樣分辨率機器上都是一樣大小(像素點數量),顯示出來的大小就不對了。 可參考下面的代碼

BitmapFactory.Options opts = new BitmapFactory.Options();
//設置圖片的DPI爲當前手機的屏幕dpi
opts.inTargetDensity = ctx.getResources().getDisplayMetrics().densityDpi;  
opts.inScaled = true;

另外,圖片的bitmap對象爲大對象,不用了要注意主動回收,

 if(!bmp.isRecycle() ){
         bmp.recycle()   //回收圖片所佔的內存
         system.gc()  //提醒系統及時回收
}








【個人應用】:

    /**
     * 讀取本地圖片的bitmap
     *
     * @Description:
     * @param filePath
     * @param outWidth
     * @param outHeight
     * @return
     * @see:
     * @since:
     * @author: zhuanggy
     * @date:2013-3-12
     */
    public static Bitmap readBitmapAutoSize(String filePath, int outWidth, int outHeight) {
        // outWidth和outHeight是目標圖片的最大寬度和高度,用做限制
        FileInputStream fs = null;
        BufferedInputStream bs = null;
        try {
            fs = new FileInputStream(filePath);
            bs = new BufferedInputStream(fs);
            BitmapFactory.Options options = setBitmapOption(filePath, outWidth, outHeight);
            return BitmapFactory.decodeStream(bs, null, options);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bs.close();
                fs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private static BitmapFactory.Options setBitmapOption(String file, int width, int height) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inJustDecodeBounds = true;
        // 設置只是解碼圖片的邊距,此操做目的是度量圖片的實際寬度和高度
        BitmapFactory.decodeFile(file, opt);

        int outWidth = opt.outWidth; // 得到圖片的實際高和寬
        int outHeight = opt.outHeight;

        GoOutDebug.e(TAG, "outWidth=" + outWidth + "  outHeight=" + outHeight);

        opt.inDither = false;
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        // 設置加載圖片的顏色數爲16bit,默認是RGB_8888,表示24bit顏色和透明通道,但通常用不上
        opt.inSampleSize = 1;

        // 設置縮放比,1表示原比例,2表示原來的四分之一....
        if (outWidth != 0 && outHeight != 0 && width != 0 && height != 0) {
            int sampleSize = (outWidth / width + outHeight / height) / 2;
            opt.inSampleSize = sampleSize;
        }

        opt.inJustDecodeBounds = false;// 最後把標誌復原
        return opt;
    }

    /**
     * 得到圓角圖片
     *
     * @Description:
     * @param bitmap
     * @param roundPx
     * @return
     * @see:
     * @since:
     * @author: zhuanggy
     * @date:2013-3-14
     */
    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int width, int height) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();

        // 若讀取圖片的寬度或高度小於ImageView的寬度或高度,則對圖片進行放大
        if (w < width || h < height) {
            Matrix matrix = new Matrix();
            matrix.postScale((float) width / w, (float) height / h); // 長和寬放大縮小的比例
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        }
        // GoOutDebug.e(TAG, "w = " + output.getWidth() + "   h = " + output.getHeight());
        // 建立一個新的bitmap,而後在bitmap裏建立一個圓角畫布,將以前的圖片畫在裏面。
        Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, width, height);
        final RectF rectF = new RectF(rect);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, 10, 10, paint);// 圓角平滑度
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }
canvas

相關文章
相關標籤/搜索