在帧屏蔽(裁剪)图像屏蔽、图像

2023-09-12 21:30:10 作者:高傲給誰看

有中,我想展示图像形状复杂的像这样

Having a rich UI application in which I want to show image with complex shape like this

现在我要的是裁剪我的形象为每面膜的形象,实际上的图片即将动态的,可以从相机或画廊(正方形或矩形)进口的,我想他的形象来适应我喜欢上面的布局框架

Now what I want is to crop my image as per Mask image, Actually image is coming dynamic and can be imported from Camera or Gallery(square or rectangle shape) and I want that image to fit in my layout frame like above

所以只是想知道的我怎么都做到这一点?任何想法/提示欢迎

So just wondering that how do I have achieve this? Any idea /hint welcome

背景帧 面膜

Background frame Mask

像this

推荐答案

终于得到了解决,而不断变化的掩模图像,并使用 Xfermode 位图

Finally got the solution while changing mask image and using of Xfermode with Bitmap

面膜

 ImageView mImageView= (ImageView)findViewById(R.id.imageview_id);
 Bitmap original = BitmapFactory.decodeResource(getResources(),R.drawable.content_image);
 Bitmap mask = BitmapFactory.decodeResource(getResources(),R.drawable.mask);
 Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
 Canvas mCanvas = new Canvas(result);
 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
 mCanvas.drawBitmap(original, 0, 0, null);
 mCanvas.drawBitmap(mask, 0, 0, paint);
 paint.setXfermode(null);
 mImageView.setImageBitmap(result);
 mImageView.setScaleType(ScaleType.CENTER);
 mImageView.setBackgroundResource(R.drawable.background_frame);

见输出

来源可以发现的 这里

Source can be found here