我正试图在android中创建一个圆角的视图。到目前为止,我找到的解决方案是定义带有圆角的形状,并使用它作为视图的背景。
下面是我所做的,定义了一个可绘制的图,如下所示:
<padding
android:top="2dp"
android:bottom="2dp"/>
<corners android:bottomRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp"/>
现在,我使用它作为布局的背景,如下所示:
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:clipChildren="true"
android:background="@drawable/rounded_corner">这是非常好的工作,我可以看到,视图有圆形的边缘。
但是我的布局中还有许多其他子视图,比如ImageView或MapView。当我在上面的布局中放置一个ImageView时,图像的角不是剪裁/裁剪的,而是显示为满的。
我已经看到了其他的解决办法,让它像这里解释的那样工作。
但是,是否有方法为视图设置圆角,其所有子视图都包含在具有圆角的主视图中?
发布于 2016-12-12 10:26:21
但是,雅普·范恒斯塔姆的回答工作得很好,但是我认为它很昂贵,例如,如果我们在Button上应用这个方法,就会失去触摸效果,因为视图被呈现为位图。
对我来说,最好和最简单的方法是在视图上应用一个掩码,如下所示:
@Override
protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
super.onSizeChanged(width, height, oldWidth, oldHeight);
float cornerRadius = <whatever_you_want>;
this.path = new Path();
this.path.addRoundRect(new RectF(0, 0, width, height), cornerRadius, cornerRadius, Path.Direction.CW);
}
@Override
protected void dispatchDraw(Canvas canvas) {
if (this.path != null) {
canvas.clipPath(this.path);
}
super.dispatchDraw(canvas);
}https://stackoverflow.com/questions/26074784
复制相似问题