转载请注明出处(万分感谢!):
https://blog.csdn.net/weixin_40790006/article/details/80017039
package com.example.f405.animtest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button btn_alpha; private Button btn_rotate; private Button btn_scale; private Button btn_trans; private ImageView iv; private Button btn_group; private AlphaAnimation alpha; private RotateAnimation rotate; private ScaleAnimation scale; private AnimationSet set; private TranslateAnimation translate; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { btn_alpha = (Button) findViewById(R.id.btn_alpha); btn_rotate = (Button) findViewById(R.id.btn_rotate); btn_scale = (Button) findViewById(R.id.btn_scale); btn_trans = (Button) findViewById(R.id.btn_trans); iv = (ImageView) findViewById(R.id.iv); btn_group = (Button) findViewById(R.id.btn_group); btn_alpha.setOnClickListener(this); btn_rotate.setOnClickListener(this); btn_scale.setOnClickListener(this); btn_trans.setOnClickListener(this); btn_group.setOnClickListener(this); } // TODO 补间动画/View动画;不会改变控件的真实坐标 @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_alpha: alpha = new AlphaAnimation(1.0f,0.0f); // 时间长度 alpha.setDuration(2000); // 重复次数 alpha.setRepeatCount(1); // 模式 alpha.setRepeatMode(Animation.REVERSE); // 设置动画 iv.startAnimation(alpha); break; case R.id.btn_rotate: // 图片旋转 // RotateAnimation rotate = new RotateAnimation(0,360); // 图片中心旋转;0.5中心点 rotate = new RotateAnimation(0.0f,360.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); rotate.setDuration(2000); // 重复次数 rotate.setRepeatCount(1); // 模式 rotate.setRepeatMode(Animation.REVERSE); // 设置动画 iv.startAnimation(rotate); break; case R.id.btn_scale: // 缩放比,缩放参照物 scale = new ScaleAnimation(1.0f,2.0f,1.0f,2.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); scale.setDuration(2000); // 重复次数 scale.setRepeatCount(1); // 模式 scale.setRepeatMode(Animation.REVERSE); // 设置动画 iv.startAnimation(scale); break; case R.id.btn_trans: // 位移;x轴 translate = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0.3f,Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0); translate.setDuration(2000); // 位移点,停止 translate.setFillAfter(true); // 设置动画 iv.startAnimation(translate); break; case R.id.btn_group: set = new AnimationSet(true); // 图片中心旋转;0.5中心点 rotate = new RotateAnimation(0.0f,360.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); rotate.setDuration(2000); // 重复次数 rotate.setRepeatCount(1); // 模式 rotate.setRepeatMode(Animation.REVERSE); // 位移;x轴 translate = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0.3f,Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0); translate.setDuration(2000); // 位移点,停止 translate.setFillAfter(true); // 添加多个动画 set.addAnimation(rotate); set.addAnimation(translate); iv.startAnimation(set); break; } } }