Android GridView实现动画效果实现代码

来源:互联网 发布:txt阅读器 for mac 编辑:程序博客网 时间:2024/05/17 05:15

 Android GridView实现动画效果

项目中用到的一些动画,GridView的Item依次从屏幕外飞入到相应位置,附上相关代码:

MainActivity.Java

?
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
packagecom.mundane.gridanimationdemo;
  
importandroid.os.Bundle;
importandroid.support.v7.app.AppCompatActivity;
importandroid.view.View;
importandroid.view.animation.Animation;
importandroid.view.animation.TranslateAnimation;
importandroid.widget.Button;
importandroid.widget.GridView;
  
importjava.util.ArrayList;
importjava.util.List;
  
publicclass MainActivity extendsAppCompatActivity {
  
  privateGridView mGridView;
  privateList<String> mList;
  privateGridAdapter mGridAdapter;
  privateButton mBtnRefresh;
  
  @Override
  protectedvoid onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mGridView = (GridView) findViewById(R.id.grid_view);
    mBtnRefresh = (Button) findViewById(R.id.btn_refresh);
    mBtnRefresh.setOnClickListener(newView.OnClickListener() {
      @Override
      publicvoid onClick(View v) {
        mBtnRefresh.setVisibility(View.INVISIBLE);
        mGridAdapter.notifyDataSetChanged();
      }
    });
    mList = newArrayList<>();
    for(inti = 0; i < 9; i++) {
      mList.add(i + "");
    }
    mGridAdapter = newGridAdapter(mList);
    finalTranslateAnimation animation = newTranslateAnimation(
        Animation.RELATIVE_TO_PARENT,
        1.0f,
        Animation.RELATIVE_TO_PARENT,
        0,
        Animation.RELATIVE_TO_SELF,
        0,
        Animation.RELATIVE_TO_SELF,
        0);
    animation.setDuration(200);
    animation.setAnimationListener(newAnimation.AnimationListener() {
      @Override
      publicvoid onAnimationStart(Animation animation) {
        mBtnRefresh.setVisibility(View.VISIBLE);
      }
  
      @Override
      publicvoid onAnimationEnd(Animation animation) {
  
      }
  
      @Override
      publicvoid onAnimationRepeat(Animation animation) {
  
      }
    });
    mGridAdapter.setOnLastItemAnimationEndListener(newGridAdapter.OnLastItemAnimationEndListener() {
      @Override
      publicvoid onAnimationEnd() {
        mBtnRefresh.startAnimation(animation);
      }
    });
    mGridView.setAdapter(mGridAdapter);
  
  }
}

GridAdapter.java

?
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
packagecom.mundane.gridanimationdemo;
  
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.view.animation.Animation;
importandroid.view.animation.TranslateAnimation;
importandroid.widget.BaseAdapter;
importandroid.widget.TextView;
  
importjava.util.List;
  
/**
 * Created by Jackie on 2017/3/7 16:29
 */
  
publicclass GridAdapter extendsBaseAdapter{
  privateList<String> mList;
  
  publicGridAdapter(List<String> list) {
    mList = list;
  }
  
  @Override
  publicint getCount() {
    returnmList.size();
  }
  
  @Override
  publicObject getItem(intposition) {
    returnmList.get(position);
  }
  
  @Override
  publiclong getItemId(intposition) {
    returnposition;
  }
  
  @Override
  publicView getView(finalint position, View convertView, ViewGroup parent) {
    String text = mList.get(position);
    ViewHolder holder;
    if(convertView == null) {
      convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_desk_grid_item, parent,false);
      holder = newViewHolder(convertView);
      convertView.setTag(holder);
    }else{
      holder = (ViewHolder) convertView.getTag();
    }
    convertView.setVisibility(View.INVISIBLE);
    holder.textView.setText(text);
    intcount = 3- position % 3;
    finalTranslateAnimation translateAnimation = newTranslateAnimation(
        Animation.RELATIVE_TO_SELF,
        count,
        Animation.RELATIVE_TO_SELF,
        0,
        Animation.RELATIVE_TO_SELF,
        0,
        Animation.RELATIVE_TO_SELF,
        0);
    translateAnimation.setDuration(count*100);
//   final Animation animation = AnimationUtils.loadAnimation(parent.getContext(), R.anim.slide_in_right);
    finalView finalConvertView = convertView;
    convertView.postDelayed(newRunnable() {
      @Override
      publicvoid run() {
        finalConvertView.startAnimation(translateAnimation);
      }
    }, position * 200);
    translateAnimation.setAnimationListener(newAnimation.AnimationListener() {
      @Override
      publicvoid onAnimationStart(Animation animation) {
        finalConvertView.setVisibility(View.VISIBLE);
      }
  
      @Override
      publicvoid onAnimationEnd(Animation animation) {
        if(position == mList.size() - 1) {
          if(mListener != null) {
            mListener.onAnimationEnd();
          }
        }
      }
  
      @Override
      publicvoid onAnimationRepeat(Animation animation) {
  
      }
    });
  
    returnconvertView;
  }
  
  staticclass ViewHolder {
    TextView textView;
      
    publicViewHolder(View view) {
      textView = (TextView) view.findViewById(R.id.tv);
    }
  }
  
  publicinterface OnLastItemAnimationEndListener {
    voidonAnimationEnd();
  }
  
  privateOnLastItemAnimationEndListener mListener;
  
  publicvoid setOnLastItemAnimationEndListener(OnLastItemAnimationEndListener listener) {
    mListener = listener;
  }
}

参上上面的代码,还可以实现GridView Item的其他动画效果,注意//注释的部分,这个就是另外的动画效果,这里就不作过多的描述。

activity_main.xml

?
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
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.mundane.gridanimationdemo.MainActivity">
  
  <Button
    android:visibility="invisible"
    android:id="@+id/btn_refresh"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="刷新"/>
  
  <GridView
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:stretchMode="columnWidth"
    android:id="@+id/grid_view"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#f6f6f6"
    android:horizontalSpacing="10dp"
    android:numColumns="3"
    android:scrollbars="none"
    android:verticalSpacing="10dp">
  
  </GridView>
  
  
</LinearLayout>

card_desk_grid_item.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:background="#33000000"
  android:layout_width="match_parent"
  android:layout_height="156dp">
  <TextView
    android:id="@+id/tv"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</LinearLayout>

效果如下:

模拟器上运行很卡,真机上是很流畅的。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!