ObjectAnimator(属性动画)

package com.example.animator; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends AppCompatActivity {private Button but; private ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 but = (Button) findViewById(R.id.but); img = (ImageView) findViewById(R.id.img); //图片点击事件 img.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {Toast.makeText(MainActivity.this, "我在这里", Toast.LENGTH_SHORT).show(); } }); //演示属性动画 but.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //创建平移动画 ObjectAnimator translationX = ObjectAnimator.ofFloat(img,"translationX",0,300); //创建透明动画 ObjectAnimator alpha = ObjectAnimator.ofFloat(img, "alpha", 1.0f, 0f); //动画集合 AnimatorSet set = new AnimatorSet(); //添加动画 set.play(translationX).with(alpha); //动画耗时 set.setDuration(5000); //开启 set.start(); //动画监听 set.addListener(new AnimatorListenerAdapter() { @Override//动画结束时 public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); }@Override public void onAnimationRepeat(Animator animation) { super.onAnimationRepeat(animation); }@Override//动画开始时 public void onAnimationStart(Animator animation) { super.onAnimationStart(animation); } }); } }); }

}

--------------------------------xml文件-----------------------------







【ObjectAnimator(属性动画)】

    推荐阅读