android 图片剪裁

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1.

Canvas类用来实现绘制.通过组合使用Canva s类的成员函数可以实现随心随欲地绘制图片的任何部分.

Canvas.clipRect:设置显示区域

Canvas.drawBitmap:绘制

例子:

Bitmap b=Bitma pFactory.decodeStream("图片编号", null);//读取图片

...

Canvas c = null;//实例Canvas

c.save();//记录原来的ca nvas状态

c.clipRect(100,100,200,300);//显示从(100,100)到(200,300)的区域(单位:象素)

c.drawBitmap(b,10,0,null); //将阉割过的图片画到(10,0)位置

c.restore();//恢复canva s状态

2.

android 从sdcard 读取图片剪切粘贴

文章分类:移动开发

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;

}

相关文档
最新文档