android 用贝塞尔曲线做曲线动作

先看看公式,熟悉熟悉。
百度公式
如果不了解android属性动画,网上找找资料很多的。

首先我们建一个emmmm,一个实体类,保存坐标以及图片(png背景透明)
有图片,所以就用bitmap


public class Point {
    private float x;
    private float y;
    private Bitmap mBitmap;

    public Point(float x, float y,Bitmap bitmao) {
        this.x = x;
        this.y = y;
        this.mBitmap= bitmao;
    }

    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }

    public Bitmap getImageView(){
        return mBitmap;
    }
}

接着我们实现

public class PointEvaluator implements TypeEvaluator{...}

可以TypeEvaluator<Point>
同时我们有一个控制点,就是曲线上面的p0,p1,p2 的p1
Point mPoint;
PointEvaluator(Point point) {
mPoint = point;
}

@Override
    public Object evaluate(float v, Object o, Object t1) {
        Point startPoint = (Point) o;
        Point endPoint = (Point) t1;
        float left = 1-v;
        float x = left * left *  startPoint.getX() +
                2 * left *  v *  mPoint.getX() +
                v * v * endPoint.getX();

        float y = left * left * startPoint.getY() +
                2  * left * v * mPoint.getY() +
                v * v * endPoint.getY();
        return new Point(x, y,mPoint.getImageView());
    }

然后,实现我们自己的一个类

public class MyView extends View {
    public static float RADIUS = 0f;

    private Point currentPoint;

    private Paint mPaint;
    private Bitmap mBitmap;
    private Context mContext;
    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.d);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setAntiAlias(true);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        if (currentPoint == null) {
            RADIUS = mBitmap.getWidth();
            currentPoint = new Point(RADIUS, RADIUS,mBitmap);
            drawBit(canvas);
            startAnimation();
        } else {
            drawBit(canvas);
        }
        Paint paint = new Paint();
        paint.setAntiAlias(false);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        Path path = new Path();
        path.moveTo(RADIUS,RADIUS);
        path.quadTo(RADIUS,getHeight(),getWidth() -RADIUS, getHeight()-RADIUS);
        canvas.drawPath(path,paint);
    }

    private void drawBit(Canvas canvas) {
        float x = currentPoint.getX();
        float y = currentPoint.getY();
        canvas.drawBitmap(currentPoint.getImageView(),x,y,mPaint);
    }

    private void startAnimation() {
        Point startPoint = new Point(RADIUS, RADIUS,mBitmap);
        Point endPoint = new Point(getWidth() -RADIUS, getHeight()-RADIUS,mBitmap);
        ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluator(new Point(RADIUS,getHeight(),mBitmap)), startPoint, endPoint);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                currentPoint = (Point) animation.getAnimatedValue();
                invalidate();
            }
        });
        anim.setDuration(5000);
        anim.start();
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="cn.krisez.spuerman.MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/sky"/>
    <cn.krisez.spuerman.MyView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

MainActivity不用写东西。
最后运行一下,你的图就在曲线运动了,红线就是让你明白走的路径,可以删了。。。最后emmmmmm三次的一样的操作。。。。。公式换了就好,再来一个控制点就好了

猜你喜欢

转载自blog.csdn.net/qq_34206863/article/details/79446488