自定义圆自由落体和抛物线

//创建圆
public class PointView extends View {
    public PointView(Context context) {
        super(context);
    }

    public PointView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PointView(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);

        //指定圆心坐标,半径就OK
        canvas.drawCircle(100, 100, 100, paint);
    }
}
//activity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView img;
    private Button bt1;
    private Button bt2;
    private PointView ball;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
/*
        img = findViewById(R.id.img);
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ObjectAnimator//
                        .ofFloat(v, "rotationX", 0.0F, 360.0F)//
                        .setDuration(500)//
                        .start();

            }
        });*/
        ball = findViewById(R.id.ball);
        bt1 = findViewById(R.id.bt1);
        bt2 = findViewById(R.id.bt2);
        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt1:

                ValueAnimator animator = ValueAnimator.ofFloat(0, 500
                        - ball.getHeight());
                animator.setTarget(ball);
                animator.setDuration(1000).start();
//		animator.setInterpolator(value)
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
                {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation)
                    {
                        ball.setTranslationY((Float) animation.getAnimatedValue());
                    }
                });



                break;

            case R.id.bt2:


                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(TAG, 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();
                        ball.setX(point.x);
                        ball.setY(point.y);

                    }
                });





                break;

        }
    }
}




相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页