欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

安卓自定义View画球加上动画抛物线和垂直

程序员文章站 2022-05-04 19:45:21
...
1.首先新建一个类继承View重写方法

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


public class Animotor extends View {
    public Animotor(Context context) {
        super(context);
    }

    public Animotor(Context context,  AttributeSet attrs) {
        super(context, attrs);


    }

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



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

        // 对于画笔
        Paint paint = new Paint();
        // 设置抗锯齿
        paint.setAntiAlias(true);
        // 设置画笔颜色
        paint.setColor(Color.RED);
        // 三种样式
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setStrokeWidth(5);
        // 阴影
        paint.setShadowLayer(10, 0, 0, Color.CYAN);
        //画圆
        canvas.drawCircle(100, 100, 100, paint);
    }

}

2.在布局引用

<com.example.zzrk2019221.Animotor
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:id="@+id/animotion"
    android:layout_weight="9"
    />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="1"
    >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/but1"
        android:text="垂直"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/but2"
        android:text="抛物线"
        />
3.在Activity里获取控件

package com.example.zzrk2019221;

import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.graphics.PointF;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;

public class Main2Activity extends AppCompatActivity {

    private Animotor animotors;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        animotors = findViewById(R.id.animotion);

        Button but1 = findViewById(R.id.but1);

        Button but2 = findViewById(R.id.but2);
        but1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                verticalRun(animotors);
            }
        });

        but2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                paowuxian(animotors);
            }
        });


    }

    /**
     * *落体
     *
     * @param view
     */
    public void verticalRun(View view) {
        ValueAnimator animator = ValueAnimator.ofFloat(0,animotors.getHeight());
        animator.setTarget(animotors);
        animator.setDuration(1000).start();
//		animator.setInterpolator(value)
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                animotors.setTranslationY((Float) animation.getAnimatedValue());
            }
        });

    }


    /**
     * 抛物线
     * @param view
     */
    public void paowuxian(View view)
    {

        ValueAnimator valueAnimator = new ValueAnimator();
        valueAnimator.setDuration(3000);
        valueAnimator.setObjectValues(new PointF(0, 0));
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.setEvaluator(new TypeEvaluator<PointF>()
        {
            // fraction = t / duration
            @Override
            public PointF evaluate(float fraction, PointF startValue,
                                   PointF endValue)
            {
                Log.e("zzz", fraction * 3 + "");
                // x方向200px/s ,则y方向0.5 * 10 * t
                PointF point = new PointF();
                point.x = 200 * fraction * 3;
                point.y = 0.5f * 200 * (fraction * 3) * (fraction * 3);
                return point;
            }
        });

        valueAnimator.start();
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
        {
            @Override
            public void onAnimationUpdate(ValueAnimator animation)
            {
                PointF point = (PointF) animation.getAnimatedValue();
                animotors.setX(point.x);
                animotors.setY(point.y);

            }
        });
    }


}