安卓Animation

来源:互联网 发布:淘宝网11.11销售额 编辑:程序博客网 时间:2024/05/26 22:05

[转自]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中设置。

其他的看我转的链接吧。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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;
        }
 
    }
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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>

0 0