1.Canvas上以一定收缩比例贴一张图
headBgBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.class_bg_sha1); headBmp = Bitmap.createBitmap(view.getWidth(),headBgBmp.getHeight(),Config.ARGB_8888); Canvas canvas2 = new Canvas(headBmp); Matrix matrix = new Matrix(); float xScale = (float) view.getWidth()/headBgBmp.getWidth(); matrix.setScale(xScale, 1.0f); canvas2.drawBitmap(headBgBmp, matrix, null);
2.Canvas上贴9.png的图
首先你要讲图片进行9.png处理
NinePatchDrawable drawable = (NinePatchDrawable) context.getResources().getDrawable(R.drawable.class_bg_sha2_2);
frameBg = drawableToBitmap(drawable, width, height);
drawableToBitmap方法:
public Bitmap drawableToBitmap(Drawable drawable, int w, int h) { // 取 drawable 的颜色格式 Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; // 建立对应 bitmap Bitmap bitmap = Bitmap.createBitmap(w, h, config); // 建立对应 bitmap 的画布 Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, w, h); // 把 drawable 内容画到画布中 drawable.draw(canvas); return bitmap; }