各种自定义动画效果

package com.ct.anim;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener{
    private  Button btnAlpha;
    private  Button btnScale;
    private  Button btnRotate;
    private  Button btnTranslate;
    private  Button btnCombine;
    private  Button btnPicAni;
    private  ImageView imgPic;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initView();
        setListener();
    }

    
    /**
     *  初始化控件
     */
    
    private void initView(){
        btnAlpha = (Button)findViewById(R.id.alpha);
        btnScale = (Button)findViewById(R.id.scale);
        btnRotate =  (Button)findViewById(R.id.rotate); 
        btnTranslate =  (Button)findViewById(R.id.translate); 
        btnCombine = (Button)findViewById(R.id.comebine); 
        btnPicAni = (Button)findViewById(R.id.picani); 
        imgPic = (ImageView)findViewById(R.id.img_pic);
    }
    
    /**
     * 设置监听
     */
    private void setListener(){
        btnAlpha.setOnClickListener(this);
        btnScale.setOnClickListener(this);
        btnRotate.setOnClickListener(this);
        btnTranslate.setOnClickListener(this);
        btnPicAni.setOnClickListener(this);
        btnCombine.setOnClickListener(this);
    }
    
    /**
     * 开始动画(淡入淡出)
     */
    
    private void startAlphaAnimation(){
//        AnimationSet animationSet = new AnimationSet(true);
//        AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
//        alphaAnimation.setDuration(1000);
//        alphaAnimation.setStartOffset(1000);
//        animationSet.addAnimation(alphaAnimation);
//        alphaAnimation.setFillBefore(false);
//        alphaAnimation.setFillAfter(true);
        
        Animation alphaAnimation = AnimationUtils.loadAnimation(MainActivity.this, 
                R.anim.apha_xml);
        
        imgPic.startAnimation(alphaAnimation);
    }
    
    
    /**
     * 开始动画(缩放)
     */
    private void startScaleAnimation(){
//        AnimationSet animationSet = new AnimationSet(true);
//        ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.5f, 1, 0.5f,
//                Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 1f);
//        scaleAnimation.setDuration(1000);
//        animationSet.addAnimation(scaleAnimation);
//        imgPic.startAnimation(animationSet);
        
        Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale_xml);
        imgPic.startAnimation(animation);
    }
    
    
    /**
     * 开始动画(旋转)
     */
    private void startRotateAnimation(){
//        AnimationSet animationSet = new AnimationSet(true);
//        RotateAnimation rotateAnimation = new RotateAnimation(0, 360, 
//                Animation.RELATIVE_TO_PARENT, 0.5f,
//                Animation.RELATIVE_TO_PARENT, 0.5f);
//        rotateAnimation.setDuration(1000);
//        animationSet.addAnimation(rotateAnimation);
//        imgPic.startAnimation(animationSet);
        
        Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate_xml);
        imgPic.startAnimation(animation);
    }
    
    /**
     * 开始动画(移动)
     */
    private void startTranslateAnimation(){
//        AnimationSet animationSet = new AnimationSet(true);
//        TranslateAnimation translateAnimation = new TranslateAnimation(
//                Animation.RELATIVE_TO_SELF, 0f,
//                Animation.RELATIVE_TO_SELF, 1.0f,
//                Animation.RELATIVE_TO_SELF, 0f,
//                Animation.RELATIVE_TO_SELF, 1.0f);
//        translateAnimation.setDuration(1000);
//        animationSet.addAnimation(translateAnimation);
//        imgPic.startAnimation(animationSet);
        
        Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate_xml);
        imgPic.startAnimation(animation);
    }
    
    /**
     * 开始动画(综合)
     */
    private void startComebineAnimation(){
//        AnimationSet animationSet = new AnimationSet(false);
//        AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);
//        ScaleAnimation scale = new ScaleAnimation(1, 0.5f, 1, 0.5f,
//        Animation.RELATIVE_TO_SELF, 0.5f,
//        Animation.RELATIVE_TO_SELF, 0.5f);
//        animationSet.addAnimation(alpha);
//        animationSet.addAnimation(scale);
//        animationSet.setDuration(2000);
//        animationSet.setStartOffset(1000);
//        animationSet.setFillAfter(true);
//        imgPic.startAnimation(animationSet);
        
        Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.combine_xml);
        imgPic.startAnimation(animation);
    }
    
    /**
     * 开始动画(图片)
     */
    private void startPicAnimation(){
//        imgPic.setBackgroundResource(R.drawable.anim_run);
//        AnimationDrawable animationDrawable  = (AnimationDrawable)imgPic.getBackground();
//        animationDrawable.start();
        
        imgPic.setImageResource(R.drawable.anim_run);
        AnimationDrawable animationDrawable = (AnimationDrawable)imgPic.getDrawable();
        animationDrawable.start();
    }
    
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.alpha:
            startAlphaAnimation();
            break;
        case R.id.scale:
            startScaleAnimation();
            break;
        case R.id.rotate:
            startRotateAnimation();
            break; 
        case R.id.translate:
            startTranslateAnimation();
            break;
        case R.id.comebine:
            startComebineAnimation();
            break;
        case R.id.picani:
            startPicAnimation();
            break;

        default:
            break;
        }
    }


    

}

(在F:\java\d动画效果大集合)

原文地址:https://www.cnblogs.com/ct732003684/p/2996592.html