canvas 多个图片叠加,图片覆盖图片显示到imageview

来源:互联网 发布:数据安全防护功能 编辑:程序博客网 时间:2024/06/02 01:46

ImageView imageView=(ImageView)findViewById(R.id.image);
// 防止出现Immutable bitmap passed to Canvas constructor错误
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(),
R.drawable.icon).copy(Bitmap.Config.ARGB_8888, true);
Bitmap bitmap2 = ((BitmapDrawable) getResources().getDrawable(
R.drawable.aa)).getBitmap();
Bitmap bitmap3 = ((BitmapDrawable) getResources().getDrawable(
R.drawable.play)).getBitmap();

    Bitmap newBitmap = null;    newBitmap = Bitmap.createBitmap(bitmap1);    Canvas canvas = new Canvas(newBitmap);    Paint paint = new Paint();    int w = bitmap1.getWidth();    int h = bitmap1.getHeight();    int w_2 = bitmap2.getWidth();    int h_2 = bitmap2.getHeight();    int w_3 = bitmap3.getWidth();    int h_3 = bitmap3.getHeight();

// paint.setColor(Color.GRAY);
paint.setAlpha(125);

// canvas.clipRect(10, 10, 1000, 1000);
// canvas.drawRect(0, 0, 100, 100, paint);
canvas.drawRect(0, 0, bitmap1.getWidth(), bitmap1.getHeight(), paint);
paint = new Paint();
paint.setFakeBoldText(true); //true为粗体,false为非粗体
canvas.drawBitmap(bitmap2, Math.abs(w - w_2) / 2,
Math.abs(h - h_2) / 2, paint);
canvas.save(Canvas.ALL_SAVE_FLAG);

    canvas.drawBitmap(bitmap3, Math.abs(w_2 - w_3),            Math.abs(h_2 - h_3), paint);    // 存储新合成的图片    canvas.restore();    imageView.setImageBitmap(newBitmap);
原创粉丝点击