From d2f11d19774772dde4094d8f69037125452f8dc9 Mon Sep 17 00:00:00 2001 From: Nikolay Pultsin Date: Sat, 2 Apr 2011 15:31:41 +0100 Subject: [PATCH] animation refactoring: coordinates are moved into AnimationProvider class --- .../ui/android/view/AnimationProvider.java | 81 +++++++++++- .../ui/android/view/ZLAndroidWidget.java | 118 +++--------------- 2 files changed, 95 insertions(+), 104 deletions(-) diff --git a/src/org/geometerplus/zlibrary/ui/android/view/AnimationProvider.java b/src/org/geometerplus/zlibrary/ui/android/view/AnimationProvider.java index 8fe9c03cd..23e2b137e 100644 --- a/src/org/geometerplus/zlibrary/ui/android/view/AnimationProvider.java +++ b/src/org/geometerplus/zlibrary/ui/android/view/AnimationProvider.java @@ -36,7 +36,7 @@ abstract class AnimationProvider { Auto = auto; } } - private ScrollingMode myScrollingMode = ScrollingMode.NoScrolling; + private ScrollingMode myMode = ScrollingMode.NoScrolling; protected final Paint myPaint; protected int myStartX; @@ -48,24 +48,46 @@ abstract class AnimationProvider { protected int myWidth; protected int myHeight; + private float mySpeed; + protected AnimationProvider(Paint paint) { myPaint = paint; } ScrollingMode getScrollingMode() { - return myScrollingMode; + return myMode; } void setScrollingMode(ScrollingMode state) { - myScrollingMode = state; + myMode = state; } void terminate() { - myScrollingMode = ScrollingMode.NoScrolling; + myMode = ScrollingMode.NoScrolling; + mySpeed = 0; + } + + void startAutoScrolling(boolean forward, float speed, ZLView.Direction direction, int w, int h) { + if (!inProgress()) { + final int x, y; + if (direction.IsHorizontal) { + x = speed < 0 ? w : 0; + y = 0; + } else { + x = 0; + y = speed < 0 ? h : 0; + } + setup(x, y, x, y, direction, w, h); + } + + myMode = forward + ? ScrollingMode.AutoScrollingForward + : ScrollingMode.AutoScrollingBackward; + mySpeed = speed; } boolean inProgress() { - return myScrollingMode != ScrollingMode.NoScrolling; + return myMode != ScrollingMode.NoScrolling; } int getScrollingShift() { @@ -82,6 +104,55 @@ abstract class AnimationProvider { myHeight = height; } + void doStep() { + if (!myMode.Auto) { + return; + } + + switch (myDirection) { + case leftToRight: + myEndX -= (int)mySpeed; + break; + case rightToLeft: + myEndX += (int)mySpeed; + break; + case up: + myEndY += (int)mySpeed; + break; + case down: + myEndY -= (int)mySpeed; + break; + } + final int bound; + if (myMode == ScrollingMode.AutoScrollingForward) { + bound = myDirection.IsHorizontal ? myWidth : myHeight; + } else { + bound = 0; + } + if (mySpeed > 0) { + if (getScrollingShift() >= bound) { + if (myDirection.IsHorizontal) { + myEndX = myStartX + bound; + } else { + myEndY = myStartY + bound; + } + terminate(); + return; + } + } else { + if (getScrollingShift() <= -bound) { + if (myDirection.IsHorizontal) { + myEndX = myStartX - bound; + } else { + myEndY = myStartY - bound; + } + terminate(); + return; + } + } + mySpeed *= 1.5; + } + abstract void draw(Canvas canvas, Bitmap bgBitmap, Bitmap fgBitmap); abstract ZLView.PageIndex getPageToScrollTo(); diff --git a/src/org/geometerplus/zlibrary/ui/android/view/ZLAndroidWidget.java b/src/org/geometerplus/zlibrary/ui/android/view/ZLAndroidWidget.java index 2ce235ad8..027dcc894 100644 --- a/src/org/geometerplus/zlibrary/ui/android/view/ZLAndroidWidget.java +++ b/src/org/geometerplus/zlibrary/ui/android/view/ZLAndroidWidget.java @@ -37,16 +37,9 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener { private boolean mySecondaryBitmapIsUpToDate; private Bitmap myFooterBitmap; - private int myStartX; - private int myStartY; - private int myEndX; - private int myEndY; - private ZLView.PageIndex myPageToScrollTo = ZLView.PageIndex.current; private ZLView.Direction myScrollingDirection; - private float myScrollingSpeed; - public ZLAndroidWidget(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); @@ -142,51 +135,6 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener { return myAnimationProvider; } - private void doStep() { - switch (myScrollingDirection) { - case leftToRight: - myEndX -= (int)myScrollingSpeed; - break; - case rightToLeft: - myEndX += (int)myScrollingSpeed; - break; - case up: - myEndY += (int)myScrollingSpeed; - break; - case down: - myEndY -= (int)myScrollingSpeed; - break; - } - final int bound; - if (getAnimationProvider().getScrollingMode() == AnimationProvider.ScrollingMode.AutoScrollingForward) { - bound = myScrollingDirection.IsHorizontal ? getWidth() : getMainAreaHeight(); - } else { - bound = 0; - } - if (myScrollingSpeed > 0) { - if (getAnimationProvider().getScrollingShift() >= bound) { - if (myScrollingDirection.IsHorizontal) { - myEndX = myStartX + bound; - } else { - myEndY = myStartY + bound; - } - getAnimationProvider().terminate(); - return; - } - } else { - if (getAnimationProvider().getScrollingShift() <= -bound) { - if (myScrollingDirection.IsHorizontal) { - myEndX = myStartX - bound; - } else { - myEndY = myStartY - bound; - } - getAnimationProvider().terminate(); - return; - } - } - myScrollingSpeed *= 1.5; - } - private void onDrawInScrolling(Canvas canvas) { final ZLView view = ZLApplication.Instance().getCurrentView(); @@ -194,30 +142,28 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener { final int h = getMainAreaHeight(); final AnimationProvider animator = getAnimationProvider(); - final AnimationProvider.ScrollingMode mode = animator.getScrollingMode(); - if (mode.Auto) { - doStep(); - } + final AnimationProvider.ScrollingMode oldMode = animator.getScrollingMode(); + animator.doStep(); if (animator.inProgress()) { - animator.setup( - myStartX, myStartY, myEndX, myEndY, - myScrollingDirection, - getWidth(), getMainAreaHeight() - ); animator.draw(canvas, mySecondaryBitmap, myMainBitmap); if (animator.getScrollingMode().Auto) { postInvalidate(); } drawFooter(canvas); } else { - if (mode == AnimationProvider.ScrollingMode.AutoScrollingForward) { - Bitmap swap = myMainBitmap; - myMainBitmap = mySecondaryBitmap; - mySecondaryBitmap = swap; - view.onScrollingFinished(myPageToScrollTo); - ZLApplication.Instance().onRepaintFinished(); - } else { - view.onScrollingFinished(ZLView.PageIndex.current); + switch (oldMode) { + case AutoScrollingForward: + { + final Bitmap swap = myMainBitmap; + myMainBitmap = mySecondaryBitmap; + mySecondaryBitmap = swap; + view.onScrollingFinished(myPageToScrollTo); + ZLApplication.Instance().onRepaintFinished(); + break; + } + case AutoScrollingBackward: + view.onScrollingFinished(ZLView.PageIndex.current); + break; } setPageToScrollTo(ZLView.PageIndex.current); onDrawStatic(canvas); @@ -238,10 +184,6 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener { getAnimationProvider().setScrollingMode(AnimationProvider.ScrollingMode.ManualScrolling); myScrollingDirection = direction; - myStartX = startX; - myStartY = startY; - myEndX = endX; - myEndY = endY; getAnimationProvider().setup( startX, startY, @@ -276,46 +218,24 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener { switch (myPageToScrollTo) { case current: animator.terminate(); - myScrollingSpeed = 0; break; case previous: - animator.setScrollingMode(AnimationProvider.ScrollingMode.AutoScrollingBackward); - myScrollingSpeed = -3; + animator.startAutoScrolling(false, -3, direction, getWidth(), getMainAreaHeight()); break; case next: - animator.setScrollingMode(AnimationProvider.ScrollingMode.AutoScrollingBackward); - myScrollingSpeed = 3; + animator.startAutoScrolling(false, 3, direction, getWidth(), getMainAreaHeight()); break; } break; case previous: - animator.setScrollingMode(AnimationProvider.ScrollingMode.AutoScrollingForward); - myScrollingSpeed = 3; + animator.startAutoScrolling(true, 3, direction, getWidth(), getMainAreaHeight()); setPageToScrollTo(pageIndex); break; case next: - animator.setScrollingMode(AnimationProvider.ScrollingMode.AutoScrollingForward); - myScrollingSpeed = -3; + animator.startAutoScrolling(true, -3, direction, getWidth(), getMainAreaHeight()); setPageToScrollTo(pageIndex); break; } - if (doSetup && animator.inProgress()) { - if (myScrollingDirection.IsHorizontal) { - myStartX = myScrollingSpeed < 0 ? getWidth() : 0; - myStartY = 0; - } else { - myStartX = 0; - myStartY = myScrollingSpeed < 0 ? getMainAreaHeight() : 0; - } - myEndX = myStartX; - myEndY = myStartY; - getAnimationProvider().setup( - myStartX, myStartY, - myEndX, myEndY, - myScrollingDirection, - getWidth(), getMainAreaHeight() - ); - } drawOnBitmap(mySecondaryBitmap); postInvalidate(); }