Android 图片处理之图片叠加--Bitmap

今天要实现一个小功能,就是在一张图片上,套上一层半透明的图片或者颜色。例如下图:

 

原图   处理后

方法其实很多,现在先实现一种方法,部分代码如下啊:

 

  
  
  
  
  1. ImageView image = (ImageView) findViewById(R.id.imageView1);
  2.  
  3.    // 防止出现Immutable bitmap passed to Canvas constructor错误  
  4.    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),  
  5.            R.drawable.teaport_1).copy(Bitmap.Config.ARGB_8888, true);
  6. // 新的图片 
  7.    Bitmap newBitmap = null;  
  8.    newBitmap = Bitmap.createBitmap(bitmap);  
  9.    Canvas canvas = new Canvas(newBitmap);  
  10.    Paint paint = new Paint();  
  11.   // 铺上一层半透明的绿色
  12.    paint.setColor(Color.GREEN);  
  13.    paint.setAlpha(70);  
  14.    canvas.drawRect(00, bitmap.getWidth(), bitmap.getHeight(), paint);  
  15.    canvas.save(Canvas.ALL_SAVE_FLAG);  
  16.    // 存储新合成的图片  
  17.    canvas.restore();  
  18.   // 搞定!
  19.    image.setImageBitmap(newBitmap); 

 方法很简单。canvas.drawRect 换成 drawBitmap就是叠加一张图片。  另外还有很多方法,例如LayerDrawable类 。以后碰到在继续整理。

你可能感兴趣的:(android,图片处理,叠加)