Android - Creating a circular mask on video

前端 未结 2 562
不思量自难忘°
不思量自难忘° 2020-12-16 01:40

I\'m relatively new to Android development, and I was wondering if it\'s possible to mask a VideoView into a shape. This is what I have so far:

Expected Result

相关标签:
2条回答
  • 2020-12-16 01:51

    I've done something like this mask before. you can get what you want as following steps
    1) prepare a png image with the shape you want and fill it with color 0x0000000
    2) use a blank layout and make sure it covers the VideView
    3) now, all touchevents are captured by this blank layout
    4) judge the points' colors which are contained in TouchEvent, if the color is 0x00000000 then pass the event to VideView

    here is a example to get a point's color, and it runs efficently:

    // build a drawingCache and draw the mask layer to the bitmap
            Bitmap drawingCache = Bitmap.createBitmap(getWidth(), getHeight(),Bitmap.Config.ARGB_4444);
            Canvas drawingCacheCanvas = new Canvas(drawingCache);
            drawingCacheCanvas.clipRect(x, y, x + 1, y + 1); 
            draw(drawingCacheCanvas);
    
            int color = drawingCache.getPixel(x, y);
    
            if (color == getMaskColor()) { 
                //TODO dispatch event to VideoView and let it to handle event
            }
    
    0 讨论(0)
  • 2020-12-16 02:13

    Turns out it is possible to clip a video into a circle. What you're going to want to do is create your own SurfaceView class and override dispatchDraw from here you can call canvas.clipPath and pass in a Path object that contains the circle you want the video to be masked to.

    Here's the view:

    public class CircleSurface extends SurfaceView {
    
        private Path clipPath;
    
        public CircleSurface(Context context) {
            super(context);
            init();
        }
    
        public CircleSurface(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public CircleSurface(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        private void init() {
            clipPath = new Path();
            //TODO: define the circle you actually want
            clipPath.addCircle(710, 330, 250, Path.Direction.CW);
        }
    
        @Override
        protected void dispatchDraw(Canvas canvas) {
            canvas.clipPath(clipPath);
            super.dispatchDraw(canvas);
        }
    }
    

    Here's what the activity might look like

    public class MainActivity extends Activity implements SurfaceHolder.Callback {
    
        CircleSurface surface;
        MediaPlayer player;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            surface = (CircleSurface) findViewById(R.id.surface);
            SurfaceHolder holder = surface.getHolder();
            holder.addCallback(this);
    
            player = MediaPlayer.create(this, R.raw.yourvideo);
        }
    
    
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            player.setDisplay(holder);
            player.start();
        }
    
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            //TODO: handle this
        }
    
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            //TODO: handle this
        }
    }
    
    0 讨论(0)
提交回复
热议问题