Android布局变化时动画效果的现实(二)

MainActivity如下:

package wy.test;

import android.animation.AnimatorInflater;
import android.animation.LayoutTransition;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
 * Demo描述:
 * 布局变化时动画效果的现实(二)
 * 当布局发生变化时展示一个动画,不至于切换效果太生硬.
 * 当然类似的还可以设置布局变化的不同时刻的动画,一共有四种:
 * LayoutTransition.APPEARING,LayoutTransition.CHANGE_APPEARING,
 * LayoutTransition.CHANGE_DISAPPEARING,LayoutTransition.DISAPPEARING
 * 
 * 
 * 参考资料:
 * 1 http://blog.csdn.net/alrdy/article/details/7836963
 * 2 http://www.cnblogs.com/mengdd/p/3305973.html
 * 3 http://blog.csdn.net/think_soft/article/details/12417107
 * 
 *   Thank you very much
 */
public class TestAnimateLayoutChangesActivity extends Activity {
	private LinearLayout mRootLinearLayout;
	private Context mContext;
	private TextView mTextView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
    }
    private void init(){
    	mContext=this;
    	mRootLinearLayout=(LinearLayout) findViewById(R.id.rootLinearLayout);
    	mTextView=(TextView) findViewById(R.id.textView);
    	mTextView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Button addedButton=new Button(mContext);
				addedButton.setText("Button");
				//属性动画ObjectAnimator
				ObjectAnimator objectAnimator=(ObjectAnimator)AnimatorInflater.loadAnimator(mContext, R.animator.scalexanimator); 
				objectAnimator.setTarget(addedButton);
				LayoutTransition layoutTransition=new LayoutTransition();
				//指定为新加View出现时展示动画
		    	layoutTransition.setAnimator(LayoutTransition.APPEARING, objectAnimator);
		    	//为布局添加LayoutTransition
		    	mRootLinearLayout.setLayoutTransition(layoutTransition);
				mRootLinearLayout.addView(addedButton);
			}
		});
    }
}

main.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootLinearLayout"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
   
   >

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="测试布局中的animateLayoutChanges属性\n注意测试环境为Android4.0" />

</LinearLayout>

scalexanimator.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="scaleX"
    android:duration="3000"
    android:valueFrom="1.0"
    android:valueTo="0.5"
     >
</objectAnimator>


你可能感兴趣的:(Android布局变化时动画效果的现实(二))