zoukankan      html  css  js  c++  java
  • 为Viewgourp内组件添加动画

    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: : 同上,相反,某个元素消失时,所对应的元素动画
  • 相关阅读:
    javascript定义类和实例化类
    c# 注册表操作,创建,删除,修改,判断节点是否存在
    asp.net 获得文件属性中的修改时间,获得系统文件属性的方法,最后一次写入时间
    研究“QQ开心农场”一点心得
    c# winform socket网络编程,点对点传输文件,socket文件传输,监听端口
    用C#创建Windows服务(Windows Services)
    c# webform js文件获取客户端控件,后台cs获取前台客户端控件的值,c#和js的交互
    c# asp.net webform web页面打印,可以控制需要打印和不需要打印的位置
    c# asp.net 调用系统设置字体文本框,设置label或页面字体,大小,FontDialog
    c# 计算程序执行时间,计算一段代码执行所用的时间,测试效率
  • 原文地址:https://www.cnblogs.com/loaderman/p/10207414.html
Copyright © 2011-2022 走看看