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 中,请参阅:

http://img4.mukewang.com/6287704b0001966b02860414.jpg

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

http://img1.mukewang.com/6287705a0001dd9604820485.jpg

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

我该怎么做才能找到适合平板电脑的良好拐角半径?谢谢


守着一只汪
浏览 87回答 1
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 {&nbsp; &nbsp; private Path path = new Path();&nbsp; &nbsp; public RoundedCornerLayout(Context context) {&nbsp; &nbsp; &nbsp; &nbsp; super(context);&nbsp; &nbsp; }&nbsp; &nbsp; public RoundedCornerLayout(Context context, AttributeSet attrs) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs);&nbsp; &nbsp; }&nbsp; &nbsp; public RoundedCornerLayout(Context context, AttributeSet attrs, int defStyle) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs, defStyle);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onSizeChanged(int w, int h, int oldw, int oldh) {&nbsp; &nbsp; &nbsp; &nbsp; super.onSizeChanged(w, h, oldw, oldh);&nbsp; &nbsp; &nbsp; &nbsp; // compute the path&nbsp; &nbsp; &nbsp; &nbsp; float halfWidth = w / 2f;&nbsp; &nbsp; &nbsp; &nbsp; float halfHeight = h / 2f;&nbsp; &nbsp; &nbsp; &nbsp; float centerX = halfWidth;&nbsp; &nbsp; &nbsp; &nbsp; float centerY = halfHeight;&nbsp; &nbsp; &nbsp; &nbsp; path.reset();&nbsp; &nbsp; &nbsp; &nbsp; path.addCircle(centerX, centerY, Math.min(halfWidth, halfHeight), Path.Direction.CW);&nbsp; &nbsp; &nbsp; &nbsp; path.close();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void dispatchDraw(Canvas canvas) {&nbsp; &nbsp; &nbsp; &nbsp; int save = canvas.save();&nbsp; &nbsp; &nbsp; &nbsp; canvas.clipPath(path);&nbsp; &nbsp; &nbsp; &nbsp; super.dispatchDraw(canvas);&nbsp; &nbsp; &nbsp; &nbsp; canvas.restoreToCount(save);&nbsp; &nbsp; }}并把你的VideoView里面。像这儿 :<com.example.myapplication.RoundedCornerLayout&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="100dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="100dp">&nbsp; &nbsp; // place your VideoView&nbsp;&nbsp; &nbsp; <ImageView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:src="@color/colorPrimary"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"/></com.example.myapplication.RoundedCornerLayout>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java