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

 更新时间:2017年03月16日 08:37:11   投稿:lqh  
这篇文章主要介绍了 Android GridView实现动画效果实现代码的相关资料,需要的朋友可以参考下

 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> 

效果如下:

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

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

相关文章

  • Android自定义View实现气泡动画

    Android自定义View实现气泡动画

    这篇文章主要为大家详细介绍了Android自定义View实现气泡动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-04-04
  • Android使用ViewDragHelper实现QQ6.X最新版本侧滑界面效果实例代码

    Android使用ViewDragHelper实现QQ6.X最新版本侧滑界面效果实例代码

    这篇文章主要介绍了Android程序开发实现QQ6.X最新版本侧滑界面效果实例代码的相关资料,需要的朋友可以参考下
    2016-02-02
  • Android View进行手势识别详解

    Android View进行手势识别详解

    本文主要介绍 Android View进行手势识别,这里整理了相关资料和简单示例,有兴趣的小伙伴可以参考下
    2016-08-08
  • Android的WebView与H5前端JS代码交互的实例代码

    Android的WebView与H5前端JS代码交互的实例代码

    本篇文章主要介绍了Android的WebView与H5前端JS代码交互的实例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-07-07
  • 基于Android本地代码生成器详解

    基于Android本地代码生成器详解

    本篇文章是对Android本地代码生成器的使用进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • Android网络编程之简易新闻客户端

    Android网络编程之简易新闻客户端

    这篇文章主要为大家详细介绍了Android网络编程之简易新闻客户端的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • android顶部(toolbar)搜索框实现代码

    android顶部(toolbar)搜索框实现代码

    这篇文章主要介绍了android顶部(toolbar)搜索框实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • Android自定义图片集合

    Android自定义图片集合

    这篇文章主要为大家分享了内容相当丰富的Android自定义图片集合,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-02-02
  • 浅析Android.mk

    浅析Android.mk

    Android.mk是Android提供的一种makefile文件,用来指定诸如编译生成so库名、引用的头文件目录、需要编译的.c/.cpp文件和.a静态库文件等。要掌握jni,就必须熟练掌握Android.mk的语法规范
    2016-01-01
  • 浅谈Android PathMeasure详解和应用

    浅谈Android PathMeasure详解和应用

    本篇文章主要介绍了浅谈Android PathMeasure详解和应用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01

最新评论