如何创建曲线对话框布局?

huangapple go评论79阅读模式
英文:

how to create curved dialog layout?

问题

我想为对话框创建类似于这个的弯曲布局,我在一个个人博客上找到了这张用于设计的图片。如何创建曲线对话框布局?

我在网上搜索了但什么都没找到,我想创建完全相同的布局。我知道如何使用可绘制对象创建圆形,但我不知道如何创建位于此卡片中间的那个弯曲部分。

提前谢谢您。

英文:

i want to create curved layout for dialog like this one , i found this image on personal blog for designing . 如何创建曲线对话框布局?

i searched on web but found nothing , i want to create exact same layout . i know how to create round circle with drawable , i don't have idea how can i create that curve that is in middle of this card .

Thank You in advance

答案1

得分: 1

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_gravity="center"
    android:background="#8F8F8F"
    tools:context=".MainActivity">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginBottom="10dp"
        android:clickable="true"
        android:focusable="true"
        app:elevation="4dp"
        app:layout_anchor="@id/customBottomBar"
        app:layout_anchorGravity="fill_vertical|center_horizontal"
        app:rippleColor="#fff" />

    <app.example.CurvedView
        android:id="@+id/customBottomBar"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_margin="10dp"
        android:background="@drawable/rounded_background">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:text="Call With"
            android:textColor="#000"
            android:textSize="18sp" />
    </app.example.CurvedView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
public class CurvedView extends RelativeLayout {
    private Path mPath;
    private Paint mPaint;

    private Point mFirstCurveStartPoint = new Point();
    private Point mFirstCurveEndPoint = new Point();
    private Point mFirstCurveControlPoint1 = new Point();
    private Point mFirstCurveControlPoint2 = new Point();

    private Point mSecondCurveEndPoint = new Point();
    private Point mSecondCurveControlPoint1 = new Point();
    private Point mSecondCurveControlPoint2 = new Point();

    public CurvedView(Context context) {
        super(context);
        init();
    }

    public CurvedView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CurvedView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPath = new Path();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setColor(Color.WHITE);
        setBackgroundColor(Color.TRANSPARENT);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        int mNavigationBarWidth = getWidth();
        int mNavigationBarHeight = getHeight();
        int CURVE_CIRCLE_RADIUS = 256 / 2;
        // Initialize the curve coordinates and control points here
        // ...

        mPath.reset();
        mPath.moveTo(0, 0);
        // Construct the path using cubic Bezier curves
        // ...

        mPath.lineTo(mNavigationBarWidth, 0);
        mPath.lineTo(mNavigationBarWidth, mNavigationBarHeight);
        mPath.lineTo(0, mNavigationBarHeight);
        mPath.close();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawPath(mPath, mPaint);
    }
}
英文:

I think this is closer to what you want :

1 - The full article: How I drew custom shapes in bottom bar

如何创建曲线对话框布局?

2 - XML :

&lt;androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_centerInParent=&quot;true&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:layout_centerVertical=&quot;true&quot;
android:layout_gravity=&quot;center&quot;
android:background=&quot;#8F8F8F&quot;
tools:context=&quot;.MainActivity&quot;&gt;
&lt;com.google.android.material.floatingactionbutton.FloatingActionButton
android:id=&quot;@+id/fab&quot;
android:layout_width=&quot;50dp&quot;
android:layout_height=&quot;50dp&quot;
android:layout_marginBottom=&quot;10dp&quot;
android:clickable=&quot;true&quot;
android:focusable=&quot;true&quot;
app:elevation=&quot;4dp&quot;
app:layout_anchor=&quot;@id/customBottomBar&quot;
app:layout_anchorGravity=&quot;fill_vertical|center_horizontal&quot;
app:rippleColor=&quot;#fff&quot; /&gt;
&lt;app.example.CurvedView
android:id=&quot;@+id/customBottomBar&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;200dp&quot;
android:layout_margin=&quot;10dp&quot;
android:background=&quot;@drawable/rounded_background&quot;&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:layout_centerVertical=&quot;true&quot;
android:gravity=&quot;center&quot;
android:text=&quot;Call With&quot;
android:textColor=&quot;#000&quot;
android:textSize=&quot;18sp&quot; /&gt;
&lt;/app.example.CurvedView&gt;
&lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;

