ViewAnimation,可以在各種View 上做透明度、大小、位置、旋轉的動畫
可以透過xml定義,xml放於res/anim目錄下
4 種動畫效果
alpha 透明度
scale 大小縮放
translate 位置移動
rotate 旋轉
<alpha>
透明度變化的動畫效果(淡入淡出)
android:duration 動畫持續的時間(毫秒)
android:fromAlpha 動畫開始的透明度。0為完全透明,1為不透明。
android:toAlpha 動畫結束的透明度。0為完全透明,1為不透明。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<set xmlns:android="http://schemas.android.com/apk/res/android"> | |
<alpha | |
android:duration="2000" | |
android:fromAlpha="1.0" | |
android:toAlpha="0" /> | |
</set> |

<translate>
移動動畫效果
android:duration 動畫持續的時間(毫秒)
android:fromXDelta 起始位置偏移量
android:toXDelta 結束位置X偏移量
android:fromYDelta 起始位置Y偏移量
android:toYDelta 結束位置Y偏移量
以上的值可設定%設定相對View的百分比位置,或%P來設定相對Parent View的百分比位置
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<set xmlns:android="http://schemas.android.com/apk/res/android"> | |
<translate | |
android:duration="2000" | |
android:fromXDelta="0" | |
android:fromYDelta="0" | |
android:toXDelta="100%" | |
android:toYDelta="100%" /> | |
</set> |

<scale>
縮放的動畫效果
android:duration 動畫持續的時間(毫秒)
android:fromXScale 起始的X座標縮放倍數
android:toXScale 結束的X座標縮放倍數
android:fromYScale 起始的Y座標縮放倍數
android:toYScale 結束的Y座標縮放倍數
android:pivotX 縮放時不變的X坐標,用百分比表示,0%表示左邊緣,50%為中間,100%表示右邊緣)
android:pivotY 縮放時不變的Y坐標,用百分比表示,0%表示上邊緣,50%為中間,100%表示下邊緣)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<set xmlns:android="http://schemas.android.com/apk/res/android"> | |
<scale | |
android:duration="2000" | |
android:fromXScale="1.0" | |
android:fromYScale="1.0" | |
android:pivotX="50%" | |
android:pivotY="50%" | |
android:toXScale="3.0" | |
android:toYScale="3.0" /> | |
</set> |

<rotate>
旋轉的動畫效果
android:duration 動畫持續的時間(毫秒)
android:fromDegrees 旋轉開始的角度
android:toDegrees 旋轉結束的角度
android:pivotX 旋轉中心點的X座標,
android:pivotY 旋轉中心點的Y座標
pivot 值可設定%設定相對View的百分比位置,或%P來設定相對Parent View的百分比位置
旋轉720度,即2圈。pivotX="50%" 代表中心點在中間
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<set xmlns:android="http://schemas.android.com/apk/res/android"> | |
<rotate | |
android:duration="2000" | |
android:fromDegrees="0" | |
android:toDegrees="720" | |
android:pivotX="50%" | |
android:pivotY="50%" /> | |
</set> |

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
this.imageView = (ImageView) this.findViewById(R.id.imageView); | |
} | |
public void alpha(View view) { | |
this.imageView.startAnimation(AnimationUtils.loadAnimation(this, R.anim.alpha)); | |
} | |
} |
implements Animation.AnimationListener ,動畫開始、結束、重複的事件。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity implements Animation.AnimationListener { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
@Override | |
public void onAnimationStart(Animation animation) { | |
// Animation Start | |
} | |
@Override | |
public void onAnimationEnd(Animation animation) { | |
// Animation End | |
} | |
@Override | |
public void onAnimationRepeat(Animation animation) { | |
// Animation Repeat | |
} | |
} |
完整程式
https://github.com/evanchen76/AnimationSample
參考
https://developer.android.com/guide/topics/graphics/view-animation.html
沒有留言:
張貼留言