在某些場景時犀呼,需要添加動畫效果,動畫執(zhí)行完成后薇组,圖片保持在最后一幀上外臂。
分兩種情況
1、單一動畫效果
使用xml文件時 在 alpha律胀、scale 宋光、translate 、rotate 節(jié)點上添加
android:fillAfter="true"
android:fillEnabled="true"
或者在代碼中設(shè)置
setFillAfter(true);
setFillEnabled(true);
2炭菌、動畫合集
需要在xml文件的set標(biāo)簽內(nèi)添加
android:fillAfter="true"
android:fillEnabled="true"
或者在代碼中設(shè)置
animationSet.setFillAfter(true);
animationSet.setFillEnabled(true);
動畫監(jiān)聽
set.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//動畫開始
}
@Override
public void onAnimationEnd(Animation animation) {
//動畫結(jié)束
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
關(guān)于更多關(guān)于動畫屬性罪佳,閱讀https://blog.csdn.net/harvic880925/article/details/39996643
代碼設(shè)置屬性動畫
/**
* 初始化動畫
*/
private void initAnima() {
// 動畫集合
AnimationSet set = new AnimationSet(false);// 參數(shù)是否需要插補器
// 旋轉(zhuǎn)動畫
RotateAnimation rotate = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
rotate.setDuration(1000);// 旋轉(zhuǎn)時長
rotate.setFillAfter(true);// 旋轉(zhuǎn)后保持原狀
set.addAnimation(rotate);
// 縮放動畫
ScaleAnimation scale = new ScaleAnimation(0, 1, 0, 1,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
scale.setDuration(1000);// 旋轉(zhuǎn)時長
scale.setFillAfter(true);// 旋轉(zhuǎn)后保持原狀
set.addAnimation(scale);
// 淡入淡出(漸變)動畫
AlphaAnimation alpha = new AlphaAnimation(0, 1);
alpha.setDuration(1000);// 旋轉(zhuǎn)時長
alpha.setFillAfter(true);// 旋轉(zhuǎn)后保持原狀
set.addAnimation(alpha);
// 動畫監(jiān)聽
set.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// 動畫開始
}
@Override
public void onAnimationRepeat(Animation animation) {
// 動畫重復(fù)
}
@Override
public void onAnimationEnd(Animation animation) {
// 動畫結(jié)束
}
});
rlRoot.startAnimation(set);
}
xml文件動畫
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:fillEnabled="true">
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000"
android:repeatCount="0">
</rotate>
<scale
android:fromXScale="1.0"
android:toXScale="0.5"
android:fromYScale="1.0"
android:toYScale="0.5"
android:pivotX="50%"
android:duration="5000"
android:pivotY="50%"
>
</scale>
</set>
```