package com.loaderman.customviewdemo;

import android.animation.Keyframe;
import android.animation.LayoutTransition;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout; public class MainActivity extends AppCompatActivity {
private LinearLayout linearLayoutContainer; private int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayoutContainer = (LinearLayout) findViewById(R.id.linearlayoutcontainer); // LayoutTransition transition = new LayoutTransition();
// //入场动画:view在这个容器中消失时触发的动画
// ObjectAnimator animIn = ObjectAnimator.ofFloat(null, "rotationY", 0f, 360f, 0f);
// transition.setAnimator(LayoutTransition.APPEARING, animIn);
//
// //出场动画:view显示时的动画
// ObjectAnimator animOut = ObjectAnimator.ofFloat(null, "rotation", 0f, 90f, 0f);
// transition.setAnimator(LayoutTransition.DISAPPEARING, animOut);
//
// PropertyValuesHolder pvhLeft = PropertyValuesHolder.ofInt("left", 0, 0);
// PropertyValuesHolder pvhTop = PropertyValuesHolder.ofInt("top", 0, 0);
// PropertyValuesHolder pvhScaleX = PropertyValuesHolder.ofFloat("scaleX", 1f, 0f, 1f);
// Animator changeAppearAnimator
// = ObjectAnimator.ofPropertyValuesHolder(linearLayoutContainer, pvhLeft, pvhTop, pvhScaleX);
// transition.setAnimator(LayoutTransition.CHANGE_APPEARING, changeAppearAnimator); LayoutTransition transition = new LayoutTransition();
PropertyValuesHolder outLeft = PropertyValuesHolder.ofInt("left", 0, 0);
PropertyValuesHolder outTop = PropertyValuesHolder.ofInt("top", 0, 0); Keyframe frame0 = Keyframe.ofFloat(0f, 0);
Keyframe frame1 = Keyframe.ofFloat(0.1f, -20f);
Keyframe frame2 = Keyframe.ofFloat(0.2f, 20f);
Keyframe frame3 = Keyframe.ofFloat(0.3f, -20f);
Keyframe frame4 = Keyframe.ofFloat(0.4f, 20f);
Keyframe frame5 = Keyframe.ofFloat(0.5f, -20f);
Keyframe frame6 = Keyframe.ofFloat(0.6f, 20f);
Keyframe frame7 = Keyframe.ofFloat(0.7f, -20f);
Keyframe frame8 = Keyframe.ofFloat(0.8f, 20f);
Keyframe frame9 = Keyframe.ofFloat(0.9f, -20f);
Keyframe frame10 = Keyframe.ofFloat(1, 0); PropertyValuesHolder mPropertyValuesHolder = PropertyValuesHolder.ofKeyframe("rotation", frame0, frame1, frame2, frame3, frame4, frame5, frame6, frame7, frame8, frame9, frame10);
ObjectAnimator mObjectAnimatorChangeDisAppearing = ObjectAnimator.ofPropertyValuesHolder(this, outLeft, outTop, mPropertyValuesHolder);
transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING, mObjectAnimatorChangeDisAppearing); transition.addTransitionListener(new LayoutTransition.TransitionListener() {
public void startTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) {
Log.d("loaderman", "start:" + "transitionType:" + transitionType + "count:" + container.getChildCount() + "view:" + view.getClass().getName());
} public void endTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) {
Log.d("loaderman", "end:" + "transitionType:" + transitionType + "count:" + container.getChildCount() + "view:" + view.getClass().getName());
}
}); linearLayoutContainer.setLayoutTransition(transition);
findViewById(R.id.add_btn).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addButtonView();
}
}); findViewById(R.id.remove_btn).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
removeButtonView();
}
}); } private void addButtonView() {
i++;
Button button = new Button(this);
button.setText("button" + i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
linearLayoutContainer.addView(button, 0);
} private void removeButtonView() {
if (i > 0) {
linearLayoutContainer.removeViewAt(0);
}
i--;
} }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <Button
android:id="@+id/add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加控件"/> <Button
android:id="@+id/remove_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="移除控件"/>
</LinearLayout> <LinearLayout
android:id="@+id/linearlayoutcontainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="false"
android:orientation="vertical"/> </LinearLayout>

效果:


Android提供四种方法为viewgroup组件添加动画

1.LayoutAnimationLayoutAnimationControllerz主要针对listview

2.gridLayoutAnimation主要针对gridview

3.android:animateLayoutChanges 属性  xml布局设置为true则带有默认动画,动画不能自定义

4.layoutTranstion最为强大,动画可以自定义

void setAnimator(int transitionType,Animator animator) 其中:transition取值如下

  • LayoutTransition.APPEARING :元素在容器中出现时所定义的动画
  • LayoutTransition.DISAPPEARING : 元素在容器中消失时所定义的动画
  • LayoutTransition.CHANGE_APPEARING : 由于元素中显示新的元素,其他需要变化的元素所对应的动画
  • LayoutTransition.CHANGE_DISAPPEARING: : 同上,相反,某个元素消失时,所对应的元素动画

最新文章

  1. MVC Api 的跨项目路由
  2. 10分钟学会前端调试利器——FireBug
  3. hihoCoder 1393 网络流三&#183;二分图多重匹配(Dinic求二分图最大多重匹配)
  4. 2017年1月5日 星期四 --出埃及记 Exodus 21:31
  5. flex 布局 初次接触这个好使又不是特别好用的布局方法
  6. Android之TextView灵活使用(转载)
  7. selenium在Eclipse中打开fireFox浏览器是报报错connect to host 127.0.0.1 on port 7055
  8. [Cocos2d-x For WP8]ActionManager动作管理
  9. windows下codelite的使用
  10. 【学习笔记】移动Web手册(PPK力作)
  11. android 项目学习随笔二十(屏幕适配)
  12. 移植yaffs文件系统
  13. 输出(test)
  14. Configure the Struts Tag Libraries
  15. PowerShell管理Exchange
  16. mysql数据一致性检查及修复
  17. 使用PowerShell快速部署Win12R2虚拟化桌面
  18. H: Dave的组合数组(二分法)
  19. r-mq实现顺序消费,不重复消费
  20. pandas中一列含有多种数据类型的转换:科学计算法转浮点数、字符映射

热门文章

  1. 1205 CSRF跨站请求与django中的auth模块使用
  2. matlab(8) Regularized logistic regression : 不同的λ(0,1,10,100)值对regularization的影响,对应不同的decision boundary\ 预测新的值和计算模型的精度predict.m
  3. java中的volatile变量
  4. Codeforces Round #533 (Div. 2) D. Kilani and the Game(BFS)
  5. 牛客练习赛33 E. tokitsukaze and Similar String (字符串哈希)
  6. 引入其他服务器的JS文件,同时也引入本地JS文件 报错时:
  7. php面向对象之$this-&gt;用法简述
  8. sql server if exists和 if not exists 的关键字用法
  9. jQuery相关方法4-----元素创建和移除
  10. 2019-2020 ICPC, Asia Jakarta Regional Contest