Android-动态蒙版形状 [英] Android - Dynamic Mask Shape
问题描述
我正在尝试为我的android项目实现图像的遮罩.我正在尝试完成与下图相同的操作:
I am trying to implement Masking of an image for my android project. I am trying to accomplish the same as below image :
,输出将与以下链接相同:
and the output would be the same as below link:
下面的链接与我要实现的目标很接近,只是我需要在运行时创建蒙版并处理onTouchListener来绘制蒙版,但是如何?
The link below is close to what I want to achieve only that I need to create the mask at runtime and handle onTouchListener to draw the mask but how?
我无法弄清楚自己,因此我陷入了这个问题.我们非常感谢您提供的任何帮助或教程.
I can't figure out myself and I feel stuck at this problem. Any help or tutorials that will help is highly appreciated.
推荐答案
尽管我没有写完整的解决方案,但它可能会向您显示正确的方向.
Although I didn't write a complete solution it could show you right direction.
package com.example.masktest.app;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class MaskDrawingView extends View {
private Path path = new Path();
private Paint pathPaint = new Paint();
public MaskDrawingView(Context context) {
super(context);
init();
}
public MaskDrawingView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MaskDrawingView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
pathPaint.setStyle(Paint.Style.STROKE);
pathPaint.setAlpha(150);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path, pathPaint);
invalidate();
}
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.lineTo(event.getX(), event.getY());
break;
}
return super.onTouchEvent(event);
}
public Bitmap finishDrawingAndGetMask() {
pathPaint.setStyle(Paint.Style.FILL);
path.close();
setDrawingCacheEnabled(true);
Bitmap result = Bitmap.createBitmap(getDrawingCache());
setDrawingCacheEnabled(false);
path.reset();
return result;
}
}
这篇关于Android-动态蒙版形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!