Android GridView实现动画效果实现代码
程序员文章站
2023-11-12 21:59:22
android gridview实现动画效果
项目中用到的一些动画,gridview的item依次从屏幕外飞入到相应位置,附上相关代码:
mainact...
android gridview实现动画效果
项目中用到的一些动画,gridview的item依次从屏幕外飞入到相应位置,附上相关代码:
mainactivity.java
package com.mundane.gridanimationdemo; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.view; import android.view.animation.animation; import android.view.animation.translateanimation; import android.widget.button; import android.widget.gridview; import java.util.arraylist; import java.util.list; public class mainactivity extends appcompatactivity { private gridview mgridview; private list<string> mlist; private gridadapter mgridadapter; private button mbtnrefresh; @override protected void 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(new view.onclicklistener() { @override public void onclick(view v) { mbtnrefresh.setvisibility(view.invisible); mgridadapter.notifydatasetchanged(); } }); mlist = new arraylist<>(); for (int i = 0; i < 9; i++) { mlist.add(i + ""); } mgridadapter = new gridadapter(mlist); final translateanimation animation = new translateanimation( 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(new animation.animationlistener() { @override public void onanimationstart(animation animation) { mbtnrefresh.setvisibility(view.visible); } @override public void onanimationend(animation animation) { } @override public void onanimationrepeat(animation animation) { } }); mgridadapter.setonlastitemanimationendlistener(new gridadapter.onlastitemanimationendlistener() { @override public void onanimationend() { mbtnrefresh.startanimation(animation); } }); mgridview.setadapter(mgridadapter); } }
gridadapter.java
package com.mundane.gridanimationdemo; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.view.animation.animation; import android.view.animation.translateanimation; import android.widget.baseadapter; import android.widget.textview; import java.util.list; /** * created by jackie on 2017/3/7 16:29 */ public class gridadapter extends baseadapter{ private list<string> mlist; public gridadapter(list<string> list) { mlist = list; } @override public int getcount() { return mlist.size(); } @override public object getitem(int position) { return mlist.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(final int 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 = new viewholder(convertview); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } convertview.setvisibility(view.invisible); holder.textview.settext(text); int count = 3 - position % 3; final translateanimation translateanimation = new translateanimation( 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); final view finalconvertview = convertview; convertview.postdelayed(new runnable() { @override public void run() { finalconvertview.startanimation(translateanimation); } }, position * 200); translateanimation.setanimationlistener(new animation.animationlistener() { @override public void onanimationstart(animation animation) { finalconvertview.setvisibility(view.visible); } @override public void onanimationend(animation animation) { if (position == mlist.size() - 1) { if (mlistener != null) { mlistener.onanimationend(); } } } @override public void onanimationrepeat(animation animation) { } }); return convertview; } static class viewholder { textview textview; public viewholder(view view) { textview = (textview) view.findviewbyid(r.id.tv); } } public interface onlastitemanimationendlistener { void onanimationend(); } private onlastitemanimationendlistener mlistener; public void setonlastitemanimationendlistener(onlastitemanimationendlistener listener) { mlistener = listener; } }
参上上面的代码,还可以实现gridview item的其他动画效果,注意//注释的部分,这个就是另外的动画效果,这里就不作过多的描述。
activity_main.xml
<?xml version="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
<?xml version="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>
效果如下:
模拟器上运行很卡,真机上是很流畅的。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
下一篇: 社交媒体营销策略 如何玩好裂变营销