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: : 同上,相反,某个元素消失时,所对应的元素动画

为Viewgourp内组件添加动画的更多相关文章

  1. form-create教程:给内置组件和自定义组件添加事件

    本文将介绍form-create如何给内置组件和自定义组件添加事件 form-create 是一个可以通过 JSON 生成具有动态渲染.数据收集.验证和提交功能的表单生成器.并且支持生成任何 Vue ...

  2. [Mugeda HTML5技术教程之7]添加动画

    前一节我们讲述了怎么在新建的作品中添加元素,元素加好以后我们还想让他们动起来,来实现比较炫的效果.这节我们将要讲述怎么给元素添加动画.Mugeda动画是通过时间轴和帧来实现的.通过在时间轴上创建图层和 ...

  3. Html5页面内使用JSON动画的实现

    有一天我们的UI设计师找到我说,要把页面中我自己用程序写的动画,换成他们给的json动画,原因是有的动画很复杂,自己写起来达不到他们的预期效果(写到这里我突然想到一个问题,这么复杂的动画为什么不使用g ...

  4. XamarinAndroid组件教程RecylerView动画组件使用动画(3)

    XamarinAndroid组件教程RecylerView动画组件使用动画(3) (8)打开Main.axml文件,构建主界面.代码如下: <?xml version="1.0&quo ...

  5. XamarinAndroid组件教程RecylerView动画组件使用动画(2)

    XamarinAndroid组件教程RecylerView动画组件使用动画(2) 如果开发者要为RecylerView的子元素添加动画效果,需要使用RecyclerView类中的SetItemAnim ...

  6. React动画组件——React-Transitio-group动画实现

    React动画组件--React-Transitio-group动画实现 安装 项目目录下使用命令行 yarn add react-transition-group 安装组件.在需要使用动画的页面加入 ...

  7. Java 给PPT添加动画效果(预设动画/自定义动画)

    PPT幻灯片中对形状可设置动画效果,常见的动画效果为内置的固定类型,即动画效果和路径是预先设定好的固定模板,但在设计动画效果时,用户也可以按照自己的喜好自定义动画动作路径.下面,通过Java后端程序代 ...

  8. extjs组件添加事件监听的三种方式

    extjs对组件添加监听的三种方式  在定义组件的配置时设置 如代码中所示:  Java代码  xtype : 'textarea',  name : 'dataSetField',  labelSe ...

  9. SWT组件添加事件的四种方式

    在我们CS日常开发过程中会经常去为组件添加事件,我们常用的为AWT与SWT.SWT的事件模型是和标准的AWT基本一样的.下面将按照事件的四种写法来实现它. 一.匿名内部类的写法 new MouseAd ...

随机推荐

  1. nginx连接php测试

    1 nginx连接php [root@web01 /application/nginx/conf/conf.d]# cat docs.conf server { server_name docs.ol ...

  2. es6中的Object.assign

    在写一些插件的时候,我们会经常遇到所传参数需要合并默认参数,并覆盖相同参数的情况,在jQuery中我们可以使用$.extend(),在原生中要想使用得自己封装, 但自从es6出现了Object.ass ...

  3. BZOJ1101 [POI2007]Zap 和 CF451E Devu and Flowers

    Zap FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d.作为FGD的同学,FGD希望得到 ...

  4. JAVA的变量,数据类型与运算符

    1. 变量 计算机处理数据,变量被用来存储处理的数据,之所以叫做变量因为你可以改变存储的值.更确切的说,一个变量指向着一块存储特定类型值的地址,换句话说,一个变量有名称.类型和值.一个变量有一个名称, ...

  5. 对url路径中的参数进行加密--Java

    需求: 后台对一些比较敏感的参数进行数据加密,然后在传送到前端.当前端跳转到后台时,再由后台对其进行解密. 参考 针对url参数的加密解密算法(java版) 修改:对中间的js页面加密代码改写为jav ...

  6. 怎样运行jar包中的文件

    1. 2.编辑sysmodule.cmd文件 java -cp sysmodule.jar;classes12.jar;mysql-connector-java-5.0.3-bin.jar;jbcl. ...

  7. Elasticsearch-head使用及ES相关概念

    elasticsearch-head安装和介绍已在上一篇讲了. 在浏览器访问http://localhost:9100,可看到如下界面,表示启动成功: 仔细观察,我们会发现客户端默认连接的是我们ela ...

  8. js2py

    js2py

  9. 爬虫(十五):scrapy中的settings详解

    Scrapy设定(settings)提供了定制Scrapy组件的方法.你可以控制包括核心(core),插件(extension),pipeline及spider组件.设定为代码提供了提取以key-va ...

  10. 【原创】go语言学习(十七)接口应用实战--日志库

    目前 日志库需求分析 日志库接口设计 文件日志库开发 Console日志开发 日志使用以及测试 日志库需求分析 1.日志库需求分析 A. 程序运行是个黑盒B. 而日志是程序运行的外在表现C. 通过日志 ...