Apply scale mode and start position for GalleryView

This commit is contained in:
seven332 committed 2016-02-23 16:07:37 +08:00
1 parent 995146f60f
commit 1ca26334de
16 files changed
+384 -114

No files matched your search

@@ -25,6 +25,7 @@ import android.support.annotation.Nullable;
import com.hippo.ehviewer.client.EhConfig;
import com.hippo.ehviewer.gallery.gl.GalleryView;
import com.hippo.ehviewer.gallery.gl.ImageView;
import com.hippo.unifile.UniFile;
import com.hippo.yorozuya.NumberUtils;
@@ -145,6 +146,30 @@ public class Settings {
putIntToStr(KEY_READING_DIRECTION, value);
}
private static final String KEY_PAGE_SCALING = "page_scaling";
private static final int DEFAULT_PAGE_SCALING = ImageView.SCALE_FIT;
@ImageView.Scale
public static int getPageScaling() {
return ImageView.sanitizeScaleMode(getIntFromStr(KEY_PAGE_SCALING, DEFAULT_PAGE_SCALING));
}
public static void putPageScaling(int value) {
putIntToStr(KEY_PAGE_SCALING, value);
}
private static final String KEY_START_POSITION = "start_position";
private static final int DEFAULT_START_POSITION = ImageView.START_POSITION_TOP_RIGHT;
@ImageView.StartPosition
public static int getStartPosition() {
return ImageView.sanitizeStartPosition(getIntFromStr(KEY_START_POSITION, DEFAULT_START_POSITION));
}
public static void putStartPosition(int value) {
putIntToStr(KEY_START_POSITION, value);
}
private static final String KEY_SHOW_CLOCK = "gallery_show_clock";
private static final boolean DEFAULT_SHOW_CLOCK = true;
@@ -39,11 +39,11 @@ public class GalleryPageView extends GLFrameLayout {
public static final float PROGRESS_GONE = -1.0f;
public static final float PROGRESS_INDETERMINATE = -2.0f;
private ImageView mImage;
private GLLinearLayout mInfo;
private GLMovableTextView mPage;
private GLTextureView mError;
private GLProgressView mProgress;
private final ImageView mImage;
private final GLLinearLayout mInfo;
private final GLMovableTextView mPage;
private final GLTextureView mError;
private final GLProgressView mProgress;
private int mIndex = INVALID_INDEX;
@@ -49,15 +49,6 @@ import java.util.concurrent.atomic.AtomicInteger;
public class GalleryView extends GLView implements GestureRecognizer.Listener {
@IntDef({SCALE_ORIGIN, SCALE_FIT_WIDTH, SCALE_FIT_HEIGHT, SCALE_FIT, SCALE_FIXED})
@Retention(RetentionPolicy.SOURCE)
public @interface Scale {}
@IntDef({START_POSITION_TOP_LEFT, START_POSITION_TOP_RIGHT, START_POSITION_BOTTOM_LEFT,
START_POSITION_BOTTOM_RIGHT, START_POSITION_CENTER})
@Retention(RetentionPolicy.SOURCE)
public @interface StartPosition {}
@IntDef({LAYOUT_MODE_LEFT_TO_RIGHT, LAYOUT_MODE_RIGHT_TO_LEFT, LAYOUT_MODE_TOP_TO_BOTTOM})
@Retention(RetentionPolicy.SOURCE)
public @interface LayoutMode {}
@@ -66,18 +57,6 @@ public class GalleryView extends GLView implements GestureRecognizer.Listener {
private static final int PROGRESS_SIZE_IN_DP = 48;
private static final int PAGE_MIN_HEIGHT_IN_DP = 256;
public static final int SCALE_ORIGIN = 0;
public static final int SCALE_FIT_WIDTH = 1;
public static final int SCALE_FIT_HEIGHT = 2;
public static final int SCALE_FIT = 3;
public static final int SCALE_FIXED = 4;
public static final int START_POSITION_TOP_LEFT = 0;
public static final int START_POSITION_TOP_RIGHT = 1;
public static final int START_POSITION_BOTTOM_LEFT = 2;
public static final int START_POSITION_BOTTOM_RIGHT = 3;
public static final int START_POSITION_CENTER = 4;
public static final int LAYOUT_MODE_LEFT_TO_RIGHT = 0;
public static final int LAYOUT_MODE_RIGHT_TO_LEFT = 1;
public static final int LAYOUT_MODE_TOP_TO_BOTTOM = 2;
@@ -105,6 +84,8 @@ public class GalleryView extends GLView implements GestureRecognizer.Listener {
private static final int METHOD_SET_CURRENT_PAGE = 15;
private static final int METHOD_PAGE_LEFT = 16;
private static final int METHOD_PAGE_RIGHT = 17;
private static final int METHOD_SET_SCALE_MODE = 18;
private static final int METHOD_SET_START_POSITION = 19;
private final Context mContext;
private MovableTextTexture mPageTextTexture;
@@ -144,13 +125,12 @@ public class GalleryView extends GLView implements GestureRecognizer.Listener {
@LayoutMode
private int mLayoutMode = LAYOUT_MODE_RIGHT_TO_LEFT;
@ImageView.Scale
private int mScaleMode = ImageView.SCALE_FIT;
@ImageView.StartPosition
private int mStartPosition = ImageView.START_POSITION_TOP_LEFT;
private int mIndex;
@Scale
private int mScaleMode = SCALE_FIT;
@StartPosition
private int mStartPosition = START_POSITION_TOP_LEFT;
private final Listener mListener;
private final List<Integer> mMethodList = new ArrayList<>(5);
@@ -161,7 +141,8 @@ public class GalleryView extends GLView implements GestureRecognizer.Listener {
private final AtomicInteger mCurrentIndex = new AtomicInteger(GalleryPageView.INVALID_INDEX);
public GalleryView(@NonNull Context context, @NonNull Adapter adapter,
Listener listener, @LayoutMode int layoutMode) {
Listener listener, @LayoutMode int layoutMode, @ImageView.Scale int scaleMode,
@ImageView.StartPosition int startPosition) {
mContext = context;
mAdapter = adapter;
mListener = listener;
@@ -171,6 +152,8 @@ public class GalleryView extends GLView implements GestureRecognizer.Listener {
adapter.setGalleryView(this);
mLayoutMode = layoutMode;
mScaleMode = scaleMode;
mStartPosition = startPosition;
mProgressColor = ResourcesUtils.getAttrColor(context, R.attr.colorPrimary);
mProgressSize = LayoutUtils.dp2pix(context, PROGRESS_SIZE_IN_DP);
mErrorSize = context.getResources().getDimension(R.dimen.text_large);
@@ -185,7 +168,7 @@ public class GalleryView extends GLView implements GestureRecognizer.Listener {
private void ensurePagerLayoutManager() {
if (mPagerLayoutManager == null) {
mPagerLayoutManager = new PagerLayoutManager(mContext, this);
mPagerLayoutManager = new PagerLayoutManager(mContext, this, mScaleMode, mStartPosition, 1.0f);
}
}
@@ -330,6 +313,14 @@ public class GalleryView extends GLView implements GestureRecognizer.Listener {
postMethod(METHOD_PAGE_RIGHT);
}
public void setScaleMode(@ImageView.Scale int scaleMode) {
postMethod(METHOD_SET_SCALE_MODE, scaleMode);
}
public void setStartPosition(@ImageView.StartPosition int startPosition) {
postMethod(METHOD_SET_START_POSITION, startPosition);
}
@Override
public boolean onSingleTapUp(float x, float y) {
postMethod(METHOD_ON_SINGLE_TAP_UP, x, y);
@@ -616,6 +607,20 @@ public class GalleryView extends GLView implements GestureRecognizer.Listener {
}
}
private void setScaleModeInternal(int scaleMode) {
mScaleMode = scaleMode;
if (mPagerLayoutManager != null) {
mPagerLayoutManager.setScaleMode(scaleMode);
}
}
private void setStartPositionInternal(int startPosition) {
mStartPosition = startPosition;
if (mPagerLayoutManager != null) {
mPagerLayoutManager.setStartPosition(startPosition);
}
}
@RenderThread
void forceFill() {
mRequestFill = true;
@@ -714,6 +719,12 @@ public class GalleryView extends GLView implements GestureRecognizer.Listener {
case METHOD_PAGE_RIGHT:
pageRightInternal();
break;
case METHOD_SET_SCALE_MODE:
setScaleModeInternal((Integer) args[0]);
break;
case METHOD_SET_START_POSITION:
setStartPositionInternal((Integer) args[0]);
break;
}
}
@@ -72,7 +72,32 @@ public class ImageView extends GLView {
private float mScale = 1.0f;
private boolean mScaleOffsetDirty = true;
private boolean mPositionInRootDirty = true;
@Scale
public static int sanitizeScaleMode(int scaleMode) {
if (scaleMode != SCALE_ORIGIN &&
scaleMode != SCALE_FIT_WIDTH &&
scaleMode != SCALE_FIT_HEIGHT &&
scaleMode != SCALE_FIT &&
scaleMode != SCALE_FIXED) {
return SCALE_FIT;
} else {
return scaleMode;
}
}
@StartPosition
public static int sanitizeStartPosition(int startPosition) {
if (startPosition != START_POSITION_TOP_LEFT &&
startPosition != START_POSITION_TOP_RIGHT &&
startPosition != START_POSITION_BOTTOM_LEFT &&
startPosition != START_POSITION_BOTTOM_RIGHT &&
startPosition != START_POSITION_CENTER) {
return START_POSITION_TOP_RIGHT;
} else {
return startPosition;
}
}
@Override
protected int getSuggestedMinimumWidth() {
@@ -128,12 +153,6 @@ public class ImageView extends GLView {
@Override
protected void onSizeChanged(int newW, int newH, int oldW, int oldH) {
mScaleOffsetDirty = true;
mPositionInRootDirty = true;
}
@Override
protected void onPositionInRootChanged(int x, int y, int oldX, int oldY) {
mPositionInRootDirty = true;
}
public void getScaleDefault(float[] scaleDefault) {
@@ -173,7 +192,6 @@ public class ImageView extends GLView {
}
mScaleOffsetDirty = true;
mPositionInRootDirty = true;
}
public Texture getTexture() {
@@ -402,7 +420,6 @@ public class ImageView extends GLView {
adjustPosition();
mScaleOffsetDirty = false;
mPositionInRootDirty = true;
}
public void scroll(int dx, int dy, int[] remain) {
@@ -462,7 +479,6 @@ public class ImageView extends GLView {
}
if (dx != remain[0] || dy != remain[1]) {
mPositionInRootDirty = true;
invalidate();
}
}
@@ -493,7 +509,6 @@ public class ImageView extends GLView {
// adjust position
adjustPosition();
mPositionInRootDirty = true;
invalidate();
}
@@ -520,8 +535,6 @@ public class ImageView extends GLView {
srcActual.setEmpty();
dstActual.setEmpty();
}
mPositionInRootDirty = false;
}
@Override
@@ -535,12 +548,7 @@ public class ImageView extends GLView {
setScaleOffset(mScaleMode, mStartPosition, mScaleValue);
}
if (mPositionInRootDirty) {
applyPositionInRoot();
} else {
mSrcActual.set(0, 0, texture.getWidth(), texture.getHeight());
mDstActual.set(mDst);
}
applyPositionInRoot();
if (!mSrcActual.isEmpty()) {
texture.draw(canvas, mSrcActual, mDstActual);
@@ -17,6 +17,7 @@
package com.hippo.ehviewer.gallery.gl;
import android.content.Context;
import android.graphics.Rect;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.util.Log;
@@ -68,6 +69,12 @@ public class PagerLayoutManager extends GalleryView.LayoutManager {
@Mode
private int mMode = MODE_RIGHT_TO_LEFT;
@ImageView.Scale
private int mScaleMode;
@ImageView.StartPosition
private int mStartPosition;
private float mScaleValue;
private int mOffset;
private int mDeltaX;
private int mDeltaY;
@@ -86,9 +93,16 @@ public class PagerLayoutManager extends GalleryView.LayoutManager {
// Current index
private int mIndex;
public PagerLayoutManager(Context context, @NonNull GalleryView galleryView) {
private final Rect mTempRect = new Rect();
public PagerLayoutManager(Context context, @NonNull GalleryView galleryView,
@ImageView.Scale int scaleMode, @ImageView.StartPosition int startPoint, float scaleValue) {
super(galleryView);
mScaleMode = scaleMode;
mStartPosition = startPoint;
mScaleValue = scaleValue;
mInterval = LayoutUtils.dp2pix(context, INTERVAL);
mPageFling = new PageFling(context);
mSmoothScroller = new SmoothScroller();
@@ -134,6 +148,40 @@ public class PagerLayoutManager extends GalleryView.LayoutManager {
}
}
public void setScaleMode(@ImageView.Scale int scaleMode) {
if (mScaleMode == scaleMode) {
return;
}
mScaleMode = scaleMode;
if (mCurrent != null) {
mCurrent.getImageView().setScaleOffset(mScaleMode, mStartPosition, mScaleValue);
}
if (mPrevious != null) {
mPrevious.getImageView().setScaleOffset(mScaleMode, mStartPosition, mScaleValue);
}
if (mNext != null) {
mNext.getImageView().setScaleOffset(mScaleMode, mStartPosition, mScaleValue);
}
}
public void setStartPosition(@ImageView.StartPosition int startPosition) {
if (mStartPosition == startPosition) {
return;
}
mStartPosition = startPosition;
if (mCurrent != null) {
mCurrent.getImageView().setScaleOffset(mScaleMode, mStartPosition, mScaleValue);
}
if (mPrevious != null) {
mPrevious.getImageView().setScaleOffset(mScaleMode, mStartPosition, mScaleValue);
}
if (mNext != null) {
mNext.getImageView().setScaleOffset(mScaleMode, mStartPosition, mScaleValue);
}
}
@Override
public void onAttach(GalleryView.Adapter adapter) {
AssertUtils.assertEquals("The PagerLayoutManager is attached", mAdapter, null);
@@ -221,6 +269,26 @@ public class PagerLayoutManager extends GalleryView.LayoutManager {
}
}
private GalleryPageView obtainPage() {
GalleryPageView page = mGalleryView.obtainPage();
page.getImageView().setScaleOffset(mScaleMode, mStartPosition, mScaleValue);
return page;
}
private void layoutPage(GalleryPageView page, int widthSpec, int heightSpec,
int left, int top, int right, int bottom) {
Rect rect = mTempRect;
page.getValidRect(rect);
boolean oldValid = !rect.isEmpty();
page.measure(widthSpec, heightSpec);
page.layout(left, top, right, bottom);
page.getValidRect(rect);
boolean newValid = !rect.isEmpty();
if (!oldValid && newValid) {
page.getImageView().setScaleOffset(mScaleMode, mStartPosition, mScaleValue);
}
}
@Override
public void onFill() {
GalleryView.Adapter adapter = mAdapter;
@@ -292,19 +360,19 @@ public class PagerLayoutManager extends GalleryView.LayoutManager {
// Ensure pages
if (mCurrent == null) {
mCurrent = galleryView.obtainPage();
mCurrent = obtainPage();
adapter.bind(mCurrent, index);
galleryView.addComponent(mCurrent);
}
if (mPrevious == null && index > 0) {
mPrevious = galleryView.obtainPage();
mPrevious = obtainPage();
adapter.bind(mPrevious, index - 1);
galleryView.addComponent(mPrevious);
} else if (mPrevious != null && index == 0) {
removePage(mPrevious);
}
if (mNext == null && index < size - 1) {
mNext = galleryView.obtainPage();
mNext = obtainPage();
adapter.bind(mNext, index + 1);
galleryView.addComponent(mNext);
} else if (mNext != null && index == size - 1) {
@@ -324,16 +392,16 @@ public class PagerLayoutManager extends GalleryView.LayoutManager {
final int widthSpec = GLView.MeasureSpec.makeMeasureSpec(width, GLView.MeasureSpec.EXACTLY);
final int heightSpec = GLView.MeasureSpec.makeMeasureSpec(height, GLView.MeasureSpec.EXACTLY);
if (mCurrent != null) {
mCurrent.measure(widthSpec, heightSpec);
mCurrent.layout(offset, 0, width + offset, height);
layoutPage(mCurrent, widthSpec, heightSpec,
offset, 0, width + offset, height);
}
if (leftPage != null) {
leftPage.measure(widthSpec, heightSpec);
leftPage.layout(-mInterval - width + offset, 0, -mInterval + offset, height);
layoutPage(leftPage, widthSpec, heightSpec,
-mInterval - width + offset, 0, -mInterval + offset, height);
}
if (rightPage != null) {
rightPage.measure(widthSpec, heightSpec);
rightPage.layout(width + mInterval + offset, 0, width + mInterval + width + offset, height);
layoutPage(rightPage, widthSpec, heightSpec,
width + mInterval + offset, 0, width + mInterval + width + offset, height);
}
}
}
@@ -411,7 +479,7 @@ public class PagerLayoutManager extends GalleryView.LayoutManager {
mPrevious = null;
if (mIndex > 0) {
mPrevious = mGalleryView.obtainPage();
mPrevious = obtainPage();
mAdapter.bind(mPrevious, mIndex - 1);
mGalleryView.addComponent(mPrevious);
}
@@ -433,7 +501,7 @@ public class PagerLayoutManager extends GalleryView.LayoutManager {
mNext = null;
if (mIndex < size - 1) {
mNext = mGalleryView.obtainPage();
mNext = obtainPage();
adapter.bind(mNext, mIndex + 1);
mGalleryView.addComponent(mNext);
}
@@ -623,7 +691,7 @@ public class PagerLayoutManager extends GalleryView.LayoutManager {
}
mCurrent.getImageView().scale(focusX, focusY, scale);
// TODO Save scale
mScaleValue = mCurrent.getImageView().getScale();
}
@Override
@@ -978,6 +1046,7 @@ public class PagerLayoutManager extends GalleryView.LayoutManager {
float scale = MathUtils.lerp(mStartScale, mEndScale, progress);
mCurrent.getImageView().scale(mFocusX, mFocusY, scale / mLastScale);
mLastScale = scale;
mScaleValue = scale;
}
}
}
@@ -146,6 +146,12 @@ public class ScrollLayoutManager extends GalleryView.LayoutManager {
return iterator;
}
private GalleryPageView obtainPage() {
GalleryPageView page = mGalleryView.obtainPage();
page.getImageView().setScaleOffset(ImageView.SCALE_FIT, ImageView.START_POSITION_TOP_RIGHT, 1.0f);
return page;
}
private void fillPages() {
GalleryView.Adapter adapter = mAdapter;
GalleryView galleryView = mGalleryView;
@@ -185,7 +191,7 @@ public class ScrollLayoutManager extends GalleryView.LayoutManager {
while (y >= minY && mIndex > 0) {
mIndex--;
GalleryPageView page = galleryView.obtainPage();
GalleryPageView page = obtainPage();
mAdapter.bind(page, mIndex);
galleryView.addComponent(page);
mPages.add(0, page);
@@ -232,7 +238,7 @@ public class ScrollLayoutManager extends GalleryView.LayoutManager {
if (pages == null || !pages.hasNext()) {
pages = null;
// New page
page = galleryView.obtainPage();
page = obtainPage();
mAdapter.bind(page, index);
galleryView.addComponent(page);
mPages.add(page);
@@ -44,6 +44,7 @@ import com.hippo.ehviewer.gallery.GalleryProviderListener;
import com.hippo.ehviewer.gallery.ZipGalleryProvider;
import com.hippo.ehviewer.gallery.gl.GalleryPageView;
import com.hippo.ehviewer.gallery.gl.GalleryView;
import com.hippo.ehviewer.gallery.gl.ImageView;
import com.hippo.gl.glrenderer.ImageTexture;
import com.hippo.gl.view.GLRootView;
import com.hippo.image.Image;
@@ -192,8 +193,9 @@ public class GalleryActivity extends AppCompatActivity
mGalleryProvider.setGLRoot(glRootView);
mUploader = new ImageTexture.Uploader(glRootView);
mGalleryView = new GalleryView(this, new GalleryAdapter(),
this, Settings.getReadingDirection());
mGalleryView = new GalleryView(this, new GalleryAdapter(), this,
Settings.getReadingDirection(), Settings.getPageScaling(),
Settings.getStartPosition());
glRootView.setContentPane(mGalleryView);
// System UI helper
@@ -501,6 +503,8 @@ public class GalleryActivity extends AppCompatActivity
private final View mView;
private final Spinner mReadingDirection;
private final Spinner mScaleMode;
private final Spinner mStartPosition;
private final SwitchCompat mShowClock;
private final SwitchCompat mShowBattery;
private final SwitchCompat mVolumePage;
@@ -509,11 +513,15 @@ public class GalleryActivity extends AppCompatActivity
public GalleryMenuHelper() {
mView = getLayoutInflater().inflate(R.layout.dialog_gallery_menu, null);
mReadingDirection = (Spinner) mView.findViewById(R.id.reading_direction);
mScaleMode = (Spinner) mView.findViewById(R.id.page_scaling);
mStartPosition = (Spinner) mView.findViewById(R.id.start_position);
mShowClock = (SwitchCompat) mView.findViewById(R.id.show_clock);
mShowBattery = (SwitchCompat) mView.findViewById(R.id.show_battery);
mVolumePage = (SwitchCompat) mView.findViewById(R.id.volume_page);
mReadingDirection.setSelection(Settings.getReadingDirection());
mScaleMode.setSelection(Settings.getPageScaling());
mStartPosition.setSelection(Settings.getStartPosition());
mShowClock.setChecked(Settings.getShowClock());
mShowBattery.setChecked(Settings.getShowBattery());
mVolumePage.setChecked(Settings.getVolumePage());
@@ -526,17 +534,23 @@ public class GalleryActivity extends AppCompatActivity
@Override
public void onClick(DialogInterface dialog, int which) {
int layoutMode = GalleryView.sanitizeLayoutMode(mReadingDirection.getSelectedItemPosition());
int scaleMode = ImageView.sanitizeScaleMode(mScaleMode.getSelectedItemPosition());
int startPosition = ImageView.sanitizeStartPosition(mStartPosition.getSelectedItemPosition());
boolean showClock = mShowClock.isChecked();
boolean showBattery = mShowBattery.isChecked();
boolean volumePage = mVolumePage.isChecked();
Settings.putReadingDirection(layoutMode);
Settings.putPageScaling(scaleMode);
Settings.putStartPosition(startPosition);
Settings.putShowClock(showClock);
Settings.putShowBattery(showBattery);
Settings.putVolumePage(volumePage);
if (mGalleryView != null) {
mGalleryView.setLayoutMode(layoutMode);
mGalleryView.setScaleMode(scaleMode);
mGalleryView.setStartPosition(startPosition);
}
if (mClock != null) {
mClock.setVisibility(showClock ? View.VISIBLE : View.GONE);
@@ -427,6 +427,7 @@ public class GLView implements TouchOwner {
public void offsetTopAndBottom(int offset) {
if (offset != 0){
mBounds.offset(0, offset);
dispatchNotifyPositionInRoot();
invalidate();
}
}
@@ -439,6 +440,7 @@ public class GLView implements TouchOwner {
public void offsetLeftAndRight(int offset) {
if (offset != 0){
mBounds.offset(offset, 0);
dispatchNotifyPositionInRoot();
invalidate();
}
}
@@ -498,9 +500,14 @@ public class GLView implements TouchOwner {
}
protected void renderChild(GLCanvas canvas, GLView component) {
if (component.getVisibility() != GLView.VISIBLE) {
return;
}
getValidRect(mTempRect);
if ((component.getVisibility() != GLView.VISIBLE || mTempRect.isEmpty())
&& component.mAnimation == null) return;
if (mTempRect.isEmpty()) {
return;
}
int xOffset = component.mBounds.left - mScrollX;
int yOffset = component.mBounds.top - mScrollY;
@@ -39,7 +39,7 @@ public class GLLinearLayout extends GLView {
private int mInterval = 0;
private int mOrientation = VERTICAL;
private List<GLView> mTempList = new ArrayList<>();
private final List<GLView> mTempList = new ArrayList<>();
public void setInterval(int interval) {
if (mInterval != interval) {
@@ -24,8 +24,8 @@ import com.hippo.gl.view.GLView;
public class GLTextureView extends GLView {
private RectF mSrc = new RectF();
private RectF mDst = new RectF();
private final RectF mSrc = new RectF();
private final RectF mDst = new RectF();
private Texture mTexture;
@@ -36,6 +36,7 @@ public class GLTextureView extends GLView {
} else {
mSrc.setEmpty();
}
invalidate();
}
public Texture getTexture() {