1.將圖片設置成圓角
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;
}
2、給圖片設置倒影效果
private Bitmap createReflectedImage(Bitmap originalBitmap) {
// 圖片與倒影間隔距離
final int reflectionGap = 4;
// 圖片的寬度
int width = originalBitmap.getWidth();
// 圖片的高度
int height = originalBitmap.getHeight();
Matrix matrix = new Matrix();
// 圖片縮放,x軸變爲原來的1倍,y軸爲-1倍,實現圖片的反轉
matrix.preScale(1, -1);
// 創建反轉後的圖片Bitmap對象,圖片高是原圖的一半。
Bitmap reflectionBitmap = Bitmap.createBitmap(originalBitmap, 0,
height / 2, width, height / 2, matrix, false);
// 創建標準的Bitmap對象,寬和原圖一致,高是原圖的1.5倍。
Bitmap withReflectionBitmap = Bitmap.createBitmap(width, (height
+ height / 2 + reflectionGap), Config.ARGB_8888);
// 構造函數傳入Bitmap對象,爲了在圖片上畫圖
Canvas canvas = new Canvas(withReflectionBitmap);
// 畫原始圖片
canvas.drawBitmap(originalBitmap, 0, 0, null);
// 畫間隔矩形
Paint defaultPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
// 畫倒影圖片
canvas.drawBitmap(reflectionBitmap, 0, height + reflectionGap, null);
// 實現倒影效果
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, originalBitmap.getHeight(),
0, withReflectionBitmap.getHeight(), 0x70ffffff, 0x00ffffff,
TileMode.MIRROR);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
// 覆蓋效果
canvas.drawRect(0, height, width, withReflectionBitmap.getHeight(), paint);
return withReflectionBitmap;
}
3、灰度處理方法
public Bitmap convertGreyImg(Bitmap img) {
int width = img.getWidth(); //獲取位圖的寬
int height = img.getHeight(); //獲取位圖的高
int []pixels = new int[width * height]; //通過位圖的大小創建像素點數組
img.getPixels(pixels, 0, width, 0, 0, width, height);
int alpha = 0xFF << 24;
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
int grey = pixels[width * i + j];
int red = ((grey & 0x00FF0000 ) >> 16);
int green = ((grey & 0x0000FF00) >> 8);
int blue = (grey & 0x000000FF);
grey = (int)((float) red * 0.3 + (float)green * 0.59 + (float)blue * 0.11);
grey = alpha | (grey << 16) | (grey << 8) | grey;
pixels[width * i + j] = grey;
}
}
Bitmap result = Bitmap.createBitmap(width, height, Config.RGB_565);
result.setPixels(pixels, 0, width, 0, 0, width, height);
return result;
}
4.縮放大小處理
//放大縮小圖片
public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidht = ((float)w / width);
float scaleHeight = ((float)h / height);
matrix.postScale(scaleWidht, scaleHeight);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
return newbmp;
}
//將Drawable轉化爲Bitmap
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;
}
5、處理圖片過大
BitmapFactory.Options opts=new BitmapFactory.Options();
/*縮放的比例,縮放是很難按準備的比例進行縮放的,其值表明縮放的倍數,SDK中建議其值是2的指數值,值越大會導致圖片不清晰*/
opts.inSampleSize=6;
Bitmap bmp=null;
bmp = BitmapFactory.decodeStream(i, null, opts);
6.處理成圓角頭像
/**
* Crops a circle out of the thumbnail photo.
*/
public Bitmap getCroppedBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
Config.ARGB_8888);
//設置一個圖片大小的矩形
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
//bm是一個剛好canvas大小的空Bitmap ,畫完後應該會自動保存到bm
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
paint.setAntiAlias(true);
int halfWidth = bitmap.getWidth()/2;
int halfHeight = bitmap.getHeight()/2;
//畫圓
canvas.drawCircle(halfWidth, halfHeight, Math.max(halfWidth, halfHeight), paint);
//設置爲取兩層圖像交集部門,只顯示上層圖像
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
//畫圖像
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}