【问题标题】:Corner for VideoView - AndroidVideoView 的角落 - Android
【发布时间】:2016-01-26 15:28:49
【问题描述】:

我正在为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();
                }
            }
        });

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

【问题讨论】:

标签: android android-video-player


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    尝试使用背景可绘制来定义您需要的形状。这是一个边框半径为 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();
        }
    }
    

    【讨论】:

    • 我也试过你的答案。它正在制作角落,但视频仍然是矩形。
    • @Amsheer 请在编辑后检查一下,我认为 VideoView 具有相同的绘制方法,因此至少应该易于测试。
    【解决方案3】:

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

    <?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>
    

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

    【讨论】:

    • 我尝试了这段代码并将其设置为像这样的背景 android:background="@drawable/stack"。但它不起作用。仍在播放矩形视频。 (堆栈是我的 xml 名称)
    • @Amsheer 您是否尝试过像我的示例中那样使用 0dp 填充?如果它不起作用,我记得我使用了一些类来用另一种方法制作圆形 ImageView 边框,所以我可能会发布它供您尝试使用视频。
    • 图像视图的角落将起作用。我的问题是视频视图。
    • @Amsheer 我知道,但我认为你得到了半径(在图像/视频后面),但视频本身仍然是矩形。这就是为什么我建议使用填充 0 而不是 2。因此,如果您根本没有得到圆形边框,那么这种带有形状的方法是行不通的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    相关资源
    最近更新 更多