广告
返回顶部
首页 > 资讯 > 移动开发 >Android GridView实现动画效果实现代码
  • 774
分享到

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

gridview动画Android 2022-06-06 11:06:47 774人浏览 八月长安
摘要

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

 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; 
 
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中GridView实现点击查看更多功能示例Android GridView简单实例Android中控件GridView实现设置行列分割线的方法示例Android使用GridView实现日历功能示例(详细代码)Android使用GridView实现日历的简单功能Android自定义DataGridView数据表格控件Android 利用ViewPager+GridView实现首页导航栏布局分页效果Android 中 GridView嵌套在ScrollView里只有一行的解决方法


--结束END--

本文标题: Android GridView实现动画效果实现代码

本文链接: https://www.lsjlt.com/news/28186.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android GridView实现动画效果实现代码
     Android GridView实现动画效果 项目中用到的一些动画,GridView的Item依次从屏幕外飞入到相应位置,附上相关代码: MainActivity...
    99+
    2022-06-06
    gridview 动画 Android
  • Android 四种动画效果的调用实现代码
    (1) main.xml 代码如下:(声明四个按钮控件) XML代码: 代码如下: <xml version="1.0" encoding="utf-8"> &l...
    99+
    2022-06-06
    调用 动画 Android
  • android GridView多选效果的实例代码
    具体代码如下: main.xml 代码如下:<LinearLayout xmlns:android="http://schemas.android.com/apk/r...
    99+
    2022-06-06
    gridview Android
  • Android xml实现animation的4种动画效果实例代码
    animation有四种动画类型:分别为alpha(透明的渐变)、rotate(旋转)、scale(尺寸伸缩)、translate(移动),二实现的分发有两种,一种是javaC...
    99+
    2022-06-06
    XML animation 动画 Android
  • Python+Pygame实现代码雨动画效果
    pygame实现代码雨动画 如视频所示 利用pygame库实现了一个代码呈雨状下落的视觉效果 部分代码如下 import sys import random import py...
    99+
    2022-11-11
  • Android实现动画效果详解
    目前Android平台提供了两类动画一类是Tween动画,第二类就是 Frame动画,具体内容介绍请看下文: 一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动...
    99+
    2022-06-06
    动画 Android
  • Android实现GridView中的item自由拖动效果
    之前的工作中,需要实现一个功能就是GridView中的item可以自由拖动, 思考了一下,其实实现起来不是很困难,主要工作就是交换节点,以及拖动时的移动效果,下面讲讲具体的实现...
    99+
    2022-06-06
    动效 gridview Android
  • CSS代码怎么实现loading动画效果
    本篇内容介绍了“CSS代码怎么实现loading动画效果”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!效果:代码使用了CSS的keyfram...
    99+
    2023-07-04
  • Python如何实现代码雨动画效果
    本篇内容介绍了“Python如何实现代码雨动画效果”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!代码如下import sysimp...
    99+
    2023-07-04
  • Android利用GridView实现单选效果
    1.实现如图所示的单选效果由于Android提供的单选按钮radiobutton只能单行或单列显示,且样式并不美观,故可用GridView进行改造,实现单选效果,而要实现这样的效果重点就在GridView的适配器这块了。首先是GridVie...
    99+
    2023-05-31
    gridview 单选 idv
  • Android实现Flip翻转动画效果
    本文实例讲述了Android实现Flip翻转动画效果的方法,分享给大家供大家学习借鉴。 具体实现代码如下: LinearLayout locationLL = (Linear...
    99+
    2022-06-06
    动画 Android
  • Android实现红包雨动画效果
    本文介绍了Android实现红包雨动画效果,分享给大家,希望对大家有帮助 红包雨 关于实现上面红包雨效果步骤如下: 1.创建一个红包实体类 public class Re...
    99+
    2022-06-06
    红包雨 动画 Android
  • Android ViewPager实现动画切换效果
    概述 ViewPager是Android开发中使用场景非常频繁的控件,单一的动画效果切换已经越来越不能满足追求个性化的应用中。而ViewPager自身也带有一个接口来处理页面间...
    99+
    2022-06-06
    viewpager 动画 Android
  • Android实现创意LoadingView动画效果
    Android上的热火锅煮萝卜蔬菜的Loading动画效果。 这是一个锅煮萝卜的Loading动画,效果仿照自之前IOS上看到的一个效果,觉得挺有意思,就移植过来了,在此完成了...
    99+
    2022-06-06
    Android
  • Android实现代码画虚线边框背景效果
    实现如下边框效果: 虚线画效果,可以使用Android中的xml来做。下面话不多说,直接上代码: <RelativeLayout android:id="...
    99+
    2022-06-06
    边框背景 Android
  • Android逐帧动画实现代码
    逐帧动画(Frame-by-frame Animations)顾名思义就是一帧接着一帧的播放图片,就像放电影一样。可以通过xml实现也可以通过java代码实现。逐帧动画适合实现...
    99+
    2022-06-06
    动画 Android
  • Android实现颜色渐变动画效果
    目录前言一、Android中插值器TypeEvaluator二、案例效果实现1.利用Android自带的颜色插值器ArgbEvaluator2.看看Android自带颜色插值器Arg...
    99+
    2022-11-13
  • Android Flutter如何实现3D动画效果
    这篇文章主要讲解了“Android Flutter如何实现3D动画效果”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android Flutter如何实现3D动画效果”吧...
    99+
    2023-06-29
  • Android 吸入动画效果实现分解
    Android 吸入动画效果详解 .  这里,我要介绍的是如何在Android上面实现一个类似的效果。先看看我实现的效果图。  上图演示了动画的某几帧,其中...
    99+
    2022-06-06
    动画 Android
  • Android怎么实现点赞动画效果
    今天小编给大家分享一下Android怎么实现点赞动画效果的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。一、前言对接下来功能实...
    99+
    2023-06-29
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作