【问题标题】:Adaptative radius on CardViewCardView 上的自适应半径
【发布时间】:2019-06-18 02:30:20
【问题描述】:

我有一个问题,希望你能给我一些信息。 为了有一个圆形 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() 依赖它,但它并不完美,我认为这不是正确的方法。

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

【问题讨论】:

标签: java android layout view radius


【解决方案1】:

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

在你的项目中添加这个类

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>

参考:12

【讨论】:

  • 谢谢@beigirad 它运作良好,但有没有办法隐藏/最小化圆形视图后面的这个白色矩形?不幸的是,我的背景不是纯色,实际上它是可绘制的
  • 太棒了!只需在第 64 行替换 Color.TRANSPARENT 而不是 Color.WHITE
  • 它不起作用,如果我用 Color.TRANSPARENT 替换 Color.WHITE,我的 VideoView 是一个矩形:/
猜你喜欢
  • 2015-06-03
  • 1970-01-01
  • 2020-10-06
  • 2021-05-29
  • 2017-07-05
  • 1970-01-01
  • 1970-01-01
  • 2022-10-17
  • 2023-04-01
相关资源
最近更新 更多