一、 相关概念
1. Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象
2. Canvas画布,绘制的目的区域,用于绘图
3. Bitmap位图,用于图的处理
4. Matrix矩阵,此例中用于操作图片
二、 步骤
1. 把drawable画到位图对象上
2. 对位图对象做缩放(或旋转等)操作
3. 把位图再转换成drawable
三、示例
static Bitmap drawableToBitmap(Drawabledrawable)//drawable 转换成bitmap
{
int width = drawable.getIntrinsicWidth(); // 取drawable的长宽
int height = drawable.getIntrinsicHeight();
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE? Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565; // 取drawable的颜色格式
Bitmap bitmap = Bitmap.createBitmap(width, height,config); // 建立对应bitmap
Canvas canvas = new Canvas(bitmap); // 建立对应bitmap的画布
drawable.setBounds(0, 0, width, height);
drawable.draw(canvas); // 把drawable内容画到画布中
return bitmap;
}
static Drawable zoomDrawable(Drawable drawable, int w, int h)
{
int width = drawable.getIntrinsicWidth();
int height= drawable.getIntrinsicHeight();
Bitmap oldbmp = drawableToBitmap(drawable);// drawable转换成bitmap
Matrix matrix = new Matrix(); // 创建操作图片用的Matrix对象
float scaleWidth = ((float)w / width); // 计算缩放比例
float scaleHeight = ((float)h / height);
matrix.postScale(scaleWidth, scaleHeight); // 设置缩放比例
Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,matrix, true); // 建立新的bitmap,其内容是对原bitmap的缩放后的图
return new BitmapDrawable(newbmp); // 把bitmap转换成drawable并返回
}
}
四、其它
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
public class FunctionUtil {
public static Bitmap imageScale(Bitmap bitmap,int dst_w, int dst_h){
int src_w =bitmap.getWidth();
int src_h =bitmap.getHeight();
float scale_w= ((float)dst_w)/src_w;
float scale_h= ((float)dst_h)/src_h;
Matrix matrix= new Matrix();
matrix.postScale(scale_w,scale_h);
Bitmap dstbmp =Bitmap.createBitmap(bitmap, 0, 0, src_w, src_h, matrix,true);
return dstbmp;
}
或者使用Bitmap.createScaledBitmap(bitmap, width, heigth,true);方法,
需要指定缩放后的图片大小尺寸。
public static Bitmap drawabletoBitmap(Drawabledrawable){
int width =drawable.getIntrinsicWidth();
int height =drawable.getIntrinsicWidth();
Bitmap bitmap =Bitmap.createBitmap(width, height, drawable.getOpacity() !=PixelFormat.OPAQUE ?
Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565);
Canvas canvas = newCanvas(bitmap);
drawable.setBounds(0, 0, width,height);
drawable.draw(canvas);
return bitmap;
}
public static Bitmap getRCB(Bitmap bitmap, floatroundPX) //RCB means Rounded Corner Bitmap
{
Bitmap dstbmp =Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),Config.ARGB_8888);
Canvas canvas = newCanvas(dstbmp);
final int color =0xff424242;
final Paint paint = newPaint();
final Rect rect = new Rect(0,0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = newRectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0,0);
paint.setColor(color);
canvas.drawRoundRect(rectF,roundPX, roundPX, paint);
paint.setXfermode(newPorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect,rect, paint);
return dstbmp;
}
}