三九宝宝网宝宝绘画儿童画

如何将android中两种bitmap叠加居中形成一个比较漂亮的图

02月12日 编辑 39baobao.com

常用场景:给桌面图标添加底盘背景图 public Bitmap addbackground4onlyicon(Resources r, Bitmap icon) { Bitmap b1 = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap1);; Bitmap b2 = icon; if (!b1.isMutable()) { //设置图片为背景为透明 b1 = b1.copy(Bitmap.Config.ARGB_8888, true); } Paint paint = new Paint(); Canvas canvas = new Canvas(b1); int b1w = b1.getWidth(); int b1h = b1.getHeight(); int b2w = b2.getWidth(); int b2h = b2.getHeight(); int bx = (b1w - b2w) / 2; int by = (b1h - b2h) / 2; canvas.drawBitmap(b2, bx, by, paint);//叠加新图b2 并且居中 canvas.save(Canvas.ALL_SAVE_FLAG); canvas.restore(); return b1; }

推荐阅读
图文推荐