Android中的基本動畫參數:
![Android基本動畫設置](http://p2.ttnews.xyz/loading.gif)
AlphaAnimation(透明度動畫):
-1.首先需要new一個AlphaAnimation實例化對象,設置參數(起始的透明度,終止的透明度) 0表示完全透明,1表示完全不透明
- AlphaAnimation anim = new AlphaAnimation(0.3f,1);
-
-2. 然後設置動漫的運行時間,(單位毫秒)
- anim.setDuration(2000); //2秒
-
- 3.運行動畫
- tv.startAnimation(anim);
TranslateAnimation(偏移動畫):
1.實例化對象,設置參數(起始x座標,終止的x座標,起始y座標,終止的y座標)
- TranslateAnimation tAnim = new TranslateAnimation(0,100,0,200);
-
2.然後設置動漫的運行時間,(單位毫秒)
- tAnim.setDuration(2000); //2秒
-
3.運行動畫
- tv.startAnimation(tAnim);
-
ScaleAnimation(縮放動畫):
1.實例化對象,設置參數
//起始的寬方向的倍數,終止的寬方向的倍數,起始的高的倍數,終止的高的倍數
//後4個參數表示縮放的中心
//第5個參數、中心x的相對方式( Animation.RELATIVE_TO_SELF相對自身)
//第6個參數、中心x的相對比例
//第7個參數、中心y的相對方式(Animation.RELATIVE_TO_PARENT相對父容器)
//第8個參數、中心y的相對比例
ScaleAnimation sAnim = new ScaleAnimation(0.1f,1.5f,0.1f,1.5f,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_PARENT,0.5f);
2運行時間
sAnim.setDuration(2000);
3.運行動畫
tv.startAnimation(sAnim);
RotateAnimation(旋轉動畫)
1.實例化對象,設置參數
- //起始角度,終止角度
//後4個參數表示縮放的中心
//3、中心x的相對方式( Animation.RELATIVE_TO_SELF相對自身)
//4、中心x的相對比例
//5、中心y的相對方式(Animation.RELATIVE_TO_PARENT相對父容器)
//6、中心y的相對比例
RotateAnimation rAnim = new RotateAnimation(0,360,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_PARENT,0.5f);
2.運行時間
rAnim.setDuration(2000);
3.運行動畫
tv.startAnimation(rAnim);
可以使用setFillAfter定住效果:
//保存動畫之後的狀態(定住效果,但是本身沒有變化)
tAnim.setFillAfter(true);
動畫重複:
tAnim.setRepeatCount(2); //重複次數
tAnim.setRepeatMode(Animation.REVERSE); //重複模式(REVERSE原路返回 RESTART重新開始)
動畫的監聽:
tAnim.setAnimationListener(new Animation.AnimationListener() {
@Override public void onAnimationStart(Animation animation) {
//動畫開始 }
@Override
public void onAnimationEnd(Animation animation) {
Log.e(“m_tag”, “end”);
//動畫結束
tv.layout(100, 200, 100 + tv.getMeasuredWidth(), 200 + tv.getMeasuredHeight());
tv.invalidate();
}
@Override
public void onAnimationRepeat(Animation animation){
//動畫循環(配合setRepeatCount來用)
}
}};
閱讀更多 九雲IT 的文章