1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 10:49:24 +02:00

animation refactoring: coordinates are moved into AnimationProvider class

This commit is contained in:
Nikolay Pultsin 2011-04-02 15:31:41 +01:00
parent b160192abe
commit d2f11d1977
2 changed files with 95 additions and 104 deletions

View file

@ -36,7 +36,7 @@ abstract class AnimationProvider {
Auto = auto; Auto = auto;
} }
} }
private ScrollingMode myScrollingMode = ScrollingMode.NoScrolling; private ScrollingMode myMode = ScrollingMode.NoScrolling;
protected final Paint myPaint; protected final Paint myPaint;
protected int myStartX; protected int myStartX;
@ -48,24 +48,46 @@ abstract class AnimationProvider {
protected int myWidth; protected int myWidth;
protected int myHeight; protected int myHeight;
private float mySpeed;
protected AnimationProvider(Paint paint) { protected AnimationProvider(Paint paint) {
myPaint = paint; myPaint = paint;
} }
ScrollingMode getScrollingMode() { ScrollingMode getScrollingMode() {
return myScrollingMode; return myMode;
} }
void setScrollingMode(ScrollingMode state) { void setScrollingMode(ScrollingMode state) {
myScrollingMode = state; myMode = state;
} }
void terminate() { 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() { boolean inProgress() {
return myScrollingMode != ScrollingMode.NoScrolling; return myMode != ScrollingMode.NoScrolling;
} }
int getScrollingShift() { int getScrollingShift() {
@ -82,6 +104,55 @@ abstract class AnimationProvider {
myHeight = height; 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 void draw(Canvas canvas, Bitmap bgBitmap, Bitmap fgBitmap);
abstract ZLView.PageIndex getPageToScrollTo(); abstract ZLView.PageIndex getPageToScrollTo();

View file

@ -37,16 +37,9 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
private boolean mySecondaryBitmapIsUpToDate; private boolean mySecondaryBitmapIsUpToDate;
private Bitmap myFooterBitmap; private Bitmap myFooterBitmap;
private int myStartX;
private int myStartY;
private int myEndX;
private int myEndY;
private ZLView.PageIndex myPageToScrollTo = ZLView.PageIndex.current; private ZLView.PageIndex myPageToScrollTo = ZLView.PageIndex.current;
private ZLView.Direction myScrollingDirection; private ZLView.Direction myScrollingDirection;
private float myScrollingSpeed;
public ZLAndroidWidget(Context context, AttributeSet attrs, int defStyle) { public ZLAndroidWidget(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle); super(context, attrs, defStyle);
init(); init();
@ -142,51 +135,6 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
return myAnimationProvider; 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) { private void onDrawInScrolling(Canvas canvas) {
final ZLView view = ZLApplication.Instance().getCurrentView(); final ZLView view = ZLApplication.Instance().getCurrentView();
@ -194,30 +142,28 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
final int h = getMainAreaHeight(); final int h = getMainAreaHeight();
final AnimationProvider animator = getAnimationProvider(); final AnimationProvider animator = getAnimationProvider();
final AnimationProvider.ScrollingMode mode = animator.getScrollingMode(); final AnimationProvider.ScrollingMode oldMode = animator.getScrollingMode();
if (mode.Auto) { animator.doStep();
doStep();
}
if (animator.inProgress()) { if (animator.inProgress()) {
animator.setup(
myStartX, myStartY, myEndX, myEndY,
myScrollingDirection,
getWidth(), getMainAreaHeight()
);
animator.draw(canvas, mySecondaryBitmap, myMainBitmap); animator.draw(canvas, mySecondaryBitmap, myMainBitmap);
if (animator.getScrollingMode().Auto) { if (animator.getScrollingMode().Auto) {
postInvalidate(); postInvalidate();
} }
drawFooter(canvas); drawFooter(canvas);
} else { } else {
if (mode == AnimationProvider.ScrollingMode.AutoScrollingForward) { switch (oldMode) {
Bitmap swap = myMainBitmap; case AutoScrollingForward:
{
final Bitmap swap = myMainBitmap;
myMainBitmap = mySecondaryBitmap; myMainBitmap = mySecondaryBitmap;
mySecondaryBitmap = swap; mySecondaryBitmap = swap;
view.onScrollingFinished(myPageToScrollTo); view.onScrollingFinished(myPageToScrollTo);
ZLApplication.Instance().onRepaintFinished(); ZLApplication.Instance().onRepaintFinished();
} else { break;
}
case AutoScrollingBackward:
view.onScrollingFinished(ZLView.PageIndex.current); view.onScrollingFinished(ZLView.PageIndex.current);
break;
} }
setPageToScrollTo(ZLView.PageIndex.current); setPageToScrollTo(ZLView.PageIndex.current);
onDrawStatic(canvas); onDrawStatic(canvas);
@ -238,10 +184,6 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
getAnimationProvider().setScrollingMode(AnimationProvider.ScrollingMode.ManualScrolling); getAnimationProvider().setScrollingMode(AnimationProvider.ScrollingMode.ManualScrolling);
myScrollingDirection = direction; myScrollingDirection = direction;
myStartX = startX;
myStartY = startY;
myEndX = endX;
myEndY = endY;
getAnimationProvider().setup( getAnimationProvider().setup(
startX, startY, startX, startY,
@ -276,46 +218,24 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
switch (myPageToScrollTo) { switch (myPageToScrollTo) {
case current: case current:
animator.terminate(); animator.terminate();
myScrollingSpeed = 0;
break; break;
case previous: case previous:
animator.setScrollingMode(AnimationProvider.ScrollingMode.AutoScrollingBackward); animator.startAutoScrolling(false, -3, direction, getWidth(), getMainAreaHeight());
myScrollingSpeed = -3;
break; break;
case next: case next:
animator.setScrollingMode(AnimationProvider.ScrollingMode.AutoScrollingBackward); animator.startAutoScrolling(false, 3, direction, getWidth(), getMainAreaHeight());
myScrollingSpeed = 3;
break; break;
} }
break; break;
case previous: case previous:
animator.setScrollingMode(AnimationProvider.ScrollingMode.AutoScrollingForward); animator.startAutoScrolling(true, 3, direction, getWidth(), getMainAreaHeight());
myScrollingSpeed = 3;
setPageToScrollTo(pageIndex); setPageToScrollTo(pageIndex);
break; break;
case next: case next:
animator.setScrollingMode(AnimationProvider.ScrollingMode.AutoScrollingForward); animator.startAutoScrolling(true, -3, direction, getWidth(), getMainAreaHeight());
myScrollingSpeed = -3;
setPageToScrollTo(pageIndex); setPageToScrollTo(pageIndex);
break; 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); drawOnBitmap(mySecondaryBitmap);
postInvalidate(); postInvalidate();
} }