android 图片编辑时需要从外界(sdcard ,res/.png...,xml)读取图片到画布,其中从sdcard读取图片到画布的过程如下:
public void drawBitMapFromSDcard(String dir) {
if(dir ==null || dir.equals("")){
return ;
}
bitMap = BitmapFactory.decodeFile(dir);
int width = bitMap.getWidth();
int height = bitMap.getHeight();
如果图片像素太大超过手机屏幕大小可以按照手机屏比例进行缩放
if (width > 320 && height > 480) {
int newWidth = 320;
int newHeight = 480;
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
float minScale = Math.min(scaleWidth, scaleHeight);
matrix = new Matrix();
matrix.postScale(minScale, minScale);
bitMap = Bitmap.createBitmap(bitMap, 0, 0, width, height,
matrix, true);
}
显示图片到手机屏幕
mCanvas.drawBitmap(bitMap, 0, 0, mPaint);
invalidate();
(二)图片剪切
在android 画布上对图像进行剪切时,首先选中一块区域,得到所选区域的像素点,然后放到到要粘贴的位置。剪切代码如下
public int[] getClipRectangePix(RectF rectf, Bitmap bitmap) {
int width = (int) rectf.width();
int height = (int) rectf.height();
int[] clipRectangePix = new int[(width * height)];// ????
int x = (int) rectf.left;
int y = (int) rectf.top;
bitmap.getPixels(clipRectangePix, 0, width, x, y, width, height);
// Log.v("pix Color:",""+ clipRectangePix[0] );
return clipRectangePix;
}
public void drawBitMapFromSDcard(String dir) {
if(dir ==null || dir.equals("")){
return ;
}
bitMap = BitmapFactory.decodeFile(dir);
int width = bitMap.getWidth();
int height = bitMap.getHeight();
如果图片像素太大超过手机屏幕大小可以按照手机屏比例进行缩放
if (width > 320 && height > 480) {
int newWidth = 320;
int newHeight = 480;
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
float minScale = Math.min(scaleWidth, scaleHeight);
matrix = new Matrix();
matrix.postScale(minScale, minScale);
bitMap = Bitmap.createBitmap(bitMap, 0, 0, width, height,
matrix, true);
}
显示图片到手机屏幕
mCanvas.drawBitmap(bitMap, 0, 0, mPaint);
invalidate();
(二)图片剪切
在android 画布上对图像进行剪切时,首先选中一块区域,得到所选区域的像素点,然后放到到要粘贴的位置。剪切代码如下
public int[] getClipRectangePix(RectF rectf, Bitmap bitmap) {
int width = (int) rectf.width();
int height = (int) rectf.height();
int[] clipRectangePix = new int[(width * height)];// ????
int x = (int) rectf.left;
int y = (int) rectf.top;
bitmap.getPixels(clipRectangePix, 0, width, x, y, width, height);
// Log.v("pix Color:",""+ clipRectangePix[0] );
return clipRectangePix;
}