android佈局改變動畫

銳湃發表於2015-10-08


MainActivity

package com.tz.dream.layout.animation;

import android.animation.LayoutTransition;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.GridLayout;
import android.widget.LinearLayout;

@SuppressLint("NewApi")
public class MainActivity extends Activity implements OnClickListener,
		OnCheckedChangeListener {

	private LayoutTransition layoutTransition = new LayoutTransition();
	private CheckBox appearingCB;
	private CheckBox disappearingCB;
	private CheckBox changeAppearingCB;
	private CheckBox changeDisappearingCB;
	private GridLayout gridLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}

	private void initView() {
		LinearLayout rootLayout = (LinearLayout) findViewById(R.id.ll_root_view);
		findViewById(R.id.bt_add).setOnClickListener(this);
		appearingCB = (CheckBox) findViewById(R.id.cb_appearing);
		disappearingCB = (CheckBox) findViewById(R.id.cb_disappearing);
		changeAppearingCB = (CheckBox) findViewById(R.id.cb_change_appearing);
		changeDisappearingCB = (CheckBox) findViewById(R.id.cb_change_disappearing);

		appearingCB.setOnCheckedChangeListener(this);
		disappearingCB.setOnCheckedChangeListener(this);
		changeAppearingCB.setOnCheckedChangeListener(this);
		changeDisappearingCB.setOnCheckedChangeListener(this);

		gridLayout = new GridLayout(this);
		// 列
		gridLayout.setColumnCount(4);
		// 行
		gridLayout.setRowCount(5);

		rootLayout.addView(gridLayout);

	}

	private int index = 1;

	@Override
	public void onClick(View v) {
		Button button = new Button(this);
		button.setText(String.valueOf(index));
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				gridLayout.removeView(v);
			}
		});
		gridLayout.addView(button, Math.min(1, gridLayout.getChildCount()));
		index++;
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		layoutTransition = new LayoutTransition();
		// LayoutTransition.APPEARING表示:當一個新的view在viewgroup中出現時,對當前新增的子view設定動畫
		// layoutTransition.setAnimator(
		// LayoutTransition.APPEARING,
		// appearingCB.isChecked() ? layoutTransition
		// .getAnimator(LayoutTransition.APPEARING) : null);
		layoutTransition.setAnimator(
				LayoutTransition.APPEARING,
				appearingCB.isChecked() ? ObjectAnimator.ofFloat(null,
						"scaleX", 0.0f, 1.0f) : null);
		// LayoutTransition.APPEARING表示:當一個子view在viewgroup中消失時,對當前消失的子view設定動畫
		layoutTransition.setAnimator(
				LayoutTransition.DISAPPEARING,
				disappearingCB.isChecked() ? layoutTransition
						.getAnimator(LayoutTransition.DISAPPEARING) : null);
		// LayoutTransition.APPEARING表示:當一個新的子view在viewgroup中出現時,
		// 這個時候新的子view對其他的view的位置造成了影響,這裡就是對被影響的view設定動畫
		layoutTransition.setAnimator(
				LayoutTransition.CHANGE_APPEARING,
				changeAppearingCB.isChecked() ? layoutTransition
						.getAnimator(LayoutTransition.CHANGE_APPEARING) : null);
		// LayoutTransition.APPEARING表示:當一個新的子view在viewgroup中消失時,
		// 這個時候新的子view對其他的view的位置造成了影響,這裡就是對被影響的view設定動畫
		layoutTransition.setAnimator(
				LayoutTransition.CHANGE_DISAPPEARING,
				changeDisappearingCB.isChecked() ? layoutTransition
						.getAnimator(LayoutTransition.CHANGE_DISAPPEARING)
						: null);

		gridLayout.setLayoutTransition(layoutTransition);
	}

}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ll_root_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/bt_add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="新增" />

    <CheckBox
        android:id="@+id/cb_appearing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="APPEARING" />

    <CheckBox
        android:id="@+id/cb_disappearing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="DISAPPEARING" />

    <CheckBox
        android:id="@+id/cb_change_appearing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="CHANGE_APPEARING" />

    <CheckBox
        android:id="@+id/cb_change_disappearing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="CHANGE_DISAPPEARING" />

</LinearLayout>

整理自教程


相關文章