進階篇-用戶界面:9.android動畫-佈局動畫

1.爲佈局添加動畫效果android

(1)在佈局文件中添加一排縱向的按鈕。app

(2)在MainActivity中給佈局添加動畫效果。這個佈局的動畫效果影響的是該佈局下全部子對象的。dom

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity{
    private LinearLayout la ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        la = (LinearLayout) findViewById(R.id.liner);
        ScaleAnimation sa = new ScaleAnimation(0,1,0,1);
        sa.setDuration(1000);
        LayoutAnimationController lac = new LayoutAnimationController(sa,0.5f);//0.5f is the delay of layoutAnimation,the second button begins to show up when the first
                                                                               //button is to half the time.
        lac.setOrder(LayoutAnimationController.ORDER_NORMAL);                  //Class LayoutAnimationController gives three kinds of order:nomal,random and reverse.
        la.setLayoutAnimation(lac);
    }
}

效果是按鈕一個一個出現,而且帶有縮放效果ide

2.佈局內容改變更畫佈局

在佈局文件中添加:動畫

android:animateLayoutChanges="true"

這樣的話,在源碼中更改佈局,添加和刪除子對象,就會有動畫效果,默認的動畫效果是透明度變化,若是想要自定義動畫效果,參考爲佈局添加動畫效果。spa

3.使用資源文件佈局動畫code

(1)在res文件夾下建立一個scale.xml:xml

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="0"
    android:toXScale="1"
    android:fromYScale="0"
    android:toYScale="1"
    android:duration="1000"/>

(2)再建立一個animcontroller.xml對象

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation  xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/scale"
    android:delay="0.5">

</layoutAnimation>

(3)再佈局文件中添加屬性:

android:layoutAnimation="@anim/animcontroller"
相關文章
相關標籤/搜索