Skip to content

Instantly share code, notes, and snippets.

@brucetoo
Last active September 9, 2015 01:09
Show Gist options
  • Save brucetoo/23ce1a375a32b28d6d3a to your computer and use it in GitHub Desktop.
Save brucetoo/23ce1a375a32b28d6d3a to your computer and use it in GitHub Desktop.
LayoutAnimation的使用
layoutAnimation 是android 中专门针对一组控件的动画控制
RecyclerView中使用 GridLayoutAnimation // https://github.com/antoniolg/MaterializeYourApp/commit/4a66a35f811cd4552ba54d7539b77345300c9d57
1. XML 配置
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="20%"
android:animation="@anim/anim_right_2_left"
android:animationOrder="normal">
</layoutAnimation>
delay 是每个子控件之前动画间隔的时间
animation 是每个子控件执行的动画配置文件
animationOrder 是子控件执行的顺序 有三个默认实现 normal reverse random
anim_right_2_left.xml
普通移动动画
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXDelta="100%"
android:toXDelta="0">
</translate>
2.代码中实现
container = (LinearLayout) findViewById(R.id.container);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_right_2_left);
LayoutAnimationController controller = new LayoutAnimationController(animation);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
controller.setDelay(0.2f);
container.setLayoutAnimation(controller);
container.startLayoutAnimation();
3.扩展使用
LayoutAnimationController默认只实现了三个普通的顺序,现在需要自定义子控件的执行顺序
如:http://material-design.storage.googleapis.com/videos/animation-meaningfultransitions-hierarchicaltiming4do_large_xhdpi.webm 所示的material 风格的动画
需要自定义控件继承 LayoutAnimationController,如下:
public class CustomLayoutAnimateController extends LayoutAnimationController {
public final static int ORDER_CUSTOM = 3; //自定义子控件的排序
private IndexCallBack indexCallBackListener;
public void setIndexCallBackListener(IndexCallBack indexCallBackListener) {
this.indexCallBackListener = indexCallBackListener;
}
public CustomLayoutAnimateController(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomLayoutAnimateController(Animation animation) {
super(animation);
}
public CustomLayoutAnimateController(Animation animation, float delay) {
super(animation, delay);
}
@Override
protected int getTransformedIndex(AnimationParameters params) {
if(indexCallBackListener != null && getOrder() == ORDER_CUSTOM){
return indexCallBackListener.currentIndex(this,params.count,params.index);
}else {
return super.getTransformedIndex(params);
}
}
/**
* 当需要自定义子控件的显示方式的时候
* 回调此方法...
*/
public interface IndexCallBack
{
int currentIndex(CustomLayoutAnimateController controller, int count, int index);
}
}
然而这其实并没有什么卵用,因为有一个更好的方法实现,就是
gridLayoutAnimation 来实现
4.GridLayoutAnimation 的使用
<gridLayoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:columnDelay="15%"
android:rowDelay="15%"
android:animation="@anim/scale_x_y"
android:animationOrder="normal"
android:direction="top_to_bottom|left_to_right"
android:interpolator="@android:interpolator/linear"
/>
columnDelay -列方向的延迟比例
rowDelay - 行方向的延迟比例
animation - 每个item执行的动画
animationOrder - 每个item执行的模式
direction - 动画执行的方向.....这个很重要
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment