I have a very long image.
I want to loop animation of moving it from left to right.
Here is a video: http://youtu.be/OvXY6-US-MQ
Please take a look at the "city line" image animation (bottom one). The problem is that free space when animation ends. How to make it really infinite?
Here is how I did what I have on video:
ivCity = (ImageView) findViewById(R.id.city);
ViewTreeObserver cityObserver = ivCity.getViewTreeObserver();
cityObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
Log.d(TAG, "ANIMATION PRE DRAW");
// Remove after the first run so it doesn't fire forever
ivCity.getViewTreeObserver().removeOnPreDrawListener(this);
cityWidth = ivCity.getMeasuredWidth();
Animation _translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, getScreenWidth(), Animation.ABSOLUTE, -cityWidth + getScreenWidth(), Animation.ABSOLUTE, 0f, Animation.ABSOLUTE, 0f);
//Animation _translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, getScreenWidth() - cityWidth, Animation.ABSOLUTE, 1.0f, Animation.ABSOLUTE, 0f, Animation.ABSOLUTE, 0f);
_translateAnimation.setDuration(10000);
_translateAnimation.setStartOffset(0);
_translateAnimation.setRepeatCount(Animation.INFINITE);
_translateAnimation.setRepeatMode(Animation.RESTART);
_translateAnimation.setInterpolator(new LinearInterpolator());
_translateAnimation.setFillAfter(true);
ivCity.startAnimation(_translateAnimation);
return true;
}
});
And the layout is:
<AbsoluteLayout
android:id="@+id/city_layout"
android:layout_marginBottom="15dp"
android:layout_above="@id/btn_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/city"
android:scaleType="center"
android:layout_width="1657.5dp"
android:layout_height="wrap_content"
android:src="@drawable/city"/>
</AbsoluteLayout>