mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 10:19:33 +02:00
refactoring: ScrollingMode is move into AnimationProvider
This commit is contained in:
parent
fb20a8fb71
commit
b160192abe
6 changed files with 125 additions and 108 deletions
|
@ -39,14 +39,10 @@ abstract public class ZLView {
|
|||
public static enum Direction {
|
||||
leftToRight(true), rightToLeft(true), up(false), down(false);
|
||||
|
||||
private final boolean myIsHorizontal;
|
||||
public final boolean IsHorizontal;
|
||||
|
||||
Direction(boolean isHorizontal) {
|
||||
myIsHorizontal = isHorizontal;
|
||||
}
|
||||
|
||||
public boolean isHorizontal() {
|
||||
return myIsHorizontal;
|
||||
IsHorizontal = isHorizontal;
|
||||
}
|
||||
};
|
||||
public static enum Animation {
|
||||
|
|
|
@ -24,6 +24,20 @@ import android.graphics.*;
|
|||
import org.geometerplus.zlibrary.core.view.ZLView;
|
||||
|
||||
abstract class AnimationProvider {
|
||||
static enum ScrollingMode {
|
||||
NoScrolling(false),
|
||||
ManualScrolling(false),
|
||||
AutoScrollingForward(true),
|
||||
AutoScrollingBackward(true);
|
||||
|
||||
final boolean Auto;
|
||||
|
||||
ScrollingMode(boolean auto) {
|
||||
Auto = auto;
|
||||
}
|
||||
}
|
||||
private ScrollingMode myScrollingMode = ScrollingMode.NoScrolling;
|
||||
|
||||
protected final Paint myPaint;
|
||||
protected int myStartX;
|
||||
protected int myStartY;
|
||||
|
@ -38,8 +52,24 @@ abstract class AnimationProvider {
|
|||
myPaint = paint;
|
||||
}
|
||||
|
||||
ScrollingMode getScrollingMode() {
|
||||
return myScrollingMode;
|
||||
}
|
||||
|
||||
void setScrollingMode(ScrollingMode state) {
|
||||
myScrollingMode = state;
|
||||
}
|
||||
|
||||
void terminate() {
|
||||
myScrollingMode = ScrollingMode.NoScrolling;
|
||||
}
|
||||
|
||||
boolean inProgress() {
|
||||
return myScrollingMode != ScrollingMode.NoScrolling;
|
||||
}
|
||||
|
||||
int getScrollingShift() {
|
||||
return myDirection.isHorizontal() ? myEndX - myStartX : myEndY - myStartY;
|
||||
return myDirection.IsHorizontal ? myEndX - myStartX : myEndY - myStartY;
|
||||
}
|
||||
|
||||
void setup(int startX, int startY, int endX, int endY, ZLView.Direction direction, int width, int height) {
|
||||
|
|
|
@ -39,7 +39,7 @@ class CurlAnimationProvider extends AnimationProvider {
|
|||
final int oppositeX = Math.abs(myWidth - cornerX);
|
||||
final int oppositeY = Math.abs(myHeight - cornerY);
|
||||
final int x, y;
|
||||
if (myDirection.isHorizontal()) {
|
||||
if (myDirection.IsHorizontal) {
|
||||
x = Math.max(1, Math.min(myWidth - 1, myEndX));
|
||||
if (cornerY == 0) {
|
||||
y = Math.max(1, Math.min(myHeight / 2, myEndY));
|
||||
|
|
|
@ -29,7 +29,7 @@ class ShiftAnimationProvider extends SimpleAnimationProvider {
|
|||
@Override
|
||||
public void draw(Canvas canvas, Bitmap bgBitmap, Bitmap fgBitmap) {
|
||||
myPaint.setColor(Color.rgb(127, 127, 127));
|
||||
if (myDirection.isHorizontal()) {
|
||||
if (myDirection.IsHorizontal) {
|
||||
final int dX = myEndX - myStartX;
|
||||
canvas.drawBitmap(bgBitmap, dX > 0 ? dX - myWidth : dX + myWidth, 0, myPaint);
|
||||
canvas.drawBitmap(fgBitmap, dX, 0, myPaint);
|
||||
|
|
|
@ -30,7 +30,7 @@ class SlideAnimationProvider extends SimpleAnimationProvider {
|
|||
public void draw(Canvas canvas, Bitmap bgBitmap, Bitmap fgBitmap) {
|
||||
canvas.drawBitmap(bgBitmap, 0, 0, myPaint);
|
||||
myPaint.setColor(Color.rgb(127, 127, 127));
|
||||
if (myDirection.isHorizontal()) {
|
||||
if (myDirection.IsHorizontal) {
|
||||
final int dX = myEndX - myStartX;
|
||||
canvas.drawBitmap(fgBitmap, dX, 0, myPaint);
|
||||
if (dX > 0 && dX < myWidth) {
|
||||
|
|
|
@ -37,14 +37,6 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
|
|||
private boolean mySecondaryBitmapIsUpToDate;
|
||||
private Bitmap myFooterBitmap;
|
||||
|
||||
private static enum ScrollingState {
|
||||
NoScrolling,
|
||||
ManualScrolling,
|
||||
AutoScrollingForward,
|
||||
AutoScrollingBackward
|
||||
}
|
||||
private ScrollingState myScrollingState = ScrollingState.NoScrolling;
|
||||
|
||||
private int myStartX;
|
||||
private int myStartY;
|
||||
private int myEndX;
|
||||
|
@ -83,7 +75,7 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
|
|||
super.onSizeChanged(w, h, oldw, oldh);
|
||||
if (myScreenIsTouched) {
|
||||
final ZLView view = ZLApplication.Instance().getCurrentView();
|
||||
myScrollingState = ScrollingState.NoScrolling;
|
||||
getAnimationProvider().terminate();
|
||||
myScreenIsTouched = false;
|
||||
view.onScrollingFinished(ZLView.PageIndex.current);
|
||||
setPageToScrollTo(ZLView.PageIndex.current);
|
||||
|
@ -118,11 +110,11 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
|
|||
drawOnBitmap(myMainBitmap);
|
||||
}
|
||||
|
||||
if (myScrollingState == ScrollingState.NoScrolling) {
|
||||
if (getAnimationProvider().inProgress()) {
|
||||
onDrawInScrolling(canvas);
|
||||
} else {
|
||||
onDrawStatic(canvas);
|
||||
ZLApplication.Instance().onRepaintFinished();
|
||||
} else {
|
||||
onDrawInScrolling(canvas);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -150,59 +142,75 @@ 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();
|
||||
|
||||
final int w = getWidth();
|
||||
final int h = getMainAreaHeight();
|
||||
|
||||
boolean doStopScrolling = false;
|
||||
if (myScrollingState == ScrollingState.AutoScrollingForward ||
|
||||
myScrollingState == ScrollingState.AutoScrollingBackward) {
|
||||
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 (myScrollingState == ScrollingState.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;
|
||||
}
|
||||
doStopScrolling = true;
|
||||
}
|
||||
} else {
|
||||
if (getAnimationProvider().getScrollingShift() <= -bound) {
|
||||
if (myScrollingDirection.isHorizontal()) {
|
||||
myEndX = myStartX - bound;
|
||||
} else {
|
||||
myEndY = myStartY - bound;
|
||||
}
|
||||
doStopScrolling = true;
|
||||
}
|
||||
}
|
||||
myScrollingSpeed *= 1.5;
|
||||
final AnimationProvider animator = getAnimationProvider();
|
||||
final AnimationProvider.ScrollingMode mode = animator.getScrollingMode();
|
||||
if (mode.Auto) {
|
||||
doStep();
|
||||
}
|
||||
|
||||
if (doStopScrolling) {
|
||||
if (myScrollingState != ScrollingState.AutoScrollingBackward) {
|
||||
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;
|
||||
|
@ -212,28 +220,8 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
|
|||
view.onScrollingFinished(ZLView.PageIndex.current);
|
||||
}
|
||||
setPageToScrollTo(ZLView.PageIndex.current);
|
||||
myScrollingState = ScrollingState.NoScrolling;
|
||||
onDrawStatic(canvas);
|
||||
return;
|
||||
}
|
||||
|
||||
getAnimationProvider().setup(
|
||||
myStartX, myStartY,
|
||||
myEndX, myEndY,
|
||||
myScrollingDirection,
|
||||
getWidth(), getMainAreaHeight()
|
||||
);
|
||||
getAnimationProvider().draw(
|
||||
canvas,
|
||||
mySecondaryBitmap, myMainBitmap
|
||||
);
|
||||
|
||||
if (myScrollingState == ScrollingState.AutoScrollingForward ||
|
||||
myScrollingState == ScrollingState.AutoScrollingBackward) {
|
||||
postInvalidate();
|
||||
}
|
||||
|
||||
drawFooter(canvas);
|
||||
}
|
||||
|
||||
private void setPageToScrollTo(ZLView.PageIndex pageIndex) {
|
||||
|
@ -248,7 +236,7 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
|
|||
return;
|
||||
}
|
||||
|
||||
myScrollingState = ScrollingState.ManualScrolling;
|
||||
getAnimationProvider().setScrollingMode(AnimationProvider.ScrollingMode.ManualScrolling);
|
||||
myScrollingDirection = direction;
|
||||
myStartX = startX;
|
||||
myStartY = startY;
|
||||
|
@ -267,7 +255,7 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
|
|||
}
|
||||
|
||||
public void scrollToCenter() {
|
||||
myScrollingState = ScrollingState.NoScrolling;
|
||||
getAnimationProvider().terminate();
|
||||
if (myMainBitmap == null) {
|
||||
return;
|
||||
}
|
||||
|
@ -281,37 +269,38 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
|
|||
return;
|
||||
}
|
||||
myScrollingDirection = direction;
|
||||
boolean doSetup = myScrollingState == ScrollingState.NoScrolling;
|
||||
final AnimationProvider animator = getAnimationProvider();
|
||||
final boolean doSetup = !animator.inProgress();
|
||||
switch (pageIndex) {
|
||||
case current:
|
||||
switch (myPageToScrollTo) {
|
||||
case current:
|
||||
myScrollingState = ScrollingState.NoScrolling;
|
||||
animator.terminate();
|
||||
myScrollingSpeed = 0;
|
||||
break;
|
||||
case previous:
|
||||
myScrollingState = ScrollingState.AutoScrollingBackward;
|
||||
animator.setScrollingMode(AnimationProvider.ScrollingMode.AutoScrollingBackward);
|
||||
myScrollingSpeed = -3;
|
||||
break;
|
||||
case next:
|
||||
myScrollingState = ScrollingState.AutoScrollingBackward;
|
||||
animator.setScrollingMode(AnimationProvider.ScrollingMode.AutoScrollingBackward);
|
||||
myScrollingSpeed = 3;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case previous:
|
||||
myScrollingState = ScrollingState.AutoScrollingForward;
|
||||
animator.setScrollingMode(AnimationProvider.ScrollingMode.AutoScrollingForward);
|
||||
myScrollingSpeed = 3;
|
||||
setPageToScrollTo(pageIndex);
|
||||
break;
|
||||
case next:
|
||||
myScrollingState = ScrollingState.AutoScrollingForward;
|
||||
animator.setScrollingMode(AnimationProvider.ScrollingMode.AutoScrollingForward);
|
||||
myScrollingSpeed = -3;
|
||||
setPageToScrollTo(pageIndex);
|
||||
break;
|
||||
}
|
||||
if (doSetup && myScrollingState != ScrollingState.NoScrolling) {
|
||||
if (myScrollingDirection.isHorizontal()) {
|
||||
if (doSetup && animator.inProgress()) {
|
||||
if (myScrollingDirection.IsHorizontal) {
|
||||
myStartX = myScrollingSpeed < 0 ? getWidth() : 0;
|
||||
myStartY = 0;
|
||||
} else {
|
||||
|
@ -592,14 +581,15 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
|
|||
if (!view.isScrollbarShown()) {
|
||||
return 0;
|
||||
}
|
||||
if (myScrollingState == ScrollingState.NoScrolling) {
|
||||
return view.getScrollbarThumbLength(ZLView.PageIndex.current);
|
||||
} else {
|
||||
final AnimationProvider animator = getAnimationProvider();
|
||||
if (animator.inProgress()) {
|
||||
final int from = view.getScrollbarThumbLength(ZLView.PageIndex.current);
|
||||
final int to = view.getScrollbarThumbLength(myPageToScrollTo);
|
||||
final int size = myScrollingDirection.isHorizontal() ? getWidth() : getMainAreaHeight();
|
||||
final int shift = Math.abs(getAnimationProvider().getScrollingShift());
|
||||
final int size = myScrollingDirection.IsHorizontal ? getWidth() : getMainAreaHeight();
|
||||
final int shift = Math.abs(animator.getScrollingShift());
|
||||
return (from * (size - shift) + to * shift) / size;
|
||||
} else {
|
||||
return view.getScrollbarThumbLength(ZLView.PageIndex.current);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -608,14 +598,15 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
|
|||
if (!view.isScrollbarShown()) {
|
||||
return 0;
|
||||
}
|
||||
if (myScrollingState == ScrollingState.NoScrolling) {
|
||||
return view.getScrollbarThumbPosition(ZLView.PageIndex.current);
|
||||
} else {
|
||||
final AnimationProvider animator = getAnimationProvider();
|
||||
if (animator.inProgress()) {
|
||||
final int from = view.getScrollbarThumbPosition(ZLView.PageIndex.current);
|
||||
final int to = view.getScrollbarThumbPosition(myPageToScrollTo);
|
||||
final int size = myScrollingDirection.isHorizontal() ? getWidth() : getMainAreaHeight();
|
||||
final int shift = Math.abs(getAnimationProvider().getScrollingShift());
|
||||
final int size = myScrollingDirection.IsHorizontal ? getWidth() : getMainAreaHeight();
|
||||
final int shift = Math.abs(animator.getScrollingShift());
|
||||
return (from * (size - shift) + to * shift) / size;
|
||||
} else {
|
||||
return view.getScrollbarThumbPosition(ZLView.PageIndex.current);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue