android 裁剪bitmap
public static Bitmap cropBitmap(Bitmap bitmap, Rect crop) {
Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
if (crop == null) {
crop = new Rect(src);
}
if (crop.equals(src)) {
return bitmap;
} else {
if (bitmap.getConfig() != Bitmap.Config.HARDWARE) {
return Bitmap.createBitmap(bitmap, crop.left, crop.top, crop.width(),
crop.height());
}
// For hardware bitmaps, use the Picture API to directly create a software bitmap
android.graphics.Picture picture = new android.graphics.Picture();
Canvas canvas = picture.beginRecording(crop.width(), crop.height());
canvas.drawBitmap(bitmap, -crop.left, -crop.top, null);
picture.endRecording();
return Bitmap.createBitmap(picture, crop.width(), crop.height(),
Bitmap.Config.ARGB_8888);
}
}以上代码为裁剪bitmap功能代码,可以直接使用,
旋转与反转bitmap图片,已封装成方法可直接使用
public static Bitmap rotateAndMirror(Bitmap b, int degrees, boolean mirror) {
if ((degrees != 0 || mirror) && b != null) {
Matrix m = new Matrix();
if (mirror) {
m.postScale(-1, 1);
degrees = (degrees + 360) % 360;
if (degrees == 0 || degrees == 180) {
m.postTranslate(b.getWidth(), 0);
} else if (degrees == 90 || degrees == 270) {
m.postTranslate(b.getHeight(), 0);
} else {
throw new IllegalArgumentException("Invalid degrees=" + degrees);
}
}
if (degrees != 0) {
m.postRotate(degrees,
(float) b.getWidth() / 2, (float) b.getHeight() / 2);
}
try {
Bitmap b2 = Bitmap.createBitmap(
b, 0, 0, b.getWidth(), b.getHeight(), m, true);
if (b != b2) {
b.recycle();
b = b2;
}
} catch (OutOfMemoryError ex) {
ex.getMessage()
}
}
return b;
}指定大小缩放图片
public static Bitmap getScaleBitmap(Bitmap input, int targetWidth, int targetHeight) {
int width = input.getWidth();
int height = input.getHeight();
float scaleWidth = targetWidth * 1.0f / width;
float scaleHeight = targetHeight * 1.0f / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap destBitmap = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(destBitmap);
Paint paint = new Paint();
canvas.drawBitmap(input, matrix, paint);
return destBitmap;
}Bitmap缩放成指定大小圆图
public static Bitmap getRoundedBitmap(Bitmap input, int targetWidth, int targetHeight) {
if (input == null) {
return null;
}
final Bitmap.Config inputConfig = input.getConfig();
final Bitmap result = Bitmap.createBitmap(targetWidth, targetHeight,
inputConfig != null ? inputConfig : Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(result);
final Paint paint = new Paint();
canvas.drawARGB(0, 0, 0, 0);
paint.setAntiAlias(true);
final RectF dst = new RectF(0, 0, targetWidth, targetHeight);
canvas.drawOval(dst, paint);
// Specifies that only pixels present in the destination (i.e. the drawn oval) should
// be overwritten with pixels from the input bitmap.
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
final int inputWidth = input.getWidth();
final int inputHeight = input.getHeight();
// Choose the largest scale factor that will fit inside the dimensions of the
// input bitmap.
final float scaleBy = Math.min((float) inputWidth / targetWidth,(float) inputHeight / targetHeight);
final int xCropAmountHalved = (int) (scaleBy * targetWidth / 2);
final int yCropAmountHalved = (int) (scaleBy * targetHeight / 2);
final Rect src = new Rect(
inputWidth / 2 - xCropAmountHalved,
inputHeight / 2 - yCropAmountHalved,
inputWidth / 2 + xCropAmountHalved,
inputHeight / 2 + yCropAmountHalved);
canvas.drawBitmap(input, src, dst, paint);
return result;
}版权声明:如无特殊标注,文章均为本站原创,转载时请以链接形式注明文章出处。




评论