[英]custom repeating of translation animation in android
我正在用不同的圖像(如水平輪盤)在屏幕上從左到右每次旋轉一個動畫。
在每個動畫周期中,我都需要從列表中將圖像更改為隨機圖像,因此我發現如果我使用重復動畫,那么在AnimationListener的onAnimationRepeat上設置新的ImageResource不會更新ImageView。 我決定使用onAnimationEnd的下一個動畫的自定義觸發。 這里有一堆代碼:
ImageView image = (ImageView)(ImageView)findViewById(R.id.imageId);
Animation aMid = AnimationUtils.loadAnimation(this, R.anim.plant_middle);
aMid.setFillEnabled(true);
aMid.setFillAfter(true);
aMid.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
setRandomImageResource();
}
@Override
public void onAnimationRepeat(Animation animation) { }
@Override
public void onAnimationEnd(Animation animation) {
if(customCounter < SPIN_COUNT){
customCounter++;
image.startAnimation(aMid);
}
}
});
而動畫ixm非常簡單,它是關於ImageView從屏幕左側到右邊:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true">
<translate
android:interpolator="@android:anim/linear_interpolator"
android:fromXDelta="-100%"
android:toXDelta="100%"
android:fromYDelta="0%"
android:toYDelta="0%"
android:duration="4000" />
</set>
問題在於 ,在每個動畫循環之間,ImageView會在屏幕中央出現很短的時間,就像從右到左飛行開始下一個動畫周期一樣,或者像它在最初位於中間的中間閃爍布局xml。
在onAnimationEnd上將其可見性設置為GONE,在onAnimationEnd上將其可見性設置為無法解決我們的問題。
做什么?
我將你的代碼用於我的應用程序,我會告訴你我是如何決定這個問題的。 首先,當您在XML文件中創建ImageView
標記時,您不能像這樣設置它的源:
<ImageView android:id="@+id/cloud_image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="20dip"
android:adjustViewBounds="false"
android:scaleType="fitXY" />
之后,您必須將ImageView
源設置為onAnimationStart()
函數,並且必須將空圖像(空和100%透明圖像)源設置為onAnimationEnd()
函數:
// == Animation listener ====================================================
mCloudsAnimation.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
mCloudsImage.setBackgroundResource(R.drawable.clouds);
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
mCloudsImage.setBackgroundResource(R.drawable.clear_sky);
mCloudsImage.startAnimation(mCloudsAnimation);
}
});
它適用於模擬器。 現在我要在我的設備上試一試。
將“setFillAfter(true)”添加到AnimationSet中(不僅僅是動畫)它對我有用。
我設法通過將布局邊距設置為-1000並返回onAnimationEnd / onAnimationStart而不是執行可見性來實現所需。 然而解決方案很難看。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.