android基础--列表数据View刷新动画

该效果类似于iPhone中View的切换动画效果

效果一:

效果二:

效果三:

效果四:

效果五(回旋效果一):

效果六(回旋效果二):

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

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

    <ListView

        android:id="@+id/firstPage"

        android:layout_width="fill_parent"

        android:layout_weight="1.0"

        android:layout_height="0dip"/>

    <ListView

        android:id="@+id/secondPage"

        android:layout_width="fill_parent"

        android:layout_weight="1.0"

        android:layout_height="0dip"

        android:visibility="gone"/>

    <Button

        android:id="@+id/startNext"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/next"

        />

</LinearLayout>

1

<strong> 下面再来看下实现以上效果的具体代码,代码中所标的顺序与上面显示的效果图一致:</strong>

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

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

package com.xiaoma.www;

import android.animation.Animator;

import android.animation.AnimatorListenerAdapter;

import android.animation.ObjectAnimator;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.animation.AccelerateInterpolator;

import android.view.animation.CycleInterpolator;

import android.view.animation.DecelerateInterpolator;

import android.view.animation.Interpolator;

import android.view.animation.OvershootInterpolator;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.ListView;

/**

* @Title: BetweenAnimationActivity.java

* @Package com.xiaoma.www

* @Description: 小马学习模仿iPhone列表分页旋转刷新

* @author XiaoMa

*/

public class BetweenAnimationActivity extends Activity implements OnClickListener

{

    /**资源声明*/

    private Button startNext = null ;

    private ListView firstPage = null ;

    private ListView secondPage = null ;

    /**列表项声明*/

    private static final String firstItem[] ={"海阔人生","光辉岁月","无尽空虚","真的爱你","岁月无声","灰色轨迹","再见理想"};

    private static final String secondItem[] ={"洗唰唰","爱啦啦","喜欢你","娃哈哈","小马果","大坏蛋","冷雨夜"};

    /**列表页面切换动画插值器声明一*/

    private Interpolator accelerator = new AccelerateInterpolator();

    private Interpolator decelerator = new DecelerateInterpolator();

    /**动画插值器二:效果五与效果六都为以下插值器*/

    private Interpolator  accelerator1= new  CycleInterpolator(45f);

    private Interpolator  decelerator1= new  OvershootInterpolator();

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        /**

         * 这个地方写下,大家尽量不要在onCreate方法中写太多的操作,

         * 如果涉及到很多配置问题时有些属性设置必须在onCreate()方法中

         * 写,比如:全屏、横竖屏必须在setContentView()前面写,

         * 如果在onCreate()方法中写太多东西的,一句话:太乱!!

         * */

        init();

    }

    /**

    * 初始化实现

    */

    private void init()

    {

        /**资源定位,添加监听*/

        startNext = (Button)findViewById(R.id.startNext);

        startNext.setOnClickListener(this);

        firstPage = (ListView)findViewById(R.id.firstPage);

        secondPage = (ListView)findViewById(R.id.secondPage);

        ArrayAdapter<String> firstAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,firstItem);

        ArrayAdapter<String> secondAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, secondItem);

        firstPage.setAdapter(firstAdapter);

        secondPage.setAdapter(secondAdapter);

    }

    @Override

    public void onClick(View v)

    {

        changePage();

    }

    //实现列表页面切换

    private void changePage()

    {

        final ListView visiable  ;

        final ListView invisiable ;

    if(firstPage.getVisibility() == View.GONE)

    {

        visiable = secondPage ;

        invisiable = firstPage ;

    }else{

        visiable = firstPage ;

        invisiable = secondPage ;

    }

        //这个地方大家可能看到了ObjectAnimator这个类,一开始我也不知道是什么东西,很简单,查官方文档,官方文档中的解释一堆英文,我//一直说的,我英文烂的要死,但不怕,只要你想,就肯定可以查出来的,大家 只看一句:该类是 ValueAnimator的子类,可以根据给定//的属性名称给目标对象设置动画参数

        //效果一(此处效果顺序与效果图一一对应)

        //final ObjectAnimator invisToVis = ObjectAnimator.ofFloat(invisiable, "rotationX",-90f, 0f);

        ObjectAnimator visToInvis = ObjectAnimator.ofFloat(visiable, "rotationX", 0f, 90f);

        //效果二

        final ObjectAnimator invisToVis = ObjectAnimator.ofFloat(invisiable, "rotationY",-90f, 0f);

       ObjectAnimator visToInvis = ObjectAnimator.ofFloat(visiable, "rotationY", 0f, 90f);

        //效果三(这个地方的alpha属性值大家只记一点:值越大越不透明就可以了!!!)

        //final ObjectAnimator invisToVis = ObjectAnimator.ofFloat(invisiable, "alpha", 0.0f, 1.0f );

        //ObjectAnimator visToInvis = ObjectAnimator.ofFloat(visiable, "alpha", 1.0f, 0.0f );

        //效果四(此于是我犯的一个错误,很天真的以为应该也有rotationZ属性名称,其实是错的,在ofFloat参数中并无此属性名称,但大家还//是可以看到列表正常,其实显示 效果很不正常了因为后台已经报错,但应用仍然不会停止 ,照常运行,但效果仅仅是两个ListView直接//替换,并无任何动画添加到其中,这个地方大家注意下): ObjectAnimator.ofFloat(invisiable, "rotationZ",-90f, 0f);

        visToInvis.setDuration(500);

        visToInvis.setInterpolator(accelerator);

        invisToVis.setDuration(500);

        invisToVis.setInterpolator(decelerator);

        //这个地方记录下,下面这个监听器小马第一次见到,查阅官方文档解释如下:此监听来监听动画的生命周期如:开始、结束、正在播放、循//环播放等 ,此处切记: Animation是不可以监听动画的,它只负责动画的

        visToInvis.addListener(new AnimatorListenerAdapter()

        {

            @Override

            public void onAnimationEnd(Animator anim)

            {

                 /*

                 * 列举几个动画的监听:

                 * 一:anim.isRunning(){//TODO}

                * 二:anim.isStarted(){//TODO}

                 * 三:anim.end(){//TODO}

                 */

                 visiable.setVisibility(View.GONE);

                 invisToVis.start();

                 invisiable.setVisibility(View.VISIBLE);

              }

        });

        visToInvis.start();

    }

}

猜你喜欢

转载自blog.csdn.net/lalate/article/details/83863644