Android的图片叠加

来源:互联网 发布:php 时间轴 编辑:程序博客网 时间:2024/05/10 20:37
android.graphics.Bitmap.Config是一个枚举类型,里面定义了位图的四种格式.
 
ALPHA_8,ARGB_4444,ARGB_8888都是透明的位图,也就是所字母A代表透明.
 

从名字中可以看出:

    ALPHA_8: 一种透明度为8位的位图.

    ARGB_4444: A,R,G,B,每一个参数由4bit表示,是一种16位的位图.

    ARGB_8888: A,R,G,B,每一个参数由8bit来表示,是一种32位的位图.

    RGB_565: R,G,B,三个参数分别占5bit,6bit,5bit,是一种三原色通道经过压缩的16位位图.
Bitmap newBitmap = Bitmap. createBitmap( imgMapWidth,imgMapHeight , Bitmap.Config.RGB_565 );

 android中可以通过以上的方式来创建一BMP格式的位图,只是图片的宽和高的值太大时,会使android的VM产生内存溢出的Exception,解决的方法之一,在生成该对象时,通过设Bitmap.Config,来压缩该位图在生成时的大小。从上面的4个属性值看来,压缩的最大值可以达到2^8。

 
以下方法能有效解决新建位图的大小过大的问题:
    imageMap是一位图的对象,尺寸有点大,1800*1600,用上面的方法创建位图会抛出异常。
    以下的方法通过判断当前的位图是否可画(可在上面画图),而决定是否复制一新对象,然后在可画图的对象上新建一画布,然后在上面画图,最后就是保存了。
复制代码
//在指定的位图上添加图标private Bitmap addMarkToImageMap(int x, int y){    //创建一个和原图同样大小的位图     //Bitmap newBitmap = Bitmap.createBitmap(imgMapWidth,imgMapHeight, Bitmap.Config.RGB_565);    Bitmap newBitmap = imageMap;    if(!newBitmap.isMutable()){         newBitmap = imageMap.copy(Bitmap.Config.RGB_565, true);    }    Canvas canvas = new Canvas(newBitmap);    canvas.drawBitmap( markImg, x, y, paint );//插入图标     canvas.save(Canvas. ALL_SAVE_FLAG);     //存储新合成的图片    canvas.restore();     return newBitmap;  }
复制代码

顺便延伸一下,在图片上写字的例子就出来了:

 

复制代码
 /**      * 图片上画字      * */     private Bitmap drawTextAtBitmap(Bitmap bitmap,String text){                    int x = bitmap.getWidth();          int y = bitmap.getHeight();                    // 创建一个和原图同样大小的位图          Bitmap newbit = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888);                    Canvas canvas = new Canvas(newbit);                    Paint paint = new Paint();                    // 在原始位置0,0插入原图          canvas.drawBitmap(bitmap, 0, 0, paint);                    paint.setColor(Color.parseColor("#dedbde"));                    paint.setTextSize(20);                    // 在原图指定位置写上字          canvas.drawText(text, 53 , 30, paint);                    canvas.save(Canvas.ALL_SAVE_FLAG);                    // 存储          canvas.restore();                    return newbit;     }
复制代码

  大家可能知道Bitmap的叠加处理在Android平台中可以通过Canvas一层一层的画就行了,而Drawable中如何处理呢?
  除了使用BitmapDrawable的getBitmap方法将Drawable转换为Bitmap外,


  今天给大家说下好用简单的 LayerDrawable类,LayerDrawable顾名思义就是层图形对象。
  下面直接用一个简单的代码表示:

    Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.cwj);
    Drawable[] array = new Drawable[3];

    array[0] = new PaintDrawable(Color.BLACK); //黑色
    array[1] = new PaintDrawable(Color.WHITE); //白色

    array[2] = new BitmapDrawable(bm); //位图资源

    LayerDrawable ld = new LayerDrawable(array); //参数为上面的Drawable数组
    ld.setLayerInset(1, 1, 1, 1, 1);  //第一个参数1代表数组的第二个元素,为白色
    ld.setLayerInset(2, 2, 2, 2, 2); //第一个参数2代表数组的第三个元素,为位图资源
    mImageView.setImageDrawable(ld);

  上面的方法中LayerDrawable是关键,Android开发网提示setLayerInset方法原型为
  public void setLayerInset (int index, int l, int t, int r, int b) 其中第一个参数为层的索引号,
  后面的四个参数分别为left、top、right和bottom。对于简单的图片合成我们可以将第一和第二层的
  PaintDrawable换成BitmapDrawable即可实现简单的图片合成。

原创粉丝点击