程序入口代码:MainActivity.java


public class MainActivity extends Activity implements OnClickListener{
                                                           
    private ImageView iv;
    private Button btn1,btn2,btn3,btn4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                                                                
        iv=(ImageView) findViewById(R.id.p_w_picpathView1);
        btn1=(Button) findViewById(R.id.button1);
        btn2=(Button) findViewById(R.id.button2);
        btn3=(Button) findViewById(R.id.button3);
        btn4=(Button) findViewById(R.id.button4);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
                                                                    
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1://缩放
            AnimationSet animationSet =new AnimationSet(true);
            ScaleAnimation scaleAnimation=new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f);
            scaleAnimation.setDuration(2000);
            scaleAnimation.setRepeatCount(1);
            animationSet.addAnimation(scaleAnimation);
            iv.startAnimation(animationSet);
            animationSet.setAnimationListener(new AnimationListener() {
                                                                        
                @Override//开始时执行
                public void onAnimationStart(Animation animation) {
                                                                                        
                }              
                @Override
                public void onAnimationRepeat(Animation animation) {
                                                                            
                }              
                @Override//动画结束时执行
                public void onAnimationEnd(Animation animation) {
                    Animation animation1=AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
                    iv.startAnimation(animation1);
                }
            });        
            break;
       case R.id.button2://旋转.以下方式需要在res/anim文件夹中定义动画效果的xml文件
            Animation animation1=AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
            iv.startAnimation(animation1);
            break;
       case R.id.button3://移动
           AnimationSet animationSet2 =new AnimationSet(true);
           TranslateAnimation ta=new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF,0);
           ta.setDuration(2000);
           animationSet2.addAnimation(ta);
           iv.startAnimation(animationSet2);
                                                                    
            break;
       case R.id.button4://淡入淡出
            Animation animation=AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
            iv.startAnimation(animation);
            break;
        default:
            break;
        }
    }
}



anim中的xml文件:

alpha.xml:

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
     android:duration="3000"
     android:fromAlpha="1"
     android:toAlpha="0"
    > 
</alpha>

rotate.xml:

<?xml version="1.0" encoding="utf-8"?>
<rotate
    android:duration="2000"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    xmlns:android="http://schemas.android.com/apk/res/android">
</rotate>

scale.xml:

<?xml version="1.0" encoding="utf-8"?>
<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fromXScale="0.0"
    android:fromYScale="0.0"
    android:toXScale="1.5"
    android:toYScale="1.5"
    android:pivotX="50%"
    android:pivotY="50%"
    >
</scale>


运行结果:

201147657.png