布局变化时的动画

布局中的每个view再不聚变化时有5中状态,应用程序可以为其中的任何一种状态设置自定义动画:

  • APPEARING 容器中出现一个新的view
  • DISAPPEARING 容器中消失一个view
  • CHANGING 布局改变导致某个视图随之改变
  • CHANGE_APPEARING 其他视图的出现导致某个视图随之改变
  • CHANGE_DISAPPEARING 其他视图的消失导致某个视图随之改变
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button" 
        android:onClick="addOnclick"/>

    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:animateLayoutChanges="true">
    LinearLayout>

LinearLayout>
package com.example.layoutanimator;

import android.animation.LayoutTransition;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity {
    LinearLayout layout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        layout = (LinearLayout) findViewById(R.id.layout);

        LayoutTransition transition = new LayoutTransition();

        ObjectAnimator apperani = ObjectAnimator.ofFloat(null, "rotationY", 90f,0f).setDuration(
                transition.getDuration(LayoutTransition.APPEARING));
        transition.setAnimator(LayoutTransition.APPEARING, apperani);

        ObjectAnimator disapper = ObjectAnimator.ofFloat(null, "rotationY", 0f,90f).setDuration(
                transition.getDuration(LayoutTransition.DISAPPEARING));
        transition.setAnimator(LayoutTransition.DISAPPEARING, disapper);
        PropertyValuesHolder holder = PropertyValuesHolder.ofFloat("y", 0,1);
        PropertyValuesHolder scalex = PropertyValuesHolder.ofFloat("ScaleX", 1f, 0.5f,1f);
        PropertyValuesHolder scaley = PropertyValuesHolder.ofFloat("scaleY", 1f,0.5f,1f);

        ObjectAnimator changeanimator = ObjectAnimator.ofPropertyValuesHolder(this, holder,scalex,scaley
                ).setDuration(transition.getDuration(LayoutTransition.CHANGE_DISAPPEARING));

        transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING, changeanimator);
        layout.setLayoutTransition(transition);



    }

    public void addOnclick(View v){
        Button btn = new Button(this);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                layout.removeView(v);
            }
        }); 

        layout.addView(btn,new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    }


}
其实在api 11以上设置android:animateLayoutChanges="true" 即可以简单的实现viewgroup改变时的动画效果,上方代码是为了自定义布局变化时某些状态下的动画效果

你可能感兴趣的:(android)