iT邦幫忙

2021 iThome 鐵人賽

DAY 25
0
Mobile Development

android studio 30天學習筆記系列 第 25

30天學習筆記 介紹-day 25-View Animation

View Animation可以對view做透明度、缩放、平移、旋转等動畫。

xml動畫文件需再res/anim目錄中(在res新增Directory名為anim)

成果

1.透明度

淡入淡出的動畫
android:fromAlpha="" 動畫開始的透明度,0(完全透明)~1(不透明)
android:toAlpha="" 動畫結束的透明度,0(完全透明)~1(不透明)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="2000" //動畫持續時間2秒
        android:fromAlpha="1.0"
        android:toAlpha="0" />
</set>

2.平移

做向量間的移動動畫
android:fromXDelta="" x的起始座標值
android:fromYDelta="" y的起始座標值
android:toXDelta="" x的結束座標值
android:toYDelta="" y的結束座標值

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="2000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="200"
        android:toYDelta="100"/>
    
</set>

3.縮放

物件變大變小的動畫
android:fromXScale="" X起始座標
android:fromYScale="" Y起始座標
android:pivotX="" 縮放時中心的X座標
android:pivotY="" 縮放時中心的Y座標
android:toXScale="" X結束座標
android:toYScale="" Y結束座標

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="2000"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:pivotX="50%"
        android:pivotY="10%"
        android:toXScale="2.0"
        android:toYScale="2.0"/>  
</set>

4.旋轉

旋轉的動畫
android:fromDegrees="" 旋轉起始的角度
android:toDegrees="" 旋轉結束的角度
android:pivotX="" 旋轉中心的x座標
android:pivotY="" 旋轉中心的Y座標

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="100%"
        android:pivotY="100%"
        android:toDegrees="720"/>
</set>

Test1Activity.class

調用AnimationUtils類的靜態函數loadAnimation(),讓元件進行動畫的顯示,

public class Test1Activity extends AppCompatActivity {
    Button scalebtn,translatebtn,alphabtn,rotatebtn;
    private Animation animationAlpha,animationTranslate,animationScale,animationRotate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test1);
        component();
        AnimationSet();
    }

    private void AnimationSet() {
        animationScale=AnimationUtils.loadAnimation(this,R.anim.viewanimationscale);
        animationTranslate=AnimationUtils.loadAnimation(this,R.anim.viewanimationtranslate);
        animationAlpha=AnimationUtils.loadAnimation(this,R.anim.viewanimationalpha);
        animationRotate=AnimationUtils.loadAnimation(this,R.anim.viewanimationrotate);

        scalebtn.startAnimation(animationScale);
        translatebtn.startAnimation(animationTranslate);
        alphabtn.startAnimation(animationAlpha);
        rotatebtn.startAnimation(animationRotate);
    }
    private void component() {
        scalebtn=findViewById(R.id.scale);
        translatebtn=findViewById(R.id.translate);
        alphabtn=findViewById(R.id.alpha);
        rotatebtn=findViewById(R.id.rotate);
    }
}

上一篇
30天學習筆記 -day 24-Dagger (下篇)
下一篇
30天學習筆記 -day 26-Motion Editor(上篇)
系列文
android studio 30天學習筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言