propertyanimator(属性动画)之valuesanimator(动画执行类)

1:获取一张图片,用系统图片也行

<ImageView
    android:id="@+id/iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"/>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal">

    <Button
        android:id="@+id/bt_verticalRun"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自由落体"/>

    <Button
        android:id="@+id/bt_parabola"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="抛物线"/>
LinearLayout>
2:代码实现属性动画

public class OtherActivity extends AppCompatActivity implements View.OnClickListener{
    private int screenHeightPixels;
    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);
        //找控件
        iv = (ImageView) findViewById(R.id.iv);
        Button bt_parabola = (Button) findViewById(R.id.bt_parabola);
        Button bt_verticalRun = (Button) findViewById(R.id.bt_verticalRun);
        bt_verticalRun.setOnClickListener(this);
        bt_parabola.setOnClickListener(this);
        //获取屏幕
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        //得到屏幕的高度
        screenHeightPixels = metrics.heightPixels;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_parabola://抛物线
                //获取动画执行类
                ValueAnimator valueAnimator = new ValueAnimator();
                //设置起始坐标
                valueAnimator.setObjectValues(new PointF(0, 0));
                valueAnimator.setDuration(3000);
                //插值器
                valueAnimator.setInterpolator(new LinearInterpolator());
                //设置类型估值
                valueAnimator.setEvaluator(new TypeEvaluator() {

                    @Override
                    public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
                        // fraction = t / duration
                        PointF pointF = new PointF();
                        // x方向200px/s ,则y方向0.5 * 10 * t
                        pointF.x = 200 * fraction * 3;
                        pointF.y = 300 * (fraction * 3) * (fraction * 3);
                        return pointF;
                    }
                });
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        PointF pointF = (PointF) animation.getAnimatedValue();
                        //获取最终位置
                        iv.setX(pointF.x);
                        iv.setY(pointF.y);
                    }
                });
                valueAnimator.start();

                break;
            case R.id.bt_verticalRun://自由落体
                //动画执行类
                ValueAnimator animator = ValueAnimator.ofFloat(0, screenHeightPixels - iv.getHeight());
                animator.setTarget(iv);
                animator.setDuration(4000).start();
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        //获取平移位置
                        float hight = (float) animation.getAnimatedValue();
                        iv.setTranslationY(hight);
                    }
                });
                break;
        }
    }
}
3:推荐博客    http://blog.csdn.net/lmj623565791/article/details/38067475/

你可能感兴趣的:(propertyanimator(属性动画)之valuesanimator(动画执行类))