属性动画之☞valueanimator

来源:互联网 发布:51单片机项目外包网 编辑:程序博客网 时间:2024/06/05 11:56
//valueanimator实现的动画    //不需要设置操作的属性,这就是和objectanimator的区别    //好处,不需要操作对象的属性,一定要有getter和settter方法,可以根据当前动画的计算值,来操作任何属性    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        id_ball = (ImageView) findViewById(R.id.id_ball);        //获取屏幕的高度        DisplayMetrics displayMetrics = new DisplayMetrics();        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);        heightPixels = displayMetrics.heightPixels;        id_ball.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                Toast .makeText(MainActivity.this,"123",Toast.LENGTH_SHORT).show();            }        });    }    // 抛物线    public void paowuxian(View view) {        //获取动画执行类,        ValueAnimator valueAnimator = new ValueAnimator();        //设置动画执行时间        valueAnimator.setDuration(3000);        //设置起始点        valueAnimator.setObjectValues(new PointF(0, 0));        //设置类型估值        valueAnimator.setEvaluator(new TypeEvaluator<PointF>() {            @Override            public PointF evaluate(float v, PointF pointF, PointF t1) {                PointF point = new PointF();                point.x = 200 * v * 2;                point.y = 0.5f * 200 * (v * 3) * (v * 2);                return point;            }        });        valueAnimator.start();        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()        {            @Override            public void onAnimationUpdate(ValueAnimator animation)            {                PointF point = (PointF) animation.getAnimatedValue();                id_ball.setX(point.x);                id_ball.setY(point.y);            }        });    }    public void verticalRun(View view){        //垂直//参数一:X起始位置,,参数二Y起始位置        ValueAnimator animator = ValueAnimator.ofFloat(0, heightPixels                - id_ball.getHeight());        animator.setTarget(id_ball);        animator.setDuration(3000).start();//      animator.setInterpolator(value)        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()        {            @Override            public void onAnimationUpdate(ValueAnimator animation)            {                id_ball.setTranslationY((Float) animation.getAnimatedValue());            }        });    }    public void bei(View view){    }}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.valueanimator.MainActivity">    <ImageView        android:id="@+id/id_ball"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@mipmap/ic_launcher"/>    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:orientation="horizontal">        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="verticalRun"            android:text="垂直"/>        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="paowuxian"            android:text="抛物线"/>       </LinearLayout></RelativeLayout>

原创粉丝点击