Android - displaying ExoPlayer in a circle

I am trying to display an ExoPlayerView inside a circle by overlaying another ExoPlayer (picture in picture): enter image description here

I tried to put the second player inside the frame with rounded corners (both this answer and this one ), but the player will always leave the parent frame and draw a full rectangle of the video.

I found this solution that uses GLSurfaceView, however this solution uses the classic MediaPlayer, not ExoPlayer.

+7
source share
2 answers

You need to create a custom container for it. try this and put the player in it.

public class RoundFrameLayout extends FrameLayout {

private final Path clip = new Path();

private int posX;
private int posY;
private int radius;

public RoundFrameLayout(Context context) {
    this(context,null);

}

public RoundFrameLayout(Context context, AttributeSet attrs) {
    this(context, attrs,0);
}

public RoundFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    // We can use outlines on 21 and up for anti-aliased clipping.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        setClipToOutline(true);
    }
}

@Override
protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
    posX = Math.round((float) width / 2);
    posY = Math.round((float) height / 2);

    // noinspection NumericCastThatLosesPrecision
    radius = (int) Math.floor((float) Math.min(width, height) / 2);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        setOutlineProvider(new OutlineProvider(posX, posY, radius));
    } else {
        clip.reset();
        clip.addCircle(posX, posY, radius, Direction.CW);
    }
}

@Override
protected void dispatchDraw(Canvas canvas) {
    // Not needed on 21 and up since we're clipping to the outline instead.
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        canvas.clipPath(clip);
    }

    super.dispatchDraw(canvas);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    // Don't pass touch events that occur outside of our clip to the children.
    float distanceX = Math.abs(event.getX() - posX);
    float distanceY = Math.abs(event.getY() - posY);
    double distance = Math.hypot(distanceX, distanceY);

    return distance > radius;
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
static class OutlineProvider extends ViewOutlineProvider {

    final int left;
    final int top;
    final int right;
    final int bottom;

    OutlineProvider(int posX, int posY, int radius) {
        left = posX - radius;
        top = posY - radius;
        right = posX + radius;
        bottom = posY + radius;
    }

    @Override
    public void getOutline(View view, Outline outline) {
        outline.setOval(left, top, right, bottom);
    }

}

}

+1

, , XML :

app:surface_type="texture_view"

.

- ( ):

SurfaceView TextureView? SurfaceView TextureView :

. , . , DRM. SurfaceView , TextureView, . TextureView , SurfaceView . - Android N (. / ?). TextureView , SDK_INT 24 (Android N) SurfaceView .

0

Source: https://habr.com/ru/post/1017361/