VideoView - Android 角球

Corner for VideoView - Android

我正在为 Android mobiles 开发应用程序。我想播放视频。它工作正常。现在我有一个问题。我想为 VideoView 设置角。 VideoView可以设置角吗?

我使用以下 xml 代码:

<RelativeLayout 
        android:id="@+id/video_relative"
android:layout_width="match_parent"
android:layout_height="match_parent"

>

    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

    android:layout_alignParentTop="true"
     android:layout_alignParentBottom="true"
     android:layout_alignParentLeft="true"
     android:layout_alignParentRight="true"

        />

    </RelativeLayout>

在我的 java 代码中:

myVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.my_video));



 myVideoView.setOnPreparedListener(new OnPreparedListener() {

            public void onPrepared(MediaPlayer mediaPlayer) {
                // close the progress bar and play the video
//                progressDialog.dismiss();
                //if we have a position on savedInstanceState, the video playback should start from here
                myVideoView.seekTo(position);
                if (position == 0) {
                    myVideoView.start();

                } 

                else {
                    //if we come from a resumed activity, video playback will be paused
                    myVideoView.pause();
                }
            }
        });

请有人帮我解决这个问题。

我建议设置自定义背景可绘制对象。我的示例在底部添加了带阴影的圆角背景。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape
        android:dither="true"
        android:shape="rectangle">
        <corners android:radius="2dp"/>

        <solid android:color="@color/black_alpha_08"/>
    </shape>
  </item>
  <item android:bottom="2dp">
    <shape
        android:dither="true"
        android:shape="rectangle">
        <corners android:radius="2dp"/>

        <solid android:color="@color/grey_02"/>
        <stroke
            android:width="0.5dp" android:color="@color/grey_30"/>

        <padding android:bottom="2dp"/>
    </shape>
  </item>
</layer-list>

当然,您需要定义自己的颜色 ;)

尝试使用定义所需形状的背景可绘制对象。这是一个简单的形状,边框半径为 6dp。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke android:width="0dp" android:color="#00FFFFFF" />
    <solid android:color="#AA3377AA"/>
    <corners android:radius="6dp" />
    <padding android:left="0dp" android:top="0dp"
            android:right="0dp" android:bottom="0dp" />
</shape> 

啊,你也可以这样使用它:

android:background="@drawable/rounded_border"

编辑

另一个选项(使用图像视图完成,但也许您可以调整它以使用视频)。不过我不确定性能。

public class RoundedImageView extends ImageView {
    private final Paint restorePaint = new Paint();
    private final Paint maskXferPaint = new Paint();
    private final Paint canvasPaint = new Paint();

    private final Rect bounds = new Rect();
    private final RectF boundsf = new RectF();

    public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public RoundedImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RoundedImageView(Context context) {
        super(context);
        init();
    }

    private void init() {
        canvasPaint.setAntiAlias(true);
        canvasPaint.setColor(Color.argb(255, 255, 255, 255));
        restorePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
        maskXferPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.getClipBounds(bounds);
        boundsf.set(bounds);

        canvas.saveLayer(boundsf, restorePaint, Canvas.ALL_SAVE_FLAG);
        super.onDraw(canvas);

        canvas.saveLayer(boundsf, maskXferPaint, Canvas.ALL_SAVE_FLAG);
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawRoundRect(boundsf, 6, 6, canvasPaint);

        canvas.restore();
        canvas.restore();
    }
}

我为 videoview 设角成功。我的解决方案很简单。我只是为视频视图设置了背景。背景图像是 9-patch 图像,只有角中心的颜色是透明的。我不知道它是否是干净的解决方案,但它解决了我的问题。