Android - Crop an image from multipoints(Android - 从多点裁剪图像)
问题描述
我需要裁剪一个位图,但不是有一个 矩形 裁剪图像(我成功地做到了),我需要它是 任何形式 定义坐标.
I need to crop a Bitmap, but instead of having a rectangular cropped image (which I managed successfully to do), I need it to be any form defined by coordinates.
我正在关注此线程的答案:从 Bitmap 中剪下一个多点多边形并将其置于透明度上,并尝试实现它,但不幸的是它没有剪裁图像.
I'm following the answer from this thread: Cutting a multipoint ploygon out of Bitmap and placing it on transparency , and trying to implement it, but unfortunatly it does not clip the image.
我按照描述进行了操作,但似乎某处存在错误.图像以矩形方式绘制.我错过了什么吗?
I did as in the description, but it seems there's a bug somewhere. The image is drawn in rectangular way. Am I missing something?
Bitmap originalBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.test_image);
// Image cropped
Bitmap croppedBitmap=Bitmap.createBitmap(originalBitmap, 10, 10, 200, 200);
Canvas canvas=new Canvas(croppedBitmap);
// Create a path
Path path=new Path();
path.setFillType(FillType.INVERSE_EVEN_ODD);
path.moveTo(0, 0);
path.moveTo(0, 100);
path.moveTo(100, 0);
path.moveTo(0, 0);
// Paint with Xfermode
Paint paint=new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
// Draw the path
canvas.drawPath(path, paint);
imageView.setImageBitmap(croppedBitmap);
推荐答案
我非常接近解决方案.这里是:
I was very close to the solution. Here it is:
compositeImageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap1=BitmapFactory.decodeResource(getResources(), R.drawable.batman_ad);
Bitmap bitmap2=BitmapFactory.decodeResource(getResources(), R.drawable.logo);
Bitmap resultingImage=Bitmap.createBitmap(320, 480, bitmap1.getConfig());
Canvas canvas = new Canvas(resultingImage);
Paint paint = new Paint();
paint.setAntiAlias(true);
Path path=new Path();
path.lineTo(150, 0);
path.lineTo(230, 120);
path.lineTo(70, 120);
path.lineTo(150, 0);
canvas.drawPath(path, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap2, 0, 0, paint);
compositeImageView.setImageBitmap(resultingImage);
这篇关于Android - 从多点裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android - 从多点裁剪图像
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01