简体   繁体   English

具有旋转和缩放功能的Android图像蒙版

[英]Android Image Masking with Rotate & Scale

How to mask image such as we can scale/zoom/rotate the background image but not the mask? 如何遮罩图像,例如我们可以缩放/缩放/旋转背景图像,但不能遮罩?

面具

I should be able to zoom the image in this mask but it is scaling whole image. 我应该可以在此蒙版中缩放图像,但是它可以缩放整个图像。

Suggest me a way to achieve this, I'm creating a photo collage app. 建议我一种实现此目的的方法,我正在创建一个照片拼贴应用程序。

大学

Blue color is background of Layout. 蓝色是布局的背景。

The white color is for mask 白色是面膜

I'm able to achieve this type of layout with masking, but when I apply MultiTouchListener to scale and zoom, it will scale the whole image with mask and not the image inside it. 我可以通过遮罩实现这种布局,但是当我应用MultiTouchListener缩放和缩放时,它将使用遮罩而不是其中的图像缩放整个图像。

private void getMaskedBitmap() {

    Bitmap bgBitmap =  BitmapFactory.decodeResource(getResources(), R.drawable.background_drawable);
    ImageView bg = (ImageView) findViewById(R.id.bg);
    bg.setImageBitmap(bgBitmap);

    Bitmap.Config conf = Bitmap.Config.ARGB_8888; 
    Bitmap emptyBitmap = Bitmap.createBitmap(bgBitmap.getWidth(), bgBitmap.getHeight(), conf);
    Canvas canvasBmp = new Canvas(bgBitmap);


    ImageView mImageView = (ImageView) findViewById(R.id.troll_face);

    Bitmap original = BitmapFactory.decodeResource(getResources(), R.drawable.random_drawable);

    Bitmap mask = BitmapFactory.decodeResource(getResources(), R.drawable.mask_drawable);

    original = Bitmap.createScaledBitmap(original, mask.getWidth(), mask.getHeight(), true);

    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.FIT_XY);
    mImageView.setBackgroundResource(R.drawable.background_drawable);

    bg.setOnTouchListener(new MultiTouchListener());

    mImageView.invalidate();


}

Exception on line 在线异常

Canvas canvasBmp = new Canvas(bgBitmap); Canvas canvasBmp =新的Canvas(bgBitmap);

java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor java.lang.IllegalStateException:不变的位图传递给Canvas构造函数

I cannot provide you code but here is what you should do: 我无法为您提供代码,但是您应该执行以下操作:

  1. Get background image as bitmap and draw it on view's canvas. 将背景图像获取为位图并将其绘制在视图的画布上。

  2. Create an new (empty) bitmap of same size (use Bitmap.Config.ARGB_8888) and get its canvas, here you will draw mask. 创建一个相同大小的新(空)位图(使用Bitmap.Config.ARGB_8888)并获取其画布,在这里您将绘制蒙版。

    Canvas canvasBmp = new Canvas(bmp); Canvas canvasBmp =新的Canvas(bmp);

  3. Now draw bmp on view's canvas. 现在在视图的画布上绘制bmp。 So, that you can get mask effect on image. 因此,您可以在图像上获得蒙版效果。

  4. Zoom (re-size etc whatever you want) background bitmap image. 缩放(调整大小等等)背景位图图像。

  5. Invalidate your view, and follow all steps again 使您的视图无效,然后再次执行所有步骤

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM