CardView 上的自适应半径

Adaptative radius on CardView

我有一个问题,希望你能给我一些信息。 为了有一个循环 VideoView ,我把它放在 CardView

<android.support.v7.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/cardVideo"
                    app:cardCornerRadius="180dp"
                    android:background="#000">

                    <com.twilio.video.VideoView
                        android:id="@+id/videoView"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:visibility="visible" />
                </android.support.v7.widget.CardView>

但问题是我在多个平板电脑上构建我的应用程序并且 cardCornerRadius 不适应屏幕尺寸,180dp 对于 8 英寸平板电脑来说太大了所以我的 VideoView 出现在 DIAMONDS 中看到:

例如在 10 英寸的平板电脑中它是一个完美的圆:

我尝试以编程方式获取设备英寸并使用 setRadius() 依赖于它,但它并不完美,我认为这不是正确的方法。

如何找到适合平板电脑的良好圆角半径?谢谢

好的,我找到了你的答案:

将此 class 添加到您的项目中

package com.example.myapplication;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.widget.FrameLayout;

public class RoundedCornerLayout extends FrameLayout {

    private Path path = new Path();

    public RoundedCornerLayout(Context context) {
        super(context);
    }

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

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

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        // compute the path
        float halfWidth = w / 2f;
        float halfHeight = h / 2f;
        float centerX = halfWidth;
        float centerY = halfHeight;
        path.reset();
        path.addCircle(centerX, centerY, Math.min(halfWidth, halfHeight), Path.Direction.CW);
        path.close();

    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        int save = canvas.save();
        canvas.clipPath(path);
        super.dispatchDraw(canvas);
        canvas.restoreToCount(save);
    }
}

并将你的 VideoView 放入其中。喜欢这里:

<com.example.myapplication.RoundedCornerLayout
        android:layout_width="100dp"
        android:layout_height="100dp">

    // place your VideoView 
    <ImageView
            android:layout_width="match_parent"
            android:src="@color/colorPrimary"
            android:layout_height="match_parent"/>

</com.example.myapplication.RoundedCornerLayout>

参考资料:1 2