Drawable转BitMap
/**
*Drawable转Bitmap
* @param drawable
* @return bitmap
*/
public static Bitmap getBmpFormDraw(Drawable drawable) {
Bitmap bitmap;
if (drawable instanceof ColorDrawable) {
bitmap = Bitmap.createBitmap(
COLORDRAWABLE_DIMENSION,
COLORDRAWABLE_DIMENSION,
Bitmap.Config.ARGB_8888);
} else {
bitmap = Bitmap.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ?
Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
}
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
Canvas canvas = new Canvas(bitmap);
drawable.draw(canvas);
return bitmap;
}
重新设置bitmap的大小
/**
*重新设置bitmap大小
* @param bitmap 处理之前的图片
* @param w bitmap处理之后的宽度
* @param h bitmap处理之后的高度
* @return bitmap 处理之后的图片
*/
public static Bitmap resizeImage(Bitmap bitmap, int w, int h) {
Bitmap BitmapOrg = bitmap;
int width = BitmapOrg.getWidth();
int height = BitmapOrg.getHeight();
int newWidth = w;
int newHeight = h;
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
height, matrix, true);
return resizedBitmap;
}
变圆处理
/**
*弄圆形图片
* @param borderWidth 边界宽度
* @param borderColor 边界颜色
* @return bitmap 处理之后图片
*/
public static Bitmap getCircleBitmap(Bitmap mBitmap, int borderWidth, int borderColor) {
Paint mPaint = new Paint();
BitmapShader shader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mPaint.setShader(shader);
mPaint.setAntiAlias(true);
int minLength = Math.min(mBitmap.getWidth(), mBitmap.getHeight());
int radius = minLength / 2;
Bitmap newBitmap = Bitmap.createBitmap(
minLength + borderWidth * 2,
minLength + borderWidth * 2,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawCircle(borderWidth + radius, borderWidth + radius, radius, mPaint);
if (borderWidth != 0) {
Paint borderPaint = new Paint();
borderPaint.setColor(borderColor);
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setAntiAlias(true);
borderPaint.setStrokeWidth(borderWidth);
canvas.drawCircle(borderWidth + radius, borderWidth + radius, radius, borderPaint);
}
return newBitmap;
}
总体代码
public class CircleMake {
private static int COLORDRAWABLE_DIMENSION = 2;
private final static int borderWidth = 2;
private final static int borderColor = Color.WHITE;
public static Bitmap getBitmap(Context context, int rsId, Resources.Theme theme) {
Drawable drawable = context.getResources().getDrawable(rsId, theme);
return getBitmap(drawable, borderWidth, borderColor);
}
public static Bitmap getBitmap(Drawable drawable) {
return getBitmap(drawable, borderWidth, borderColor);
}
public static Bitmap getBitmap(Drawable drawable, int borderWidth, int borderColor) {
Bitmap bitmap = getBmpFormDraw(drawable);
return getCircleBitmap(bitmap, borderWidth, borderColor);
}
/**
*弄圆形图片
* @param borderWidth 边界宽度
* @param borderColor 边界颜色
* @return bitmap 处理之后图片
*/
public static Bitmap getCircleBitmap(Bitmap mBitmap, int borderWidth, int borderColor) {
Paint mPaint = new Paint();
BitmapShader shader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mPaint.setShader(shader);
mPaint.setAntiAlias(true);
int minLength = Math.min(mBitmap.getWidth(), mBitmap.getHeight());
int radius = minLength / 2;
Bitmap newBitmap = Bitmap.createBitmap(
minLength + borderWidth * 2,
minLength + borderWidth * 2,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawCircle(borderWidth + radius, borderWidth + radius, radius, mPaint);
if (borderWidth != 0) {
Paint borderPaint = new Paint();
borderPaint.setColor(borderColor);
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setAntiAlias(true);
borderPaint.setStrokeWidth(borderWidth);
canvas.drawCircle(borderWidth + radius, borderWidth + radius, radius, borderPaint);
}
return newBitmap;
}
/**
*Drawable转Bitmap
* @param drawable
* @return bitmap
*/
public static Bitmap getBmpFormDraw(Drawable drawable) {
Bitmap bitmap;
if (drawable instanceof ColorDrawable) {
bitmap = Bitmap.createBitmap(
COLORDRAWABLE_DIMENSION,
COLORDRAWABLE_DIMENSION,
Bitmap.Config.ARGB_8888);
} else {
bitmap = Bitmap.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ?
Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
}
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
Canvas canvas = new Canvas(bitmap);
drawable.draw(canvas);
return bitmap;
}
/**
*重新设置bitmap大小
* @param bitmap 处理之前的图片
* @param w bitmap处理之后的宽度
* @param h bitmap处理之后的高度
* @return bitmap 处理之后的图片
*/
public static Bitmap resizeImage(Bitmap bitmap, int w, int h) {
Bitmap BitmapOrg = bitmap;
int width = BitmapOrg.getWidth();
int height = BitmapOrg.getHeight();
int newWidth = w;
int newHeight = h;
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
height, matrix, true);
return resizedBitmap;
}
}