一、简介
二、代码
1.xml(1)activity_main.xml1 25 6 9 10 13 14 17 18 21 22 24 25 3029
2.java
(1)MainActivity.java1 package com.animation1; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.View.OnClickListener; 7 import android.view.animation.AlphaAnimation; 8 import android.view.animation.Animation; 9 import android.view.animation.AnimationSet;10 import android.view.animation.RotateAnimation;11 import android.view.animation.ScaleAnimation;12 import android.view.animation.TranslateAnimation;13 import android.widget.Button;14 import android.widget.ImageView;15 16 public class MainActivity extends Activity {17 18 private ImageView imageView = null;19 private Button rotateButton, scaleButton, alphaButton, translateButton = null;20 @Override21 protected void onCreate(Bundle savedInstanceState) {22 super.onCreate(savedInstanceState);23 setContentView(R.layout.activity_main);24 25 imageView = (ImageView) findViewById(R.id.imageViewId);26 27 rotateButton = (Button) findViewById(R.id.rotateButtonId);28 scaleButton = (Button) findViewById(R.id.scaleButtonId);29 alphaButton = (Button) findViewById(R.id.alphaButtonId);30 translateButton = (Button) findViewById(R.id.translateButtonId);31 32 rotateButton.setOnClickListener(new RotateButtonListener());33 scaleButton.setOnClickListener(new ScaleButtonListener());34 alphaButton.setOnClickListener(new AlphaButtonListener());35 translateButton.setOnClickListener(new TranslateButtonListener());36 37 }38 39 private class RotateButtonListener implements OnClickListener {40 @Override41 public void onClick(View v) {42 AnimationSet animationSet = new AnimationSet(true);43 RotateAnimation rotateAnimation = new RotateAnimation(0, 360, 44 AnimationSet.RELATIVE_TO_PARENT, 1f,45 Animation.RELATIVE_TO_PARENT, 0f);46 rotateAnimation.setDuration(5000);47 animationSet.addAnimation(rotateAnimation);48 imageView.startAnimation(animationSet);49 }50 }51 52 private class ScaleButtonListener implements OnClickListener {53 @Override54 public void onClick(View v) {55 AnimationSet animationSet = new AnimationSet(true);56 ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f, 57 Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);58 animationSet.addAnimation(scaleAnimation);59 animationSet.setStartOffset(1000);60 animationSet.setFillAfter(true);61 animationSet.setFillBefore(false);62 animationSet.setDuration(2000);63 imageView.startAnimation(animationSet);64 }65 }66 67 private class AlphaButtonListener implements OnClickListener {68 @Override69 public void onClick(View v) {70 AnimationSet animationSet = new AnimationSet(true);71 AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);72 alphaAnimation.setDuration(1000);73 animationSet.addAnimation(alphaAnimation);74 imageView.startAnimation(animationSet);75 }76 }77 78 private class TranslateButtonListener implements OnClickListener {79 @Override80 public void onClick(View v) {81 AnimationSet animationSet = new AnimationSet(true);82 TranslateAnimation translateAnimation = new TranslateAnimation(83 Animation.RELATIVE_TO_SELF, 0f, 84 Animation.RELATIVE_TO_SELF, 0.5f, 85 Animation.RELATIVE_TO_SELF, 0f, 86 Animation.RELATIVE_TO_SELF, 1.0f);87 translateAnimation.setDuration(1000);88 animationSet.addAnimation(translateAnimation);89 imageView.startAnimation(translateAnimation);90 }91 }92 }