3 - JAVA :

public class CurvedView extends RelativeLayout {
private Path mPath;
private Paint mPaint;
// the coordinates of the first curve
private Point mFirstCurveStartPoint = new Point();
private Point mFirstCurveEndPoint = new Point();
private Point mFirstCurveControlPoint1 = new Point();
private Point mFirstCurveControlPoint2 = new Point();
private Point mSecondCurveEndPoint = new Point();
private Point mSecondCurveControlPoint1 = new Point();
private Point mSecondCurveControlPoint2 = new Point();
public CurvedView(Context context) {
super(context);
init();
}
public CurvedView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CurvedView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mPath = new Path();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setColor(Color.WHITE);
setBackgroundColor(Color.TRANSPARENT);
// setBackgroundResource(R.drawable.rounded_background);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// get width and height of navigation bar
// Navigation bar bounds (width &amp; height)
int mNavigationBarWidth = getWidth();
int mNavigationBarHeight = getHeight();
// the coordinates (x,y) of the start point before curve
/**
* the CURVE_CIRCLE_RADIUS represent the radius of the fab button
*/
int CURVE_CIRCLE_RADIUS = 256 / 2;
mFirstCurveStartPoint.set((mNavigationBarWidth / 2) - (CURVE_CIRCLE_RADIUS * 2) - (CURVE_CIRCLE_RADIUS / 3), 0);
// the coordinates (x,y) of the end point after curve
mFirstCurveEndPoint.set(mNavigationBarWidth / 2, CURVE_CIRCLE_RADIUS + (CURVE_CIRCLE_RADIUS / 4));
// same thing for the second curve
Point mSecondCurveStartPoint = mFirstCurveEndPoint;
mSecondCurveEndPoint.set((mNavigationBarWidth / 2) + (CURVE_CIRCLE_RADIUS * 2) + (CURVE_CIRCLE_RADIUS / 3), 0);
// the coordinates (x,y)  of the 1st control point on a cubic curve
mFirstCurveControlPoint1.set(mFirstCurveStartPoint.x + CURVE_CIRCLE_RADIUS + (CURVE_CIRCLE_RADIUS / 4), mFirstCurveStartPoint.y);
// the coordinates (x,y)  of the 2nd control point on a cubic curve
mFirstCurveControlPoint2.set(mFirstCurveEndPoint.x - (CURVE_CIRCLE_RADIUS * 2) + CURVE_CIRCLE_RADIUS, mFirstCurveEndPoint.y);
mSecondCurveControlPoint1.set(mSecondCurveStartPoint.x + (CURVE_CIRCLE_RADIUS * 2) - CURVE_CIRCLE_RADIUS, mSecondCurveStartPoint.y);
mSecondCurveControlPoint2.set(mSecondCurveEndPoint.x - (CURVE_CIRCLE_RADIUS + (CURVE_CIRCLE_RADIUS / 4)), mSecondCurveEndPoint.y);
mPath.reset();
mPath.moveTo(0, 0);
mPath.lineTo(mFirstCurveStartPoint.x, mFirstCurveStartPoint.y);
mPath.cubicTo(mFirstCurveControlPoint1.x, mFirstCurveControlPoint1.y,
mFirstCurveControlPoint2.x, mFirstCurveControlPoint2.y,
mFirstCurveEndPoint.x, mFirstCurveEndPoint.y);
mPath.cubicTo(mSecondCurveControlPoint1.x, mSecondCurveControlPoint1.y,
mSecondCurveControlPoint2.x, mSecondCurveControlPoint2.y,
mSecondCurveEndPoint.x, mSecondCurveEndPoint.y);
mPath.lineTo(mNavigationBarWidth, 0);
mPath.lineTo(mNavigationBarWidth, mNavigationBarHeight);
mPath.lineTo(0, mNavigationBarHeight);
mPath.close();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(mPath, mPaint);
}
}

huangapple
  • 本文由 发表于 2020年9月3日 19:56:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63723153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定