热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

优化后的标题:在Android中利用GridView实现动态视觉效果

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

项目中用到的一些动画,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 mList;private GridAdapter mGridAdapter;private Button mBtnRefresh;&#64;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mGridView &#61; (GridView) findViewById(R.id.grid_view);mBtnRefresh &#61; (Button) findViewById(R.id.btn_refresh);mBtnRefresh.setOnClickListener(new View.OnClickListener() {&#64;Overridepublic void onClick(View v) {mBtnRefresh.setVisibility(View.INVISIBLE);mGridAdapter.notifyDataSetChanged();}});mList &#61; new ArrayList<>();for (int i &#61; 0; i <9; i&#43;&#43;) {mList.add(i &#43; "");}mGridAdapter &#61; new GridAdapter(mList);final TranslateAnimation animation &#61; 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() {&#64;Overridepublic void onAnimationStart(Animation animation) {mBtnRefresh.setVisibility(View.VISIBLE);}&#64;Overridepublic void onAnimationEnd(Animation animation) {}&#64;Overridepublic void onAnimationRepeat(Animation animation) {}});mGridAdapter.setOnLastItemAnimationEndListener(new GridAdapter.OnLastItemAnimationEndListener() {&#64;Overridepublic 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 mList;public GridAdapter(List list) {mList &#61; list;}&#64;Overridepublic int getCount() {return mList.size();}&#64;Overridepublic Object getItem(int position) {return mList.get(position);}&#64;Overridepublic long getItemId(int position) {return position;}&#64;Overridepublic View getView(final int position, View convertView, ViewGroup parent) {String text &#61; mList.get(position);ViewHolder holder;if (convertView &#61;&#61; null) {convertView &#61; LayoutInflater.from(parent.getContext()).inflate(R.layout.card_desk_grid_item, parent, false);holder &#61; new ViewHolder(convertView);convertView.setTag(holder);} else {holder &#61; (ViewHolder) convertView.getTag();}convertView.setVisibility(View.INVISIBLE);holder.textView.setText(text);int count &#61; 3 - position % 3;final TranslateAnimation translateAnimation &#61; 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 &#61; AnimationUtils.loadAnimation(parent.getContext(), R.anim.slide_in_right);final View finalConvertView &#61; convertView;convertView.postDelayed(new Runnable() {&#64;Overridepublic void run() {finalConvertView.startAnimation(translateAnimation);}}, position * 200);translateAnimation.setAnimationListener(new Animation.AnimationListener() {&#64;Overridepublic void onAnimationStart(Animation animation) {finalConvertView.setVisibility(View.VISIBLE);}&#64;Overridepublic void onAnimationEnd(Animation animation) {if (position &#61;&#61; mList.size() - 1) {if (mListener !&#61; null) {mListener.onAnimationEnd();}}}&#64;Overridepublic void onAnimationRepeat(Animation animation) {}});return convertView;}static class ViewHolder {TextView textView;public ViewHolder(View view) {textView &#61; (TextView) view.findViewById(R.id.tv);}}public interface OnLastItemAnimationEndListener {void onAnimationEnd();}private OnLastItemAnimationEndListener mListener;public void setOnLastItemAnimationEndListener(OnLastItemAnimationEndListener listener) {mListener &#61; listener;}
}

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

activity_main.xml



card_desk_grid_item.xml




效果如下&#xff1a;

模拟器上运行很卡&#xff0c;真机上是很流畅的。





推荐阅读
author-avatar
liuleyi
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有