自定义向下凹入的布局

效果如下有一个向内凹入的效果
截图.png

实现思路:用贝塞尔曲线画出弧形,再填充空白部分,代码如下

package com.zjh.baseutillib.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.View;

import io.reactivex.annotations.Nullable;

/**
 * 向内凹的布局
 * Created by ZJH
 * on 2019-05-21
 */
public class BalanceView extends View {

    private Paint mPaint;//画笔
    private Path mPath;//路径

    private PointF start, end, control;
    private float width, height;

    public BalanceView(Context context) {
        super(context);
        initData();
    }

    public BalanceView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initData();
    }

    public BalanceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initData();
    }

    private void initData() {
        mPath = new Path();
        mPaint = new Paint();
        //抗锯齿
        mPaint.setAntiAlias(true);
        //画笔颜色
        mPaint.setColor(Color.WHITE);
        //画笔模式填充
        mPaint.setStyle(Paint.Style.FILL);

        start = new PointF(0, 0);
        end = new PointF(0, 0);
        control = new PointF(0, 0);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        int centerX = w / 2;
        width = w;
        height = h;

        // 初始化数据点和控制点的位置
        start.x = 0;
        start.y = 0;
        end.x = w;
        end.y = 0;
        control.x = centerX;
        control.y = 80;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 绘制贝塞尔曲线
        mPath.moveTo(start.x, start.y);
        mPath.quadTo(control.x, control.y, end.x, end.y);
        //填充剩余部分
        mPath.lineTo(width, height);
        mPath.lineTo(0, height);

        canvas.drawPath(mPath, mPaint);
    }
}



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转眼已经18年底了,在Androd这行已经混了3年,一直说写些东西,总是没有执行。有想法的时候没时间,有时间的时候...
    茫茫前路一行者阅读 1,022评论 0 0
  • 系列文章之 Android中自定义View(一)系列文章之 Android中自定义View(二)系列文章之 And...
    YoungerDev阅读 4,477评论 3 11
  • 有人喜问你的年龄,有人喜问你的容颜,有人喜问你的风骨,而我却只懂将你陪伴…… 像陪伴清晨在湿雾中逐渐透...
    茶朵张涵阅读 979评论 0 4
  • 前不久,国乒的另一位天才少年樊振东拿到了自己职业生涯的第一个单打世界冠军,15天3个冠军,这不由的让我想起当年“六...
    山东工商学院阅读 166评论 0 0
  • 很容易被捕获阿我 也很容易快速逃脱 很容易感动的掉泪 也很容易决绝离开 天空什么都没有 却给我最大的安慰 而你拥有...
    dew清澈阅读 186评论 0 0