Android 圖片處理方法大全

package cn.com.mzba.service;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;


import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.Drawable;
import android.util.Log;


public class ImageThumbnail {


/**
* 計算縮放比
* @param oldWidth
* @param oldHeight
* @param newWidth
* @param newHeight
* @return
*/
    public static int reckonThumbnail(int oldWidth, int oldHeight, int newWidth, int newHeight) {
        if ((oldHeight > newHeight && oldWidth > newWidth)
                || (oldHeight <= newHeight && oldWidth > newWidth)) {
            int be = (int) (oldWidth / (float) newWidth);
            if (be <= 1)
                be = 1;
            return be;
        } else if (oldHeight > newHeight && oldWidth <= newWidth) {
            int be = (int) (oldHeight / (float) newHeight);
            if (be <= 1)
                be = 1;
            return be;
        }


        return 1;
    }


    /**
     * @param photoPath --原圖路經
     * @param aFile     --保存縮圖
     * @param newWidth  --縮圖寬度
     * @param newHeight --縮圖高度
     */
    public static boolean bitmapToFile(String photoPath, File aFile, int newWidth, int newHeight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        // 獲取這個圖片的寬和高
        Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
        options.inJustDecodeBounds = false;
 
        //計算縮放比
        options.inSampleSize = reckonThumbnail(options.outWidth, options.outHeight, newWidth, newHeight);


        bitmap = BitmapFactory.decodeFile(photoPath, options);


        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] photoBytes = baos.toByteArray();


            if (aFile.exists()) {
                aFile.delete();
            }
            aFile.createNewFile();


            FileOutputStream fos = new FileOutputStream(aFile);
            fos.write(photoBytes);
            fos.flush();
            fos.close();


            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            if (aFile.exists()) {
                aFile.delete();
            }
            Log.e("Bitmap To File Fail", e1.toString());
            return false;
        }
    }


    /**
     * 縮放圖片
     * @param bmp
     * @param width
     * @param height
     * @return
     */
    public static Bitmap PicZoom(Bitmap bmp, int width, int height) {
        int bmpWidth = bmp.getWidth();
        int bmpHeght = bmp.getHeight();
        Matrix matrix = new Matrix();
        matrix.postScale((float) width / bmpWidth, (float) height / bmpHeght);


        return Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeght, matrix, true);
    }
    /**
    * 把圖片變成圓角   
    * @param bitmap 需要修改的圖片   
    * @param pixels 圓角的弧度   
    * @return 圓角圖片   
    */    
   public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {    
     
       Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);    
       Canvas canvas = new Canvas(output);    
     
       final int color = 0xff424242;    
       final Paint paint = new Paint();    
       final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());    
       final RectF rectF = new RectF(rect);    
       final float roundPx = pixels;    
     
       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;    
   }  
   
   /**
    * 獲取圖片的倒影 
    * @param bitmap
    * @return
    */
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);
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
+ reflectionGap, paint);
return bitmapWithReflection;
}

/**
* 將Drawable轉化爲Bitmap 
* @param drawable
* @return
*/
public static Bitmap drawableToBitmap(Drawable drawable) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, width, height);
drawable.draw(canvas);
return bitmap;
}


}



解釋:solid的表示填充顏色,爲了簡單,這裏用的是黑色。

而corners則是表示圓角,注意的是這裏bottomRightRadius是左下角而不是右下角,bottomLeftRadius右下角。 
當然上面的效果也可以像下面一樣設置,如下: <corners android:radius="5dp" />  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章