android佈局改變動畫
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>
整理自教程
相關文章
- Android動態改變佈局Android
- 三種滑動方式的比較(scrollTo & view動畫 & 改變佈局引數)View動畫
- CSS3——針對窄屏來改變佈局CSSS3
- Android 佈局Android
- 鴻蒙HarmonyOS實戰-ArkUI動畫(佈局更新動畫)鴻蒙UI動畫
- 【譯】React Native佈局原理(以及Fabric將做出的改變)React Native
- Android佈局概述Android
- Android xml 佈局AndroidXML
- CSS 動畫方式改變 div 尺寸CSS動畫
- Android 動態佈局 動態生成 銷燬控制元件 改變控制元件的位置等Android控制元件
- Android中常見的佈局和佈局引數Android
- android佈局------RelativeLayout(相對佈局)詳解Android
- android筆記二(水平佈局與垂直佈局)Android筆記
- Flutter學習之佈局、互動、動畫Flutter動畫
- Android學習—— Android佈局Android
- Android 佈局優化Android優化
- android 介面佈局(大概)Android
- 我們在信創下的改變,新技術體系已佈局?
- Android佈局優化技巧Android優化
- Android之TableLayout(表格佈局)Android
- Android的佈局介紹Android
- android:四種基本佈局Android
- Android之佈局屬性Android
- Android GUI之View佈局AndroidGUIView
- Android響應式佈局Android
- android佈局屬性大全Android
- Android入門教程 | UI佈局之RelativeLayout 相對佈局AndroidUI
- 寫給 Android 開發的小程式佈局指南,Flex 佈局!AndroidFlex
- android 相對佈局,程式碼建立imageview,佈局居中問題AndroidView
- flutter佈局-8-animated_icons動畫圖片Flutter動畫
- 關於Android中xml佈局檔案之android 入門xml佈局檔案AndroidXML
- Android入門教程 | UI佈局之LinearLayout 線性佈局AndroidUI
- Android的四個基本佈局Android
- Android開發之常用佈局Android
- android --巧用 flexboxLayout 佈局AndroidFlex
- Android效能優——佈局優化Android優化
- Android 自定義氣泡佈局Android
- Android佈局檢測優化Android優化