[转]安卓Animation

[转自]http://www.360doc.com/content/13/0102/22/6541311_257754535.shtml

重要的事情说三遍

fillBefore是指动画结束时画面停留在第一帧,fillAfter是指动画结束是画面停留在最后一帧。最关键的问题是,这2个参数不能在</alpha>,</scale>,</translate>,</rotate>中设置,这是没有用的,必须在动画xml文件的</set>节点中设置。如果是代码必须在AnimationSet中设置。

fillBefore是指动画结束时画面停留在第一帧,fillAfter是指动画结束是画面停留在最后一帧。最关键的问题是,这2个参数不能在</alpha>,</scale>,</translate>,</rotate>中设置,这是没有用的,必须在动画xml文件的</set>节点中设置。如果是代码必须在AnimationSet中设置。

fillBefore是指动画结束时画面停留在第一帧,fillAfter是指动画结束是画面停留在最后一帧。最关键的问题是,这2个参数不能在</alpha>,</scale>,</translate>,</rotate>中设置,这是没有用的,必须在动画xml文件的</set>节点中设置。如果是代码必须在AnimationSet中设置。

其他的看我转的链接吧。

package com.example.myapp.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.*;
import android.widget.Button;
import android.widget.ImageView;
import com.example.myapp.R;

/**
 * Created by admin on 15/9/9.
 */
public class AnimationActivity extends Activity implements View.OnClickListener{
    private Button alpha;
    private Button traslate;
    private Button rotate;
    private Button scale;
    private Button all;
    private Button xml;
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animation);
        initviews();
        initListener();
    }

    public void initviews(){
        alpha = (Button)findViewById(R.id.alpha);
        traslate = (Button)findViewById(R.id.traslate);
        rotate = (Button)findViewById(R.id.rotate);
        scale = (Button)findViewById(R.id.scale);
        imageView = (ImageView)findViewById(R.id.image);
        all = (Button)findViewById(R.id.all);
        xml = (Button)findViewById(R.id.xml);

    }

    public void initListener(){
        alpha.setOnClickListener(this);
        traslate.setOnClickListener(this);
        rotate.setOnClickListener(this);
        scale.setOnClickListener(this);
        all.setOnClickListener(this);
        xml.setOnClickListener(this);
    }

    /**
     * Tween Animations的通用方法
       1、setDuration(long durationMills)
       设置动画持续时间(单位:毫秒)
       2、setFillAfter(Boolean fillAfter)
       如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态
       3、setFillBefore(Boolean fillBefore)
       如果fillBefore的值为true,则动画执行后,控件将回到动画执行之前的状态
       4、setStartOffSet(long startOffSet)
       设置动画执行之前的等待时间
       5、setRepeatCount(int repeatCount)
       设置动画重复执行的次数
     * @param v
     */

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.alpha:
                AnimationSet animationSet = new AnimationSet(true);
                AlphaAnimation alphaAnimation = new AlphaAnimation(0,0.5f);
                alphaAnimation.setDuration(1000);
                animationSet.addAnimation(alphaAnimation);
                animationSet.setFillAfter(true);
                imageView.startAnimation(animationSet);
                break;
            case R.id.traslate:
                AnimationSet animationSet1 = new AnimationSet(true);
                TranslateAnimation translateAnimation = new TranslateAnimation(
                        Animation.RELATIVE_TO_SELF,0,
                        Animation.RELATIVE_TO_SELF,1,
                        Animation.RELATIVE_TO_SELF,0,
                        Animation.RELATIVE_TO_SELF,1);
                translateAnimation.setDuration(1000);
                animationSet1.addAnimation(translateAnimation);
                imageView.setAnimation(animationSet1);
                break;
            case R.id.rotate:
                AnimationSet animationSet2 = new AnimationSet(true);
                RotateAnimation rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                rotateAnimation.setDuration(1000);
                animationSet2.addAnimation(rotateAnimation);
                imageView.setAnimation(animationSet2);
                break;
            case R.id.scale:
                AnimationSet animationSet3 = new AnimationSet(true);
                ScaleAnimation scaleAnimation = new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                scaleAnimation.setDuration(1000);
                animationSet3.addAnimation(scaleAnimation);
                imageView.setAnimation(animationSet3);
                break;
            case R.id.all:
                AnimationSet animationSet4 = new AnimationSet(true);
                AlphaAnimation alphaAnimation2 = new AlphaAnimation(0,1);
                alphaAnimation2.setDuration(1000);
                animationSet4.addAnimation(alphaAnimation2);

                TranslateAnimation translateAnimation2 = new TranslateAnimation(
                        Animation.RELATIVE_TO_SELF,0,
                        Animation.RELATIVE_TO_SELF,0.5f,
                        Animation.RELATIVE_TO_SELF,0,
                        Animation.RELATIVE_TO_SELF,0.5f);
                translateAnimation2.setDuration(1000);
                animationSet4.addAnimation(translateAnimation2);

                RotateAnimation rotateAnimation2 = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                rotateAnimation2.setDuration(1000);
                animationSet4.addAnimation(rotateAnimation2);

                ScaleAnimation scaleAnimation2 = new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                scaleAnimation2.setDuration(1000);
                animationSet4.addAnimation(scaleAnimation2);

                imageView.setAnimation(animationSet4);
                break;
            case R.id.xml:
                Animation animation = AnimationUtils.loadAnimation(this,R.anim.animation);
                imageView.startAnimation(animation);
                break;
        }

    }
}
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:shareInterpolator="true"
        android:fillAfter="true">
    <alpha android:duration="1000"
           android:fromAlpha="0.0"
           android:toAlpha="0.3"></alpha>
    <rotate android:duration="1000"
            android:fromDegrees="0"
            android:toDegrees="360"
            android:pivotX="50%"
            android:pivotY="50%"></rotate>
    <scale android:duration="1000"
           android:fromXScale="0"
           android:toXScale="1.0"
           android:fromYScale="0"
           android:toYScale="1.0"
           android:pivotX="50%"
           android:pivotY="50%"></scale>
</set>
原文地址:https://www.cnblogs.com/bbglz/p/4797411.html