Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android布局變化時動畫效果的現實(二)

Android布局變化時動畫效果的現實(二)

編輯:關於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如下:




    


scalexanimator.xml如下:




  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved