根据贝塞尔曲线编写一个弹性按钮

xz.gif
首先要了解两个Path的两个方法,这里使用了两次二次贝塞尔曲线

1.path.quadTo(x,y, a, b)绘制二次贝塞尔曲线,(x,y)为控制点

  1. path.cubicTo(x,y,x1,y1,a,b)绘制三次贝塞尔曲线,(x,y),(x1,y1)为控制点
自己用画图工具画的图
无标题.jpg

根据相似和三角函数可以求得start1,end1,start2,end2这四个点的坐标,这里自己的处理有些麻烦,会根据在不同区域进行计算,会根据在不同的,同时将两个圆心的中间点作为贝塞尔曲线的控制点。

package com.example.chris.gds;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by chris on 2015/12/11.
 */
public class DrawView extends View implements View.OnTouchListener {
    public DrawView(Context context) {
        super(context);
    }

    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // aniamtion();
        setOnTouchListener(this);
    }

    private float x1 = 200;
    private float y1 = 400;
    private float r1 = 10;

    private float x2 = 200;
    private float y2 = 400;
    private float r2 = 50;

    /**
     * 获取两个圆心之间的距离
     * @return
     */
    private double getInstance() {
        float a = Math.abs(x2 - x1);
        float b = Math.abs(y2 - y1);
        return Math.sqrt(a * a + b * b);
    }

    /**
     * 获取圆心角一半的弧度
     * @return
     */
    private double getCenterAngle() {
        double s = getInstance();
        return Math.acos(Math.abs(r2 - r1) / s);
    }

    /**
     * 获取圆心连线和x轴的夹角
     * @return
     */
    private double getLineAngle() {
        double s = getInstance();
        double value = Math.asin(Math.abs(y2 - y1) / s);
        return value;
    }
    /**
     * 获取两个圆心之间的中间点
     * @return
     */
    public float[] getCenterPoint(float x1, float y1, float x2, float y2) {
        float centerX = 0;
        if (x1 > x2) {
            centerX = x2 + (x1 - x2) / 2;
        } else {
            centerX = x1 + (x2 - x1) / 2;
        }
        float centerY = 0;
        if (y2 > y1) {
            centerY = y1 + (y2 - y1) / 2;
        } else {
            centerY = y2 + (y1 - y2) / 2;
        }
        return new float[]{centerX, centerY};
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);

        //首先绘制两个圆
        canvas.drawCircle(x1, y1, r1, paint);
        canvas.drawCircle(x2, y2, r2, paint);
        Path path = new Path();
        double centerAngle = getCenterAngle();
        double lineAngle = getLineAngle();
        double requireAngle1 = centerAngle - lineAngle;
        double requireAngle2 = centerAngle + lineAngle - Math.PI / 2;
        //贝塞尔曲线的起始点
        float startX1 = 0;
        float startY1 = 0;
        //贝塞尔曲线的结束点
        float endX1 = 0;
        float endY1 = 0;
        float startX2 = 0;
        float startY2 = 0;
        //贝塞尔曲线的结束点
        float endX2 = 0;
        float endY2 = 0;
        if (x1 <= x2 && y1 >= y2) {
            //在第一象限
            startX1 = (float) (x2 - (r2 * Math.cos(requireAngle1)));
            startY1 = (float) (y2 - (r2 * Math.sin(requireAngle1)));

            endX1 = (float) (x1 - (r1 * Math.cos(requireAngle1)));
            endY1 = (float) (y1 - (r1 * Math.sin(requireAngle1)));

            startX2 = (float) (x2 + (r2 * Math.sin(requireAngle2)));
            startY2 = (float) (y2 + (r2 * Math.cos(requireAngle2)));
            //贝塞尔曲线的结束点
            endX2 = (float) (x1 + (r1 * Math.sin(requireAngle2)));
            endY2 = (float) (y1 + (r1 * Math.cos(requireAngle2)));

        } else if (x1 > x2 && y1 >= y2) {
            //在第二象限

            startX1 = (float) (x2 - (r2 * Math.sin(requireAngle2)));
            startY1 = (float) (y2 + (r2 * Math.cos(requireAngle2)));

            endX1 = (float) (x1 - (r1 * Math.sin(requireAngle2)));
            endY1 = (float) (y1 + (r1 * Math.cos(requireAngle2)));

            startX2 = (float) (x2 + (r2 * Math.cos(requireAngle1)));
            startY2 = (float) (y2 - (r2 * Math.sin(requireAngle1)));
            //贝塞尔曲线的结束点
            endX2 = (float) (x1 + (r1 * Math.cos(requireAngle1)));
            endY2 = (float) (y1 - (r1 * Math.sin(requireAngle1)));

        } else if (x1 > x2 && y1 < y2) {
            //在第三象限
            startX1 = (float) (x2 + (r2 * Math.cos(requireAngle1)));
            startY1 = (float) (y2 + (r2 * Math.sin(requireAngle1)));

            endX1 = (float) (x1 + (r1 * Math.cos(requireAngle1)));
            endY1 = (float) (y1 + (r1 * Math.sin(requireAngle1)));

            startX2 = (float) (x2 - (r2 * Math.sin(requireAngle2)));
            startY2 = (float) (y2 - (r2 * Math.cos(requireAngle2)));
            //贝塞尔曲线的结束点
            endX2 = (float) (x1 - (r1 * Math.sin(requireAngle2)));
            endY2 = (float) (y1 - (r1 * Math.cos(requireAngle2)));


        } else if (x1 <= x2 && y1 < y2) {
            //在第四象限
            startX1 = (float) (x2 + (r2 * Math.sin(requireAngle2)));
            startY1 = (float) (y2 - (r2 * Math.cos(requireAngle2)));

            endX1 = (float) (x1 + (r1 * Math.sin(requireAngle2)));
            endY1 = (float) (y1 - (r1 * Math.cos(requireAngle2)));

            startX2 = (float) (x2 - (r2 * Math.cos(requireAngle1)));
            startY2 = (float) (y2 + (r2 * Math.sin(requireAngle1)));
            //贝塞尔曲线的结束点
            endX2 = (float) (x1 - (r1 * Math.cos(requireAngle1)));
            endY2 = (float) (y1 + (r1 * Math.sin(requireAngle1)));
        }
        path.moveTo(startX1, startY1);

        //贝塞尔曲线控制点
        //float[] center1 = getControlPoint(startX1, startY1, endX1, endY1, false);
        float[] center1 = getCenterPoint(x1, y1, x2, y2);

        path.quadTo(center1[0], center1[1], endX1, endY1);

        path.lineTo(endX2, endY2);
        //贝塞尔曲线控制点
        //float[] center2 = getControlPoint(startX2, startY2, endX2, endY2, true);
        float[] center2 = getCenterPoint(x1, y1, x2, y2);
        path.quadTo(center2[0], center2[1], startX2, startY2);
        path.lineTo(startX1, startY1);
        //封闭两个贝塞尔曲线
        canvas.drawPath(path, paint);
    }

    /**
     * 回弹动画
     */
    public void animation() {

        AnimatorSet animatorSet = new AnimatorSet();

        ValueAnimator valueAnimator1 = ValueAnimator.ofFloat(y2, 400f).setDuration(100);
        valueAnimator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                y2 = ((float) animation.getAnimatedValue());
                invalidate();
            }
        });

        ValueAnimator valueAnimator2 = ValueAnimator.ofFloat(x2, 200f).setDuration(100);

        valueAnimator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                x2 = ((float) animation.getAnimatedValue());
                invalidate();
            }
        });

        float r = 50;
        ValueAnimator valueAnimator3 = ValueAnimator.ofFloat(r * 0.7f, r * 1f, r * 0.8f, r * 1f, r * 0.9f, r * 1f).setDuration(400);
        valueAnimator3.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                r2 = ((float) animation.getAnimatedValue());
                invalidate();
            }
        });
        animatorSet.play(valueAnimator1).with(valueAnimator2).before(valueAnimator3);
        animatorSet.start();
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                setOnTouchListener(DrawView.this);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        isTouch = false;
    }

    private boolean isTouch = false;
    private float startX;
    private float startY;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isTouch = isTouch(event.getX(), event.getY());
                if (isTouch) {
                    startX = event.getX();
                    startY = event.getY();
                }
                break;
            case MotionEvent.ACTION_MOVE:

                float currentX = event.getX();
                float currentY = event.getY();
                if (isTouch) {
                    //滑动距离超过300就回弹
                    if (getInstance() > 300) {
                        animation();
                        setOnTouchListener(null);
                    } else {
                        x2 = x2 + (currentX - startX);
                        y2 = y2 + (currentY - startY);
                        postInvalidate();
                        startX = currentX;
                        startY = currentY;
                    }

                }

                break;
            case MotionEvent.ACTION_UP:
                if (isTouch) {
                    animation();
                }
                break;
        }
        return true;
    }

    /**
     * 判断是否点中
     * @param touchX
     * @param touchY
     * @return
     */
    private boolean isTouch(float touchX, float touchY) {
        float minX = x2 - r2;
        float maxX = x2 + r2;
        float minY = y2 - r2;
        float maxY = y2 + r2;
        if (touchX >= minX && touchX <= maxX && touchY >= minY && touchY <= maxY) {
            return true;
        }
        return false;
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,639评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,277评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,221评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,474评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,570评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,816评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,957评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,718评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,176评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,511评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,646评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,322评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,934评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,755评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,987评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,358评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,514评论 2 348

推荐阅读更多精彩内容