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 {
|
public static enum Direction {
|
||||||
leftToRight(true), rightToLeft(true), up(false), down(false);
|
leftToRight(true), rightToLeft(true), up(false), down(false);
|
||||||
|
|
||||||
private final boolean myIsHorizontal;
|
public final boolean IsHorizontal;
|
||||||
|
|
||||||
Direction(boolean isHorizontal) {
|
Direction(boolean isHorizontal) {
|
||||||
myIsHorizontal = isHorizontal;
|
IsHorizontal = isHorizontal;
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isHorizontal() {
|
|
||||||
return myIsHorizontal;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
public static enum Animation {
|
public static enum Animation {
|
||||||
|
|
|
@ -24,6 +24,20 @@ import android.graphics.*;
|
||||||
import org.geometerplus.zlibrary.core.view.ZLView;
|
import org.geometerplus.zlibrary.core.view.ZLView;
|
||||||
|
|
||||||
abstract class AnimationProvider {
|
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 final Paint myPaint;
|
||||||
protected int myStartX;
|
protected int myStartX;
|
||||||
protected int myStartY;
|
protected int myStartY;
|
||||||
|
@ -38,8 +52,24 @@ abstract class AnimationProvider {
|
||||||
myPaint = paint;
|
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() {
|
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) {
|
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 oppositeX = Math.abs(myWidth - cornerX);
|
||||||
final int oppositeY = Math.abs(myHeight - cornerY);
|
final int oppositeY = Math.abs(myHeight - cornerY);
|
||||||
final int x, y;
|
final int x, y;
|
||||||
if (myDirection.isHorizontal()) {
|
if (myDirection.IsHorizontal) {
|
||||||
x = Math.max(1, Math.min(myWidth - 1, myEndX));
|
x = Math.max(1, Math.min(myWidth - 1, myEndX));
|
||||||
if (cornerY == 0) {
|
if (cornerY == 0) {
|
||||||
y = Math.max(1, Math.min(myHeight / 2, myEndY));
|
y = Math.max(1, Math.min(myHeight / 2, myEndY));
|
||||||
|
|
|
@ -29,7 +29,7 @@ class ShiftAnimationProvider extends SimpleAnimationProvider {
|
||||||
@Override
|
@Override
|
||||||
public void draw(Canvas canvas, Bitmap bgBitmap, Bitmap fgBitmap) {
|
public void draw(Canvas canvas, Bitmap bgBitmap, Bitmap fgBitmap) {
|
||||||
myPaint.setColor(Color.rgb(127, 127, 127));
|
myPaint.setColor(Color.rgb(127, 127, 127));
|
||||||
if (myDirection.isHorizontal()) {
|
if (myDirection.IsHorizontal) {
|
||||||
final int dX = myEndX - myStartX;
|
final int dX = myEndX - myStartX;
|
||||||
canvas.drawBitmap(bgBitmap, dX > 0 ? dX - myWidth : dX + myWidth, 0, myPaint);
|
canvas.drawBitmap(bgBitmap, dX > 0 ? dX - myWidth : dX + myWidth, 0, myPaint);
|
||||||
canvas.drawBitmap(fgBitmap, dX, 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) {
|
public void draw(Canvas canvas, Bitmap bgBitmap, Bitmap fgBitmap) {
|
||||||
canvas.drawBitmap(bgBitmap, 0, 0, myPaint);
|
canvas.drawBitmap(bgBitmap, 0, 0, myPaint);
|
||||||
myPaint.setColor(Color.rgb(127, 127, 127));
|
myPaint.setColor(Color.rgb(127, 127, 127));
|
||||||
if (myDirection.isHorizontal()) {
|
if (myDirection.IsHorizontal) {
|
||||||
final int dX = myEndX - myStartX;
|
final int dX = myEndX - myStartX;
|
||||||
canvas.drawBitmap(fgBitmap, dX, 0, myPaint);
|
canvas.drawBitmap(fgBitmap, dX, 0, myPaint);
|
||||||
if (dX > 0 && dX < myWidth) {
|
if (dX > 0 && dX < myWidth) {
|
||||||
|
|
|
@ -37,14 +37,6 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
|
||||||
private boolean mySecondaryBitmapIsUpToDate;
|
private boolean mySecondaryBitmapIsUpToDate;
|
||||||
private Bitmap myFooterBitmap;
|
private Bitmap myFooterBitmap;
|
||||||
|
|
||||||
private static enum ScrollingState {
|
|
||||||
NoScrolling,
|
|
||||||
ManualScrolling,
|
|
||||||
AutoScrollingForward,
|
|
||||||
AutoScrollingBackward
|
|
||||||
}
|
|
||||||
private ScrollingState myScrollingState = ScrollingState.NoScrolling;
|
|
||||||
|
|
||||||
private int myStartX;
|
private int myStartX;
|
||||||
private int myStartY;
|
private int myStartY;
|
||||||
private int myEndX;
|
private int myEndX;
|
||||||
|
@ -83,7 +75,7 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
|
||||||
super.onSizeChanged(w, h, oldw, oldh);
|
super.onSizeChanged(w, h, oldw, oldh);
|
||||||
if (myScreenIsTouched) {
|
if (myScreenIsTouched) {
|
||||||
final ZLView view = ZLApplication.Instance().getCurrentView();
|
final ZLView view = ZLApplication.Instance().getCurrentView();
|
||||||
myScrollingState = ScrollingState.NoScrolling;
|
getAnimationProvider().terminate();
|
||||||
myScreenIsTouched = false;
|
myScreenIsTouched = false;
|
||||||
view.onScrollingFinished(ZLView.PageIndex.current);
|
view.onScrollingFinished(ZLView.PageIndex.current);
|
||||||
setPageToScrollTo(ZLView.PageIndex.current);
|
setPageToScrollTo(ZLView.PageIndex.current);
|
||||||
|
@ -118,11 +110,11 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
|
||||||
drawOnBitmap(myMainBitmap);
|
drawOnBitmap(myMainBitmap);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (myScrollingState == ScrollingState.NoScrolling) {
|
if (getAnimationProvider().inProgress()) {
|
||||||
|
onDrawInScrolling(canvas);
|
||||||
|
} else {
|
||||||
onDrawStatic(canvas);
|
onDrawStatic(canvas);
|
||||||
ZLApplication.Instance().onRepaintFinished();
|
ZLApplication.Instance().onRepaintFinished();
|
||||||
} else {
|
|
||||||
onDrawInScrolling(canvas);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,59 +142,75 @@ 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();
|
||||||
|
|
||||||
final int w = getWidth();
|
final int w = getWidth();
|
||||||
final int h = getMainAreaHeight();
|
final int h = getMainAreaHeight();
|
||||||
|
|
||||||
boolean doStopScrolling = false;
|
final AnimationProvider animator = getAnimationProvider();
|
||||||
if (myScrollingState == ScrollingState.AutoScrollingForward ||
|
final AnimationProvider.ScrollingMode mode = animator.getScrollingMode();
|
||||||
myScrollingState == ScrollingState.AutoScrollingBackward) {
|
if (mode.Auto) {
|
||||||
switch (myScrollingDirection) {
|
doStep();
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
if (animator.inProgress()) {
|
||||||
if (doStopScrolling) {
|
animator.setup(
|
||||||
if (myScrollingState != ScrollingState.AutoScrollingBackward) {
|
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;
|
Bitmap swap = myMainBitmap;
|
||||||
myMainBitmap = mySecondaryBitmap;
|
myMainBitmap = mySecondaryBitmap;
|
||||||
mySecondaryBitmap = swap;
|
mySecondaryBitmap = swap;
|
||||||
|
@ -212,28 +220,8 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
|
||||||
view.onScrollingFinished(ZLView.PageIndex.current);
|
view.onScrollingFinished(ZLView.PageIndex.current);
|
||||||
}
|
}
|
||||||
setPageToScrollTo(ZLView.PageIndex.current);
|
setPageToScrollTo(ZLView.PageIndex.current);
|
||||||
myScrollingState = ScrollingState.NoScrolling;
|
|
||||||
onDrawStatic(canvas);
|
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) {
|
private void setPageToScrollTo(ZLView.PageIndex pageIndex) {
|
||||||
|
@ -248,7 +236,7 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
myScrollingState = ScrollingState.ManualScrolling;
|
getAnimationProvider().setScrollingMode(AnimationProvider.ScrollingMode.ManualScrolling);
|
||||||
myScrollingDirection = direction;
|
myScrollingDirection = direction;
|
||||||
myStartX = startX;
|
myStartX = startX;
|
||||||
myStartY = startY;
|
myStartY = startY;
|
||||||
|
@ -267,7 +255,7 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void scrollToCenter() {
|
public void scrollToCenter() {
|
||||||
myScrollingState = ScrollingState.NoScrolling;
|
getAnimationProvider().terminate();
|
||||||
if (myMainBitmap == null) {
|
if (myMainBitmap == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -281,37 +269,38 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
myScrollingDirection = direction;
|
myScrollingDirection = direction;
|
||||||
boolean doSetup = myScrollingState == ScrollingState.NoScrolling;
|
final AnimationProvider animator = getAnimationProvider();
|
||||||
|
final boolean doSetup = !animator.inProgress();
|
||||||
switch (pageIndex) {
|
switch (pageIndex) {
|
||||||
case current:
|
case current:
|
||||||
switch (myPageToScrollTo) {
|
switch (myPageToScrollTo) {
|
||||||
case current:
|
case current:
|
||||||
myScrollingState = ScrollingState.NoScrolling;
|
animator.terminate();
|
||||||
myScrollingSpeed = 0;
|
myScrollingSpeed = 0;
|
||||||
break;
|
break;
|
||||||
case previous:
|
case previous:
|
||||||
myScrollingState = ScrollingState.AutoScrollingBackward;
|
animator.setScrollingMode(AnimationProvider.ScrollingMode.AutoScrollingBackward);
|
||||||
myScrollingSpeed = -3;
|
myScrollingSpeed = -3;
|
||||||
break;
|
break;
|
||||||
case next:
|
case next:
|
||||||
myScrollingState = ScrollingState.AutoScrollingBackward;
|
animator.setScrollingMode(AnimationProvider.ScrollingMode.AutoScrollingBackward);
|
||||||
myScrollingSpeed = 3;
|
myScrollingSpeed = 3;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case previous:
|
case previous:
|
||||||
myScrollingState = ScrollingState.AutoScrollingForward;
|
animator.setScrollingMode(AnimationProvider.ScrollingMode.AutoScrollingForward);
|
||||||
myScrollingSpeed = 3;
|
myScrollingSpeed = 3;
|
||||||
setPageToScrollTo(pageIndex);
|
setPageToScrollTo(pageIndex);
|
||||||
break;
|
break;
|
||||||
case next:
|
case next:
|
||||||
myScrollingState = ScrollingState.AutoScrollingForward;
|
animator.setScrollingMode(AnimationProvider.ScrollingMode.AutoScrollingForward);
|
||||||
myScrollingSpeed = -3;
|
myScrollingSpeed = -3;
|
||||||
setPageToScrollTo(pageIndex);
|
setPageToScrollTo(pageIndex);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (doSetup && myScrollingState != ScrollingState.NoScrolling) {
|
if (doSetup && animator.inProgress()) {
|
||||||
if (myScrollingDirection.isHorizontal()) {
|
if (myScrollingDirection.IsHorizontal) {
|
||||||
myStartX = myScrollingSpeed < 0 ? getWidth() : 0;
|
myStartX = myScrollingSpeed < 0 ? getWidth() : 0;
|
||||||
myStartY = 0;
|
myStartY = 0;
|
||||||
} else {
|
} else {
|
||||||
|
@ -592,14 +581,15 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
|
||||||
if (!view.isScrollbarShown()) {
|
if (!view.isScrollbarShown()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (myScrollingState == ScrollingState.NoScrolling) {
|
final AnimationProvider animator = getAnimationProvider();
|
||||||
return view.getScrollbarThumbLength(ZLView.PageIndex.current);
|
if (animator.inProgress()) {
|
||||||
} else {
|
|
||||||
final int from = view.getScrollbarThumbLength(ZLView.PageIndex.current);
|
final int from = view.getScrollbarThumbLength(ZLView.PageIndex.current);
|
||||||
final int to = view.getScrollbarThumbLength(myPageToScrollTo);
|
final int to = view.getScrollbarThumbLength(myPageToScrollTo);
|
||||||
final int size = myScrollingDirection.isHorizontal() ? getWidth() : getMainAreaHeight();
|
final int size = myScrollingDirection.IsHorizontal ? getWidth() : getMainAreaHeight();
|
||||||
final int shift = Math.abs(getAnimationProvider().getScrollingShift());
|
final int shift = Math.abs(animator.getScrollingShift());
|
||||||
return (from * (size - shift) + to * shift) / size;
|
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()) {
|
if (!view.isScrollbarShown()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (myScrollingState == ScrollingState.NoScrolling) {
|
final AnimationProvider animator = getAnimationProvider();
|
||||||
return view.getScrollbarThumbPosition(ZLView.PageIndex.current);
|
if (animator.inProgress()) {
|
||||||
} else {
|
|
||||||
final int from = view.getScrollbarThumbPosition(ZLView.PageIndex.current);
|
final int from = view.getScrollbarThumbPosition(ZLView.PageIndex.current);
|
||||||
final int to = view.getScrollbarThumbPosition(myPageToScrollTo);
|
final int to = view.getScrollbarThumbPosition(myPageToScrollTo);
|
||||||
final int size = myScrollingDirection.isHorizontal() ? getWidth() : getMainAreaHeight();
|
final int size = myScrollingDirection.IsHorizontal ? getWidth() : getMainAreaHeight();
|
||||||
final int shift = Math.abs(getAnimationProvider().getScrollingShift());
|
final int shift = Math.abs(animator.getScrollingShift());
|
||||||
return (from * (size - shift) + to * shift) / size;
|
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