1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-04 18:29:23 +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;
}
}
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();

View file

@ -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();
}