android 图片叠加效果——两种方法的简介与内容 ,带解决Immutable bitmap passed to Canvas constructor错误
第一种是通过canvas画出来的效果:
- public void first(View v) {
- // 防止出现Immutable bitmap passed to Canvas constructor错误
- Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(),
- R.drawable.apple).copy(Bitmap.Config.ARGB_8888, true);
- Bitmap bitmap2 = ((BitmapDrawable) getResources().getDrawable(
- R.drawable.go)).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();
- paint.setColor(Color.GRAY);
- paint.setAlpha(125);
- canvas.drawRect(0, 0, bitmap1.getWidth(), bitmap1.getHeight(), paint);
- paint = new Paint();
- canvas.drawBitmap(bitmap2, Math.abs(w - w_2) / 2,
- Math.abs(h - h_2) / 2, paint);
- canvas.save(Canvas.ALL_SAVE_FLAG);
- // 存储新合成的图片
- canvas.restore();
- image.setImageBitmap(newBitmap);
- }
public void first(View v) { // 防止出现Immutable bitmap passed to Canvas constructor错误 Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.apple).copy(Bitmap.Config.ARGB_8888, true); Bitmap bitmap2 = ((BitmapDrawable) getResources().getDrawable( R.drawable.go)).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(); paint.setColor(Color.GRAY); paint.setAlpha(125); canvas.drawRect(0, 0, bitmap1.getWidth(), bitmap1.getHeight(), paint); paint = new Paint(); canvas.drawBitmap(bitmap2, Math.abs(w - w_2) / 2, Math.abs(h - h_2) / 2, paint); canvas.save(Canvas.ALL_SAVE_FLAG); // 存储新合成的图片 canvas.restore(); image.setImageBitmap(newBitmap); }
Canvas canvas = new Canvas(newBitmap); 当以newBitmap创建Canvas时,所以操作都已经在该图上实现了。
该例子可以想做是播放器开始播放的效果,计算好中间位置,先覆盖一层透明灰色的正方形,然后在中间画上自己播放的按钮。
第二种是使用系统的LayerDrawable类,该类主要用来控制多个图片的结合:
- public void second(View v) {
- Bitmap bitmap1 = ((BitmapDrawable) getResources().getDrawable(
- R.drawable.apple)).getBitmap();
- Bitmap bitmap2 = ((BitmapDrawable) getResources().getDrawable(
- R.drawable.go)).getBitmap();
- Drawable[] array = new Drawable[2];
- array[0] = new BitmapDrawable(bitmap1);
- array[1] = new BitmapDrawable(bitmap2);
- LayerDrawable la = new LayerDrawable(array);
- // 其中第一个参数为层的索引号,后面的四个参数分别为left、top、right和bottom
- la.setLayerInset(0, 0, 0, 0, 0);
- la.setLayerInset(1, 20, 20, 20, 20);
- image.setImageDrawable(la);
- }
public void second(View v) { Bitmap bitmap1 = ((BitmapDrawable) getResources().getDrawable( R.drawable.apple)).getBitmap(); Bitmap bitmap2 = ((BitmapDrawable) getResources().getDrawable( R.drawable.go)).getBitmap(); Drawable[] array = new Drawable[2]; array[0] = new BitmapDrawable(bitmap1); array[1] = new BitmapDrawable(bitmap2); LayerDrawable la = new LayerDrawable(array); // 其中第一个参数为层的索引号,后面的四个参数分别为left、top、right和bottom la.setLayerInset(0, 0, 0, 0, 0); la.setLayerInset(1, 20, 20, 20, 20); image.setImageDrawable(la); }
关联数组array,控制每一层的位置
装载自:http://blog.csdn.net/gh102/article/details/6719624
注意:上面防止出现Immutable bitmap passed to Canvas constructor错误
原因是如果不用copy的方法,直接引用会对资源文件进行修改,而android是不允许在代码里修改res文件里的图片