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;
}