Android 自动动画布局更新 使用,在RecyclerView上使用布局动画(Layout animation)
自从Material Design发布以来,我对一些视频中演示的网格铺开动画感到惊讶。这是一种斜对角线动画,让activity从上到下从左到右铺开。非常漂亮。
我一直试图尝试所有能得到那种效果的方法。一种办法是,使用RecyclerView::notifyItemInserted()方法,这是很多人都提到的办法。但是这个方法没有提供太多控制动画顺序的方法,因此看起来并不是一个好办法。另一个就是在onBind()中必要的时候对每个元素使用动画,这也的确可行。但是那样的话代码就比较脆弱和过于侵入性(我们是在adapter中添加的动画)。要让它恰当的工作比较困难。
布局动画
最后,解决的办法居然比想象的简单。我得承认我很少用 布局动画(layout animation),因此我没能立即想到这个办法。但是在寻找答案的过程中,我发现了这个非常棒的代码:,它给我指明了解决方法。这里的问题是RecyclerView默认并没有使用 layout animation,但是这个代码可以让它能像GridView那样使用GridLayoutAnimation。我们提到的gist是这样的:/*
* Copyright (C) 2014 Freddie (Musenkishi) Lust-Hed
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.musenkishi.gists.view;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.GridLayoutAnimationController;
/**
* An extension of RecyclerView, focused more on resembling a GridView.
* Unlike [email protected] android.support.v7.widget.RecyclerView}, this view can handle
* [email protected] } as long as you provide it a
* [email protected] android.support.v7.widget.GridLayoutManager} in
* [email protected] setLayoutManager(LayoutManager layout)}.
*
* Created by Freddie (Musenkishi) Lust-Hed.
*/
public class GridRecyclerView extends RecyclerView {
public GridRecyclerView(Context context) {
super(context);
}
public GridRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GridRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void setLayoutManager(LayoutManager layout) {
if (layout instanceof GridLayoutManager){
super.setLayoutManager(layout);
} else {
throw new ClassCastException("You should only use a GridLayoutManager with GridRecyclerView.");
}
}
@Override
protected void attachLayoutAnimationParameters(View child, ViewGroup.LayoutParams params, int index, int count) {
if (getAdapter() != null && getLayoutManager() instanceof GridLayoutManager){
GridLayoutAnimationController.AnimationParameters animationParams =
(GridLayoutAnimationController.AnimationParameters) params.layoutAnimationParameters;
if (animationParams == null) {
animationParams = new GridLayoutAnimationController.AnimationParameters();
params.layoutAnimationParameters = animationParams;
}
int columns = ((GridLayoutManager) getLayoutManager()).getSpanCount();
animationParams.count = count;
animationParams.index = index;
animationParams.columnsCount = columns;
animationParams.rowsCount = count / columns;
final int invertedIndex = count - 1 - index;
animationParams.column = columns - 1 - (invertedIndex % columns);
animationParams.row = animationParams.rowsCount - 1 - invertedIndex / columns;
} else {
super.attachLayoutAnimationParameters(child, params, index, count);
}
}
}
配置布局动画
布局动画好的一面就是我们可以使用xml来定义与部署它们,因此我们的代码不会被动画穿插。我们只需用相应的布局动画定义xml。
android:columnDelay="15%"
android:rowDelay="15%"
android:animation="@anim/slide_in_bottom"
android:animationOrder="normal"
android:direction="top_to_bottom|left_to_right"/>
我们可以根据自己的喜好来自定义动画:
–columnDelay / rowDelay:行元素与列元素在动画时的延迟时间百分数。这样我们才能让下一行下一列view一个接一个的动画,而不是一起动画。
–animation: view出现在屏幕上的动画,我使用的是从底部滑出的动画。
– animationOrder: 可以是 normal, reverse 或者 random.
– direction: 指定item如何 基于列延迟显示出来,可取值:top_to_bottom, left_to_right,bottom_to_top,right_to_left。
这里是slide 动画的xml代码:
android:interpolator="@android:anim/decelerate_interpolator"
android:fromYDelta="100%p" android:toYDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
调整动画的时机
如果你执行现在的代码,你会发现app打开的同时布局动画也在执行,因此你其实看不到什么效果。对于Lollipop 之前的设备你没什么办法,没有有效的方法可以知道进入动画何时完成(至少我不知道)。但是从Lollipop 开始,我们可以使用onEnterAnimationComplete来检查。因此在onCreate中,如果SDK 版本旧于Lollipop,RecyclerView直接落定:if (Build.VERSION.SDK_INT
setRecyclerAdapter(recyclerView);
}
在Lollipop 或者更新设备,onEnterAnimationComplete会被调用。这是落定RecyclerView与请求新的布局动画的时机:@Override public void onEnterAnimationComplete() {
super.onEnterAnimationComplete();
setRecyclerAdapter(recyclerView);
recyclerView.scheduleLayoutAnimation();
}
总结
你可以轻易调整此布局动画来产生别的进入动画。可以尝试弄弄动画设置看看能得到些什么效果。
Android 自动动画布局更新 使用,在RecyclerView上使用布局动画(Layout animation)相关推荐
- h5动画 php,GitHub - calcyu/wechat_html5_animate: 微信上的HTML5动画,类似易企秀效果。...
wechat_html5_animate ##微信上的HTML5动画,类似易企秀效果. ##制作HTML5动画的动机 微信上收到了一个可以编辑祝福语的拜年H5,故想尝试一下如何制作. ##制作工具 A ...
- Android Studio App开发之循环试图RecyclerView,布局管理器LayoutManager、动态更新循环视图讲解及实战(附源码)
运行有问题或需要全部源码请点赞关注收藏后评论区留言~~~ 一.循环视图RecyclerView 尽管ListView和GridView分别实现了多行单列和多行多列的列表,使用也很简单,可是它们缺少变化 ...
- 认识Android(常用布局,控件,四大组件,动画,自定义控件及异常消息处理机制)
目录 一.布局 1.LinearLayout(线性布局): 2.相对布局(RelativeLayout) 3.GridLayout(网格布局) 4.FrameLayout(帧布局): 二.控件 1.T ...
- android 资产管理动图,这可能是你见过的迄今为止最简单的RecyclerView Item加载动画...
如何实现RecyclerView Item动画? 这个问题想必有很多人都会讲,我可以用ItemAnimator实现啊,这是RecyclerView官方定义的接口,专门扩展Item动画的,那我为什么要寻 ...
- Android动画效果-更新中
概述 Android系统提供了三种实现动画的方式,一种是补间动画(Tween Animation 在SDK中成为View Animation),另一种是帧动画(Frame Animation 在SDK ...
- android recyclerview多布局_图文讲解RecyclerView的复用机制 ||Recyclerview进阶
码个蛋(codeegg) 第 1085 次推 作者:susion 博客地址:https://juejin.im/post/5c1369cff265da613b6fa87f 本文是RecyclerVie ...
- android 5.0+原生质设计,cardview、Recyclerview、百分比布局PercentRelativeLayout简单使用
Android 5.0出来已经快一年时间了,全新的系统采用Material Design设计,耳目一新的感觉,当然新系统的出来也增加了新的API为开发带来便捷,现在就说说新出的几个控件,cardvie ...
- android开发 RecyclerView 瀑布列表布局
android开发 RecyclerView 瀑布列表布局 1.写一个内容的自定义小布局: <?xml version="1.0" encoding="utf-8& ...
- android gps 锁屏更新坐标_MIUI内测版更新日志解析,以及动画解说!
嗨咯各位小伙伴们大家好呀,今天主要先说一说"系统动效"这个词汇在MIUI官方宣传上用的是比较少的,更多时候都是叫"系统动画"好不管是怎么个叫法为了方便今天都一概 ...
最新文章
- tomcat通过conf-Catalina-localhost目录发布项目详解
- 关于html和CSS的几个基本知识点
- 【转】Phong和Blinn-Phong光照模型
- SharpDeveloeper开发ASP.NET MVC汗流浃背
- 计算机专业教学工作小结,计算机专业教学的工作总结
- OpenCV手部关键点检测(手势识别)代码示例
- 理解运算符优先级(简单易懂,建议收藏)
- java 判断是否为cst格式_Java判断文件编码格式
- JavaWeb request对象常用操作
- 数据库试题及答案 两套
- python制作表情,使用Python制作滑稽表情
- 计算机网络之物理层,数据链路层,网络层 学习笔记
- java printout_word中printout函数的相关参数介绍 | 学步园
- wildcard函数
- 《Java修炼指南:高频源码解析》阅读笔记一Java数据结构的实现集合类
- vector BLF 文件读写
- 数据库MySQL备份命令,手动备份MySQL数据库
- 错误处理机制perror的详解
- 使用RTOS系统如何选取大容量存储芯片NAND FLASH
- Neo4j【有与无】【N0】前言
热门文章
- 经过这几年的磨练_通过构建这15个项目来磨练您JavaScript技能
- azure服务器_如何使用Blazor WebAssembly实施Azure无服务器
- 清除数据和缓存是什么_什么是缓存数据? 清除缓存的含义是什么?
- react可视化_如何使用React和Popmotion.io可视化煎饼算法
- ai端到端_如何使用行为树构建端到端的对话式AI系统
- 创建react应用程序_如何将React应用程序部署到Netlify
- 初级开发人员的缺点_我希望成为初级开发人员的事情
- 如何锻炼膝盖和膝盖周围肌肉_我的分享膝盖照片的社交网络将如何拯救世界
- Python3网络爬虫开发实战,Cookies 池的搭建,破解反爬虫!
- Python高级——闭包与装饰器