【问题标题】:Android - Dynamic Mask ShapeAndroid - 动态蒙版形状
【发布时间】:2015-12-05 12:56:15
【问题描述】:

我正在尝试为我的 android 项目实现图像蒙版。 我正在尝试完成与下图相同的操作:

输出与以下链接相同:

下面的链接接近我想要实现的,只是我需要在运行时创建遮罩并处理 onTouchListener 来绘制遮罩,但是如何?

Masking(crop) image in frame

我无法弄清楚自己,我觉得被这个问题困住了。 非常感谢任何有帮助的帮助或教程。

【问题讨论】:

    标签: android masking


    【解决方案1】:

    虽然我没有写出完整的解决方案,但它可以为您指明正确的方向。

    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;
        }
    }
    

    【讨论】:

    • 感谢您的回复,但当用户触摸与第一张图像相同的边缘时,我需要调整视图。
    • 在我的课堂上,您基本上可以使用触摸为您的图像创建蒙版。那么你只需要将这个蒙版应用到你的图像上,如果你需要其他的东西,你最好提供一个视频示例。
    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 2017-09-12
    • 2019-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多