例:
MainActivity 主方法类:
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button py;//平移 private Button xz;//旋转 private Button jb;//渐变 private Button sf;//缩放 private Button yq;//一起执行 private ImageView img;//图片 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { py = findViewById(R.id.py);//平移 xz = findViewById(R.id.xz);//旋转 jb = findViewById(R.id.jb);//渐变 sf = findViewById(R.id.sf);//缩放 yq = findViewById(R.id.yq);//一起执行 img = findViewById(R.id.img);//图片 //设置点击监听 py.setOnClickListener(this);//平移 xz.setOnClickListener(this);//旋转 jb.setOnClickListener(this);//渐变 sf.setOnClickListener(this);//缩放 yq.setOnClickListener(this);//一起执行 } @Override//点击事件 public void onClick(View view) { switch (view.getId()){ case R.id.py: //平移 TranslateAnimation animation = new TranslateAnimation(50, 300, 50, 300); animation.setDuration(2000);//设置持续时间 animation.setFillAfter(false);//设置不保持动画结束的状态 animation.setInterpolator(new AccelerateDecelerateInterpolator()); img.startAnimation(animation); break; case R.id.xz: //旋转 RotateAnimation rotation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotation.setDuration(1500);//设置持续时间 rotation.setRepeatMode(Animation.REVERSE);//设置从结束位置反过来进行动画模式 rotation.setRepeatCount(Animation.INFINITE);//设置动画无限循环的次数 img.startAnimation(rotation); break; case R.id.jb: //渐变透明 Animation alpha = new AlphaAnimation(0.1f, 1.0f);//从透明到不透明 alpha.setDuration(1000);//设置持续时间 alpha.setFillAfter(false);//设置不保持动画结束的状态 alpha.setRepeatMode(Animation.REVERSE);//设置从结束位置反过来进行动画模式 alpha.setRepeatCount(Animation.INFINITE);//设置动画无限循环的次数 img.startAnimation(alpha); break; case R.id.sf: //缩放 ScaleAnimation scaleX = new ScaleAnimation(1f, 0.5f, 1f, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleX.setDuration(1500);//设置持续时间 scaleX.setRepeatMode(Animation.REVERSE);//设置从结束位置反过来进行动画模式 scaleX.setRepeatCount(Animation.INFINITE);//设置动画无限循环的次数 img.startAnimation(scaleX); break; case R.id.yq: //一起执行 AnimationSet animationSet = new AnimationSet(true); //渐变动画 AlphaAnimation alphaAnimation = new AlphaAnimation(1f, 0.05f); alphaAnimation.setDuration(3000);//设置持续时间 //缩放动画 ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 0.05f, 1f, 0.05f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnimation.setDuration(3000);//设置持续时间 //旋转动画 RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation.setDuration(3000);//设置持续时间 //动画集合添加三个动画 animationSet.addAnimation(alphaAnimation); animationSet.addAnimation(scaleAnimation); animationSet.addAnimation(rotateAnimation); img.startAnimation(animationSet); break; } } }
activity_main布局:
<?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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ImageView android:id="@+id/img" android:layout_width="300dp" android:layout_height="300dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_gravity="center" android:layout_marginTop="44dp" android:src="@drawable/pp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal"> <Button android:id="@+id/py" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="平移" /> <Button android:id="@+id/xz" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旋转" /> <Button android:id="@+id/jb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="渐变" /> <Button android:id="@+id/sf" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="缩放" /> <Button android:id="@+id/yq" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="一起执行" /> </LinearLayout> </RelativeLayout>
。。。