簡體   English   中英

繪制帶圓角的LinearLayout

[英]Drawing LinearLayout with rounded corners

我正在嘗試實現一個使用圓角繪制自己的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示例),但應該是透明的區域(圓形的)具有黑色背景,並且子項的角仍然可見。 這是使用Android 4.2.2的Galaxy Nexus的結果:

例

任何提示?

編輯:這是我想要實現的,抱歉photoshopping的粗糙:)

在此輸入圖像描述

編輯2:我向GitHub添加了一個可運行的示例項目: https//github.com/venator85/RoundClippingLayout

謝謝 ;)

不太一樣:Romain Guy做了一篇關於使用位圖着色器裁剪圖像邊角的博文。不確定是否要擴展同樣的東西。

http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/

試試這個,

布局:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout 
        android:id="@+id/linearLayout"
        android:layout_width="300dp"
        android:gravity="center"
        android:layout_height="300dp"
        android:layout_centerInParent="true"
        android:background="@drawable/rounded_edge">
        <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="foo" />
    </LinearLayout>
</RelativeLayout>

形狀(可繪制): - rounded_edge.xml

<shape 
        xmlns:android="http://schemas.android.com/apk/res/android">
            <solid 
                android:color="@android:color/darker_gray">
            </solid>
            <stroke 
                 android:width="0dp" 
                 android:color="#424242">
            </stroke>
            <corners 
                 android:topLeftRadius="100dip"
                 android:topRightRadius="100dip"
                 android:bottomLeftRadius="100dip"
                 android:bottomRightRadius="100dip">
            </corners>
        </shape>

我可以像這樣實現帶圓角的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.像這樣在主布局中使用它

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#336699" >

    <com.example.rounded.RoundedLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:padding="30dp"
        android:background="@android:color/transparent" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="foo" />

    </com.example.rounded.RoundedLayout>    

</LinearLayout>

嘗試這個 !! 取自這篇文章

將以下內容添加到文件(例如customshape.xml)中,然后將其放入(res / drawable / customshape.xml)

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
 android:shape="rectangle"> 
 <gradient 
     android:startColor="#SomeGradientBeginColor"
     android:endColor="#SomeGradientEndColor" 
     android:angle="270"/> 

<corners 
     android:bottomRightRadius="7dp" 
     android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" 
     android:topRightRadius="7dp"/> 
</shape> 

完成創建此文件后,只需使用以下方法之一設置背景:

通過代碼:

yourObject.setBackgroundResource(R.drawable.customshape);

或者通過XML,只需將以下屬性添加到容器中(例如:LinearLayout或任何字段):

android:background="@drawable/customshape"

怎么樣...

myLayout.setBackgroundResource(R.drawable.my_rounded_drawable);

然后...

my_rounded_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="#FFFFFFFF" />
            <stroke android:width="1dip" android:color="#FF000000" />
            <corners android:radius="10dp" />
        </shape>
    </item>
</selector>

為什么不把Drawable放在它上面作為一種與你的背景顏色相匹配的框架?

[編輯:看起來這將添加到L: https//developer.android.com/preview/material/views-shadows.html#clip ,它允許您將視圖剪切為矩形,圓形,或圓形矩形可繪制。]

我只是嘗試了很長時間自己這樣做,並且發現這個答案表明它是不可能的,因為View類基於Rect類。 我剛檢查了源代碼,從評論中看起來仍然如此。

摩托羅拉將在今年夏天晚些時候發布Moto 360(帶有圓臉的Android Wear手表),因此可能會對框架進行更新,允許視圖使用矩形以外的形狀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM