Android

Android Property Animation(屬性動畫)教學

1.平移

X軸

translateX.setOnClickListener {
    ObjectAnimator.ofFloat(icon, "translationX", 0f,200f,-200f,0f).setDuration(2000).start()
}

Y軸

translateY.setOnClickListener {
    ObjectAnimator.ofFloat(icon, "translationY", 0f,200f,-200f,0f).setDuration(2000).start()
}

2.透明度

alpha.setOnClickListener {
    ObjectAnimator.ofFloat(icon, "alpha", 0f,1f,0f,1f).setDuration(2000).start()
}

3.縮放

X軸

scaleX.setOnClickListener {
    ObjectAnimator.ofFloat(icon, "scaleX", 1f,3f,1f).setDuration(2000).start()
}

Y軸

scaleY.setOnClickListener {
    ObjectAnimator.ofFloat(icon, "scaleY", 1f,3f,1f).setDuration(2000).start()
}

X軸+Y軸

scale.setOnClickListener {
    ObjectAnimator.ofFloat(icon, "scaleX", 1f,3f,1f).setDuration(2000).start()
    ObjectAnimator.ofFloat(icon, "scaleY", 1f,3f,1f).setDuration(2000).start()
}

4.旋轉

X軸

rotateX.setOnClickListener {
    ObjectAnimator.ofFloat(icon, "rotationX", 0f, 360f).setDuration(1000).start()
}

Y軸

rotateY.setOnClickListener {
    ObjectAnimator.ofFloat(icon, "rotationY", 0f, 360f).setDuration(1000).start()
}

自轉

rotate.setOnClickListener {
    ObjectAnimator.ofFloat(icon, "rotation", 0f, 360f).setDuration(1000).start()
}

5.自定義

textColor.setOnClickListener {
    val textColor = ObjectAnimator.ofInt(it, "textColor", Color.RED, Color.YELLOW)
    textColor.repeatMode = ValueAnimator.REVERSE //顛倒
    textColor.repeatCount = -1 //無限重複
    textColor.duration = 3000;
    textColor.start()
}

6.同時執行

icon.setOnClickListener {
    val rotate =ObjectAnimator.ofFloat(icon, "rotation", 0f, 360f)
    val scaleX = ObjectAnimator.ofFloat(icon, "scaleX", 1f,3f,1f)
    val scaleY = ObjectAnimator.ofFloat(icon, "scaleY", 1f,3f,1f)

    val set = AnimatorSet()
    set.duration = 1000
    set.playTogether(rotate, scaleX, scaleY)
    set.start()
}

7.效果展示

發表迴響