如何在Android中获得平滑的圆形FrameLayout

时间:2017-12-05 06:13:50

标签: java android android-framelayout

我有一个菜单,里面的每个项目都是一个Framelayout,里面包含ImageView和TextView,输出看起来像这个截图:

enter image description here

RoundedCornerLayout.java是我的类,它扩展了FrameLayout以重绘它循环:

public class RoundedCornerLayout extends FrameLayout {



int borderColor;
Paint paint;
private Path path = new Path();

public RoundedCornerLayout(Context context, AttributeSet attrs) {
    super(context, attrs);


    borderColor = android.graphics.Color.rgb(128,0,129);

    paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(borderColor);
    paint.setAntiAlias(true);
    paint.setStrokeWidth(8);
    paint.setStyle(Paint.Style.STROKE);
    paint.setFlags(Paint.ANTI_ALIAS_FLAG);


}

@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.drawPath(path, paint);
    canvas.restoreToCount(save);

}

}

活动代码:

<RoundedCornerLayout
        android:id="@+id/viewClicked"
        android:layout_width="110dp"
        android:layout_height="110dp"
        android:layout_marginRight="151dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="50dp">

        <ImageView
            android:id="@+id/imgNature"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/pic_nature" />

        <TextView
            android:id="@+id/lblClick"
            android:layout_width="match_parent"
            android:layout_height="39dp"
            android:layout_marginTop="75dp"
            android:background="#CC800081"
            android:text="click"
            android:textAlignment="center"
            android:textColor="#ffffff" />
    </RoundedCornerLayout>

我在这里得到的是对FrameLayout进行舍入,包括内部的所有控件,但我的问题是舍入并不像我预期的那样平滑!

知道如何编辑上面的代码以获得平滑的圆形FrameLayout吗?

请注意,通过android:background =“@ drawable / circularlayout.xml进行舍入没有使得没有RoundedCornerLayout.java的ImageView和TextView的舍入

0 个答案:

没有答案