Android - 在一个圆圈中显示ExoPlayer

The*_*ter 7 android exoplayer

我试图在一个圆圈内显示一个ExoPlayerView,覆盖另一个ExoPlayer(画中画): 在此输入图像描述

我已经尝试将第二个玩家放在一个带圆角的框架内(这个答案这一个)但是玩家将总是逃离父框架并绘制视频的完整矩形.

我发现这个解决方案使用了GLSurfaceView,但是这个解决方案使用的是经典的MediaPlayer而不是ExoPlayer.

小智 0

您需要为其创建一个自定义容器。尝试这个并将你的玩家视图放入其中。

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

}
Run Code Online (Sandbox Code Playgroud)

}