ObjectAnimator平移并透明动画

ObjectAnimator平移并透明动画
文章图片

布局文件

【ObjectAnimator平移并透明动画】MainActivity.java
package com.example.administrator.day_13; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends AppCompatActivity {@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ImageView img = (ImageView) findViewById(R.id.image); img.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "hello", Toast.LENGTH_SHORT).show(); } }); Button bt = (Button) findViewById(R.id.translate); bt.setOnClickListener(new View.OnClickListener() { private static final String TAG = "MainActivity"; @Override public void onClick(View v) { 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); img.setVisibility(View.GONE); }@Override public void onAnimationRepeat(Animator animation) { super.onAnimationRepeat(animation); }@Override public void onAnimationStart(Animator animation) { super.onAnimationStart(animation); } }); } }); } }

    推荐阅读