绘制人脸形状

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;


public class FaceGraphicView extends View {

    private Point leftTop;
    private Point rightTop;

    private Point leftBottom;
    private Point rightBottom;
    private Point startPoint;
    private Point endPoint;
    private int screenWidth;
    private int screenHeight;

    private Paint paintBezier;
    private Paint paintLine;

    public FaceGraphicView(Context context) {
        this(context, null);
    }

    public FaceGraphicView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FaceGraphicView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setBackgroundColor(0x33000000);
        if (android.os.Build.VERSION.SDK_INT >= 11) {
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
        paintBezier = new Paint();
        paintBezier.setStyle(Paint.Style.FILL);
        paintBezier.setColor(Color.TRANSPARENT);
        paintBezier.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

        paintLine = new Paint();
        paintLine.setStyle(Paint.Style.STROKE);
        paintLine.setColor(0xFF1D77EC);
        paintLine.setStrokeWidth(3);

        DisplayMetrics dm = context.getResources()
                .getDisplayMetrics();
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;

        int leftSpace = (int) (screenWidth / 6.5f);
        leftTop = new Point(leftSpace, (int) (leftSpace * 0.5f));
        rightTop = new Point(screenWidth - leftSpace, leftTop.y);
        startPoint = new Point(leftTop.x, (int) (leftSpace * 3));
        endPoint = new Point(rightTop.x, startPoint.y);
        leftBottom = new Point(leftTop.x, (int) (startPoint.y * 2.5f));
        rightBottom = new Point(rightTop.x, leftBottom.y);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 回执
        Path path = new Path();
        path.moveTo(startPoint.x, startPoint.y);
        path.cubicTo(leftTop.x, leftTop.y, rightTop.x, rightTop.y, endPoint.x, endPoint.y);
        //绘制透明层
        canvas.drawPath(path, paintBezier);
        // 回执线条
        canvas.drawPath(path, paintLine);

        Path path1 = new Path();
        path1.moveTo(startPoint.x, startPoint.y);
        path1.cubicTo(leftBottom.x, leftBottom.y, rightBottom.x, rightBottom.y, endPoint.x, endPoint.y);
        //绘制路径
        canvas.drawPath(path1, paintBezier);
        // 回执线条
        canvas.drawPath(path1, paintLine);
    }

}

 

转载于:https://www.cnblogs.com/xxaing/p/7324919.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值