diff --git a/app/src/main/java/com/hippo/ehviewer/gallery/ui/FrameView.java b/app/src/main/java/com/hippo/ehviewer/gallery/ui/FrameView.java new file mode 100644 index 000000000..95317cf99 --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/gallery/ui/FrameView.java @@ -0,0 +1,82 @@ +package com.hippo.ehviewer.gallery.ui; + +public class FrameView extends GLView { + + @Override + protected void onMeasure(int widthSpec, int heightSpec) { + int maxWidth = 0; + int maxHeight = 0; + for (int i = 0, n = getComponentCount(); i < n; i++) { + GLView component = getComponent(i); + measureComponent(component, widthSpec, heightSpec); + maxWidth = Math.max(maxWidth, component.getMeasuredWidth()); + maxHeight = Math.max(maxHeight, component.getMeasuredHeight()); + } + + // Consider min + maxWidth = Math.max(maxWidth, getMinimumWidth()); + maxHeight = Math.max(maxHeight, getMinimumHeight()); + + // The final + maxWidth = getDefaultSize(maxWidth, widthSpec); + maxHeight = getDefaultSize(maxHeight, heightSpec); + + setMeasuredSize(maxWidth, maxHeight); + + if (MeasureSpec.getSize(widthSpec) != MeasureSpec.EXACTLY || + MeasureSpec.getSize(heightSpec) != MeasureSpec.EXACTLY) { + // Measure again + measureAllComponents(this, MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.EXACTLY), + MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.EXACTLY)); + } + } + + @Override + protected void onLayout(boolean changeSize, int left, int top, int right, int bottom) { + int width = getWidth(); + int height = getHeight(); + + for (int i = 0, n = getComponentCount(); i < n; i++) { + GLView component = getComponent(i); + int measureWidth = component.getMeasuredWidth(); + int measureHeight = component.getMeasuredHeight(); + int componentLeft; + int componentTop; + + GravityLayoutParams lp = (GravityLayoutParams) component.getLayoutParams(); + int gravity = lp.gravity; + if (Gravity.centerHorizontal(gravity)) { + componentLeft = (width / 2) - (measureWidth / 2); + } else if (Gravity.right(gravity)) { + componentLeft = width - measureWidth; + } else { + componentLeft = 0; + } + if (Gravity.centerVertical(gravity)) { + componentTop = (height / 2) - (measureHeight / 2); + } else if (Gravity.bottom(gravity)) { + componentTop = height - measureHeight; + } else { + componentTop = 0; + } + + component.layout(componentLeft, componentTop, + componentLeft + measureWidth, componentTop + measureHeight); + } + } + + @Override + protected boolean checkLayoutParams(GLView.LayoutParams p) { + return p instanceof GravityLayoutParams; + } + + @Override + protected LayoutParams generateDefaultLayoutParams() { + return new GravityLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); + } + + @Override + protected LayoutParams generateLayoutParams(GLView.LayoutParams p) { + return p == null ? generateDefaultLayoutParams() : new GravityLayoutParams(p); + } +} diff --git a/app/src/main/java/com/hippo/ehviewer/gallery/ui/Gravity.java b/app/src/main/java/com/hippo/ehviewer/gallery/ui/Gravity.java new file mode 100644 index 000000000..d3c2a80e3 --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/gallery/ui/Gravity.java @@ -0,0 +1,42 @@ +package com.hippo.ehviewer.gallery.ui; + +public class Gravity { + + public static final int NO_GRAVITY = 0x0000; + + public static final int LEFT = 0x0001; + public static final int TOP = 0x0002; + public static final int RIGHT = 0x0004; + public static final int BOTTOM = 0x0008; + + public static final int CENTER_HORIZONTAL = LEFT | RIGHT; + public static final int CENTER_VERTICAL = TOP | BOTTOM; + public static final int CENTER = CENTER_VERTICAL|CENTER_HORIZONTAL; + + public static final int HORIZONTAL_MASK = CENTER_HORIZONTAL; + public static final int VERTICAL_MASK = CENTER_VERTICAL; + + public static boolean centerHorizontal(int gravity) { + return (gravity & HORIZONTAL_MASK) == CENTER_HORIZONTAL; + } + + public static boolean right(int gravity) { + return (gravity & HORIZONTAL_MASK) == RIGHT; + } + + public static boolean left(int gravity) { + return (gravity & HORIZONTAL_MASK) == LEFT; + } + + public static boolean centerVertical(int gravity) { + return (gravity & VERTICAL_MASK) == CENTER_VERTICAL; + } + + public static boolean top(int gravity) { + return (gravity & VERTICAL_MASK) == TOP; + } + + public static boolean bottom(int gravity) { + return (gravity & VERTICAL_MASK) == BOTTOM; + } +} diff --git a/app/src/main/java/com/hippo/ehviewer/gallery/ui/LinearView.java b/app/src/main/java/com/hippo/ehviewer/gallery/ui/LinearView.java new file mode 100644 index 000000000..63015be8b --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/gallery/ui/LinearView.java @@ -0,0 +1,74 @@ +package com.hippo.ehviewer.gallery.ui; + +public class LinearView extends GLView { + + public int mInterval; + + public void setInterval(int interval) { + mInterval = interval; + requestLayout(); + } + + // TODO + @Override + protected void onMeasure(int widthSpec, int heightSpec) { + measureAllComponents(this, widthSpec, heightSpec); + + int maxWidth = 0; + int maxHeight = 0; + int n = getComponentCount(); + for (int i = 0; i < n; i++) { + GLView component = getComponent(i); + maxWidth = Math.max(maxWidth, component.getMeasuredWidth()); + if (i != 0) { + maxHeight += mInterval; + } + maxHeight += component.getMeasuredHeight(); + } + + setMeasuredSize(getDefaultSize(maxWidth, widthSpec), getDefaultSize(maxHeight, heightSpec)); + } + + @Override + protected void onLayout(boolean changeSize, int left, int top, int right, int bottom) { + int width = getWidth(); + int componentLeft; + int componentTop = 0; + + for (int i = 0, n = getComponentCount(); i < n; i++) { + GLView component = getComponent(i); + int measureWidth = component.getMeasuredWidth(); + int measureHeight = component.getMeasuredHeight(); + + GravityLayoutParams lp = (GravityLayoutParams) component.getLayoutParams(); + int gravity = lp.gravity; + if (Gravity.centerHorizontal(gravity)) { + componentLeft = (width / 2) - (measureWidth / 2); + } else if (Gravity.right(gravity)) { + componentLeft = width - measureWidth; + } else { + componentLeft = 0; + } + + component.layout(componentLeft, componentTop, + componentLeft + measureWidth, componentTop + measureHeight); + + componentTop += measureHeight + mInterval; + } + } + + @Override + protected boolean checkLayoutParams(GLView.LayoutParams p) { + return p instanceof GravityLayoutParams; + } + + @Override + protected LayoutParams generateDefaultLayoutParams() { + return new GravityLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); + } + + @Override + protected LayoutParams generateLayoutParams(GLView.LayoutParams p) { + return p == null ? generateDefaultLayoutParams() : new GravityLayoutParams(p); + } +}