我們在實際的開發(fā)過程中,有很多地方需要使用TranslateAnimation,本文是武林技術(shù)頻道小編為大家做的簡單介紹,下面是詳解Android 動畫之TranslateAnimation應(yīng)用的參數(shù)說明,希望對你學(xué)習(xí)這方面知識有幫助!
android中提供了4中動畫:
AlphaAnimation 透明度動畫效果
ScaleAnimation 縮放動畫效果
TranslateAnimation 位移動畫效果
RotateAnimation 旋轉(zhuǎn)動畫效果
本節(jié)講解TranslateAnimation動畫,TranslateAnimation比較常用,比如QQ,網(wǎng)易新聞菜單條的動畫,就可以用TranslateAnimation實現(xiàn),
通過TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) 來定義動畫
參數(shù)說明:
?
float fromXDelta 動畫開始的點(diǎn)離當(dāng)前View X坐標(biāo)上的差值
float toXDelta 動畫結(jié)束的點(diǎn)離當(dāng)前View X坐標(biāo)上的差值
float fromYDelta 動畫開始的點(diǎn)離當(dāng)前View Y坐標(biāo)上的差值
float toYDelta 動畫開始的點(diǎn)離當(dāng)前View Y坐標(biāo)上的差值
常用方法:
?
?
?
animation.setDuration(long durationMillis);//設(shè)置動畫持續(xù)時間
animation.setRepeatCount(int i);//設(shè)置重復(fù)次數(shù)
animation.setRepeatMode(Animation.REVERSE);//設(shè)置反方向執(zhí)行
Xml屬性:
?
?
?
android:duration:運(yùn)行動畫的時間
android:repeatCount:定義動畫重復(fù)的時間
代碼:
?
?
?
public class MainActivity extends Activity {
ImageView image;
Button start;
Button cancel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.main_img);
start = (Button) findViewById(R.id.main_start);
cancel = (Button) findViewById(R.id.main_cancel);
/** 設(shè)置位移動畫 向右位移150 */
final TranslateAnimation animation = new TranslateAnimation(0, 150,0, 0);
animation.setDuration(2000);//設(shè)置動畫持續(xù)時間
animation.setRepeatCount(2);//設(shè)置重復(fù)次數(shù)
animation.setRepeatMode(Animation.REVERSE);//設(shè)置反方向執(zhí)行
start.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
image.setAnimation(animation);
/** 開始動畫 */
animation.startNow();
}
});
cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
/** 結(jié)束動畫 */
animation.cancel();
}
});
}
}
效果:

上述是武林技術(shù)頻道小編給大家分享的詳解Android 動畫之TranslateAnimation應(yīng)用的參數(shù)說明,有需要的朋友可以操作一下,若是想獲得更多的Android訊息,可以持續(xù)關(guān)注武林技術(shù)頻道哦。