0

I am developing an application for Android mobiles. i want to play an video. It's working fine. Now i have one issue. i want to set the corner for VideoView. Is it possible to set the corner for VideoView?

I using following xml code:

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

And in my java code:

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

Please someone help me to solve this issue.

Amsheer
  • 7,046
  • 8
  • 47
  • 81
  • I solved it following this approach: [https://stackoverflow.com/questions/5574212/android-view-clipping](https://stackoverflow.com/questions/5574212/android-view-clipping) – Aaron Dec 02 '17 at 19:58

3 Answers3

0

I would suggest to set a custom background drawable. My sample adds a rounded corner background with shadow at the bottom.

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

And of course, you need to define your own colors ;)

Thomas R.
  • 7,988
  • 3
  • 30
  • 39
  • I try this code and set it as a background like this android:background="@drawable/stack". But it is not working. Still playing rectangle video. (stack is my xml name) – Amsheer Oct 27 '15 at 10:10
  • @Amsheer have you tried with padding of 0dp as in my example? If it's not working I remember that I've used some class to do rounded ImageView border with another approach so I might post it for you to try with the video. – kirotab Oct 27 '15 at 10:17
  • Corner for imageview will work. My problem is videoview. – Amsheer Oct 27 '15 at 10:22
  • @Amsheer I know but I thought you get the radius (which is behind the image/video) but the video itself is still rectangular. That's why I suggested to use padding 0 instead of 2. So if you don't get the rounded borders at all then this approach with shape is not working. – kirotab Oct 27 '15 at 10:26
0

Try using background drawable defining the shape you need. This is a simple shape with 6dp for border radius.

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

Ah and also you could use it like this:

android:background="@drawable/rounded_border"

EDIT

Another option (done with image view, but maybe you could adapt it to work with video). I'm not sure about performance though.

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();
    }
}
kirotab
  • 1,296
  • 1
  • 11
  • 18
  • I tried your answer too. It is making the corner but video still in rectangle shape. – Amsheer Oct 27 '15 at 10:21
  • @Amsheer Please check the one after the edit, I think VideoView has the same draw method so it should be easy to test at least. – kirotab Oct 27 '15 at 11:36
0

I set corner for videoview successfully. My solution is simple. I just set the background for video view. The background image is 9-patch image only color at the corner center is just transparent. I don't know it is clean solution or not but it solved my issue.

Amsheer
  • 7,046
  • 8
  • 47
  • 81