如何在android中裁剪图像?
可能重复:
How to crop the parsed image in android?如何在android中裁剪图像?
我在水库/可绘制文件夹中有一个形象,我想裁剪(即切出的图像的某些部分)的图像时加载它成了一个ImageView。但我不确定如何做到这一点,有什么建议吗?
如果你想同样裁剪图像之外,你应该看看ScaleType属性为ImageView的:http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
特别,你会感兴趣的“centerCrop”选项。它会剪出大于定义大小的图像的一部分。
下面是在XML布局这样的例子:
<ImageView android:id="@+id/title_logo"
android:src="@drawable/logo"
android:scaleType="centerCrop" android:padding="4dip"/>
其如此愚蠢,他们没有添加更多的作物选项,然后中心作物... – 2011-12-19 14:29:02
int targetWidth = 100;
int targetHeight = 100;
RectF rectf = new RectF(0, 0, 100, 100);//was missing before update
Bitmap targetBitmap = Bitmap.createBitmap(
targetWidth, targetHeight,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addRect(rectf, Path.Direction.CW);
canvas.clipPath(path);
canvas.drawBitmap(
sourceBitmap,
new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()),
new Rect(0, 0, targetWidth, targetHeight),
null);
ImageView imageView = (ImageView)findViewById(R.id.my_image_view);
imageView.setImageBitmap(targetBitmap);
您设置路径rect“rectf”,但不要定义它。如何计算剪裁矩形的尺寸以正确剪切位图的中心?即不能从角落剪出。 – MarkPowell 2011-01-11 16:06:35
除非需要进一步修改位图,否则使用采用源图像的Bitmap.createBitmap的方法应该效率更高。由于它创建了一个不可变的位图,因此它不必制作副本。 (除非源是可变的) – 2011-12-19 14:26:48
什么是rectf?请回复 – 2015-06-17 12:51:42
Android的联系经理EditContactActivity使用Intent("com.android.camera.action.CROP")
这是一个示例代码:
Intent intent = new Intent("com.android.camera.action.CROP");
// this will open all images in the Galery
intent.setDataAndType(photoUri, "image/*");
intent.putExtra("crop", "true");
// this defines the aspect ration
intent.putExtra("aspectX", aspectY);
intent.putExtra("aspectY", aspectX);
// this defines the output bitmap size
intent.putExtra("outputX", sizeX);
intent.putExtra("outputY", xizeY);
// true to return a Bitmap, false to directly save the cropped iamge
intent.putExtra("return-data", false);
//save output image in uri
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
如果我在SD Card/myimage.jpg上有一张图片,我如何才能将URI映射到CROP intent ?即。我想裁剪一张SD Card/myimage.jpg图像的区域。 – 2011-05-20 18:04:16
#Nguyen,你有什么评论。 – 2012-11-25 08:20:36
你有* ... – 2012-11-27 08:58:59
尝试这个:
ImageView ivPeakOver=(ImageView) findViewById(R.id.yourImageViewID);
Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.yourImageID);
int width=(int)(bmp.getWidth()*peakPercent/100);
int height=bmp.getHeight();
Bitmap resizedbitmap=Bitmap.createBitmap(bmp,0,0, width, height);
ivPeakOver.setImageBitmap(resizedbitmap);
从文档:
static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height)
返回从源位图的指定子集的不可变位图。
不错!仍然愚蠢它不能用ImageView的一个简单属性来完成,但我想这应该是足够高效的。 – 2011-12-19 14:32:59
从Bitmap.createBitmap: “从源位图的指定子集中返回一个不可变的位图。新位图可能与源相同,或者可能已创建一个副本,它的初始化密度与原始位图“。
将它传递给位图,并定义将从中创建新位图的矩形。
// Take 10 pixels off the bottom of a Bitmap
Bitmap croppedBmp = Bitmap.createBitmap(originalBmp, 0, 0, originalBmp.getWidth(), originalBmp.getHeight()-10);
对我来说,只有当我做了'originalBmp.getWidth() - 1'时才有效。似乎所有的裁剪尺寸必须完全落入原稿内部,否则原件将被退回。 – 2013-07-17 06:49:07
链接的问题答案不回答如何裁剪,请阅读它。 – m0skit0 2017-01-16 18:12:57