I have a menu, each item inside is a Framelayout, which is contain ImageView and TextView inside, the output looks like this screenshot:
RoundedCornerLayout.java is my class which extend FrameLayout to redraw it circular:
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);
}
}
Activity code:
<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>
what I got here is rounding the FrameLayout including all controls inside, but my problem is that rounding is not smooth as I expected!
Any idea how to edit the above code to get smooth circular FrameLayout ?
Please note that rounding through android:background="@drawable/circularlayout.xml didn't make the rounding over ImageView and TextView without RoundedCornerLayout.java