android:照片涂画功能实现过程及原理详解

2019-12-10 20:09:53于丽

 

【注】

ImageView的实际大小等于屏幕的大小,Canvas的实际大小由图片实际大小决定。

ImageView的宽高很容易取得,但是它里面的图片是变过形的,怎么获取它的当前大小呢?用(原始大小*缩放系数)。

合并
最后一步就是将两个图层合并为一张图片。参考代码如下:

 

复制代码
/**
* 合并两张bitmap为一张
* @param background
* @param foreground
* @return Bitmap
*/
public static Bitmap combineBitmap(Bitmap background, Bitmap foreground) {
    if (background == null) {
       return null;
    }
    int bgWidth = background.getWidth();
    int bgHeight = background.getHeight();
    int fgWidth = foreground.getWidth();
    int fgHeight = foreground.getHeight();

 

    Bitmap newmap = Bitmap.createBitmap(bgWidth, bgHeight, Config.ARGB_8888); 
    Canvas canvas = new Canvas(newmap);
    canvas.drawBitmap(background, 0, 0, null);
    canvas.drawBitmap(foreground, (bgWidth - fgWidth) / 2,
    (bgHeight - fgHeight) / 2, null);
    canvas.save(Canvas.ALL_SAVE_FLAG);
    canvas.restore();
    return newmap;
} //end of combineBitmap 

 

通过Canvas来合并和改变Bitmap的大小,由于两个图层的大小、位置完全一致,故坐标对齐(0,0)点就可以了。

如果,没有前面的工作,你是很难精确的进行图片合并的。