我试图实现的LinearLayout子,吸引了本身带有圆角。 从我的研究,我设置setWillNotDraw(false)和重写onDraw()画在画布圆角矩形:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int sc = canvas.saveLayer(0, 0, getWidth(), getHeight(), drawPaint, Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
| Canvas.FULL_COLOR_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
canvas.drawRoundRect(bounds, mCornerRadius, mCornerRadius, roundPaint);
canvas.restoreToCount(sc);
}
哪里:
drawPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
drawPaint.setColor(0xffffffff);
drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
roundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
roundPaint.setColor(0xffffffff);
DST_IN似乎这里的正确选项(根据APIDemos例子),但应该是透明的区域(圆形的),而不是有一个黑色的背景,和孩子们的角落仍然可见。 这是对的Galaxy Nexus采用Android 4.2.2结果:
任何提示?
编辑:这里是希望我来实现的,对不起使用Photoshop的粗陋:)
编辑2:我加入GitHub的一个例子可运行的项目: https://github.com/venator85/RoundClippingLayout
谢谢 ;)
Answer 1:
不太一样:罗曼盖伊做了一个博客张贴关于使用位图图像着色器裁剪角落..不知道,如果你想延长同样的事情。
http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/
Answer 2:
试试这个,
布局:-
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/linearLayout"
android:layout_width="300dp"
android:gravity="center"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:background="@drawable/rounded_edge">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foo" />
形状(可绘制): - rounded_edge.xml
xmlns:android="http://schemas.android.com/apk/res/android">
android:color="@android:color/darker_gray">
android:width="0dp"
android:color="#424242">
android:topLeftRadius="100dip"
android:topRightRadius="100dip"
android:bottomLeftRadius="100dip"
android:bottomRightRadius="100dip">
Answer 3:
我可以实现与圆角这样的LinearLayout中。
步骤 :
1.创建一个自定义布局
public class RoundedLayout extends LinearLayout {
private RectF rect;
private Paint paint;
public RoundedLayout(Context context) {
super(context);
init();
}
public RoundedLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
rect = new RectF(0.0f, 0.0f, getWidth(), getHeight());
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.parseColor("#7EB5D6"));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRoundRect(rect, 20, 20, paint);
}
}
2.使用它的主要布局是这样
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#336699" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="30dp"
android:background="@android:color/transparent" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foo" />
Answer 4:
试试这个 !! 从其中取出后
添加以下到一个文件中(比如customshape.xml),然后将其放置在(RES /绘制/ customshape.xml)
android:shape="rectangle">
android:startColor="#SomeGradientBeginColor"
android:endColor="#SomeGradientEndColor"
android:angle="270"/>
android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
一旦你完成了创建该文件的完成,只需设置背景中的下列方式之一:
通过代码:
yourObject.setBackgroundResource(R.drawable.customshape);
或通过XML,只需添加下面的属性到容器(例如:LinearLayout中或任何字段):
android:background="@drawable/customshape"
Answer 5:
怎么样...
myLayout.setBackgroundResource(R.drawable.my_rounded_drawable);
然后...
my_rounded_drawable.xml
Answer 6:
而不是试图切断你的布局角落,为什么不放在它上面可拉伸作为一种框架,你的背景颜色相匹配?
Answer 7:
[编辑:看起来像这样将以L被添加: https://developer.android.com/preview/material/views-shadows.html#clip ,它允许您剪辑的视图到型矩形的形状,圆形,或圆形的矩形绘制。]
我只是尝试了很长一段时间来这做自己,在来到这个答案是表明它是不可能的,因为View类是基于矩形类。 我只是检查源,并从评论这看起来仍然是这样。
摩托罗拉在今年夏天晚些时候发布的摩托罗拉360(Android Wear手表圆面),所以也许会有更新的框架,允许与形状并非矩形意见。
文章来源: Drawing LinearLayout with rounded corners