Cleanup unused code
This commit is contained in:
1 parent
774db8d3a4
commit
6ea7a1405f
6 files changed
+1
-673
No files matched your search
@@ -20,7 +20,6 @@ import android.os.Build
|
||||
import android.text.TextUtils
|
||||
import com.hippo.ehviewer.client.EhUrl
|
||||
import com.hippo.ehviewer.client.data.ListUrlBuilder
|
||||
import com.hippo.ehviewer.yorozuya.Utilities
|
||||
import java.io.UnsupportedEncodingException
|
||||
import java.net.MalformedURLException
|
||||
import java.net.URL
|
||||
@@ -39,7 +38,7 @@ object GalleryListUrlParser {
|
||||
} catch (e: MalformedURLException) {
|
||||
return null
|
||||
}
|
||||
if (!Utilities.contain(VALID_HOSTS, url.host)) {
|
||||
if (url.host !in VALID_HOSTS) {
|
||||
return null
|
||||
}
|
||||
val path = url.path ?: return null
|
||||
|
||||
@@ -105,10 +105,4 @@ public final class IOUtils {
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static byte[] getAllByte(InputStream is) throws IOException {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
copy(is, baos);
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -21,8 +21,6 @@ import java.util.Random;
|
||||
// Get most code from android.util.MathUtils
|
||||
public final class MathUtils {
|
||||
private static final Random sRandom = new Random();
|
||||
private static final float DEG_TO_RAD = 3.1415926f / 180.0f;
|
||||
private static final float RAD_TO_DEG = 180.0f / 3.1415926f;
|
||||
|
||||
private MathUtils() {
|
||||
}
|
||||
@@ -35,14 +33,6 @@ public final class MathUtils {
|
||||
return (float) Math.log(a);
|
||||
}
|
||||
|
||||
public static float exp(float a) {
|
||||
return (float) Math.exp(a);
|
||||
}
|
||||
|
||||
public static float pow(float a, float b) {
|
||||
return (float) Math.pow(a, b);
|
||||
}
|
||||
|
||||
public static float max(float a, float b) {
|
||||
return a > b ? a : b;
|
||||
}
|
||||
@@ -152,58 +142,10 @@ public final class MathUtils {
|
||||
return (float) Math.sqrt(x * x + y * y + z * z);
|
||||
}
|
||||
|
||||
public static boolean near(float x1, float y1, float x2, float y2, float slop) {
|
||||
return dist(x1, y1, x2, y2) < slop;
|
||||
}
|
||||
|
||||
public static boolean near(float x1, float y1, float z1, float x2, float y2, float z2, float slop) {
|
||||
return dist(x1, y1, z1, x2, y2, z2) < slop;
|
||||
}
|
||||
|
||||
public static float mag(float a, float b) {
|
||||
return (float) Math.hypot(a, b);
|
||||
}
|
||||
|
||||
public static float mag(float a, float b, float c) {
|
||||
return (float) Math.sqrt(a * a + b * b + c * c);
|
||||
}
|
||||
|
||||
public static float sq(float v) {
|
||||
return v * v;
|
||||
}
|
||||
|
||||
public static float cross(float v1x, float v1y, float v2x, float v2y) {
|
||||
return v1x * v2y - v1y * v2x;
|
||||
}
|
||||
|
||||
public static float radians(float degrees) {
|
||||
return degrees * DEG_TO_RAD;
|
||||
}
|
||||
|
||||
public static float degrees(float radians) {
|
||||
return radians * RAD_TO_DEG;
|
||||
}
|
||||
|
||||
public static float acos(float value) {
|
||||
return (float) Math.acos(value);
|
||||
}
|
||||
|
||||
public static float asin(float value) {
|
||||
return (float) Math.asin(value);
|
||||
}
|
||||
|
||||
public static float atan(float value) {
|
||||
return (float) Math.atan(value);
|
||||
}
|
||||
|
||||
public static float atan2(float a, float b) {
|
||||
return (float) Math.atan2(a, b);
|
||||
}
|
||||
|
||||
public static float tan(float angle) {
|
||||
return (float) Math.tan(angle);
|
||||
}
|
||||
|
||||
public static int lerp(int start, int stop, float amount) {
|
||||
return start + (int) ((stop - start) * amount);
|
||||
}
|
||||
@@ -212,26 +154,6 @@ public final class MathUtils {
|
||||
return start + (stop - start) * amount;
|
||||
}
|
||||
|
||||
public static float delerp(int start, int stop, int value) {
|
||||
if (stop == start) {
|
||||
return 1.0f;
|
||||
} else {
|
||||
return (float) (value - start) / (float) (stop - start);
|
||||
}
|
||||
}
|
||||
|
||||
public static float delerp(float start, float stop, float value) {
|
||||
if (stop == start) {
|
||||
return 1.0f;
|
||||
} else {
|
||||
return (value - start) / (stop - start);
|
||||
}
|
||||
}
|
||||
|
||||
public static float norm(float start, float stop, float value) {
|
||||
return (value - start) / (stop - start);
|
||||
}
|
||||
|
||||
public static float map(float minStart, float minStop, float maxStart, float maxStop, float value) {
|
||||
return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart));
|
||||
}
|
||||
@@ -245,64 +167,6 @@ public final class MathUtils {
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the input value x clamped to the range [min, max].
|
||||
*/
|
||||
public static float clamp(float x, float min, float max) {
|
||||
if (x > max) return max;
|
||||
if (x < min) return min;
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the input value x clamped to the range [min, max].
|
||||
*/
|
||||
public static long clamp(long x, long min, long max) {
|
||||
if (x > max) return max;
|
||||
if (x < min) return min;
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next power of two.
|
||||
* Returns the input if it is already power of 2.
|
||||
* Throws IllegalArgumentException if the input is <= 0 or
|
||||
* the answer overflows.
|
||||
*/
|
||||
public static int nextPowerOf2(int n) {
|
||||
if (n <= 0 || n > (1 << 30)) throw new IllegalArgumentException("n is invalid: " + n);
|
||||
n -= 1;
|
||||
n |= n >> 16;
|
||||
n |= n >> 8;
|
||||
n |= n >> 4;
|
||||
n |= n >> 2;
|
||||
n |= n >> 1;
|
||||
return n + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the previous power of two.
|
||||
* Returns the input if it is already power of 2.
|
||||
* Throws IllegalArgumentException if the input is <= 0 or
|
||||
* the answer overflows.
|
||||
*/
|
||||
public static int previousPowerOf2(int n) {
|
||||
if (n <= 0 || n > (1 << 30)) throw new IllegalArgumentException("n is invalid: " + n);
|
||||
n |= n >> 1;
|
||||
n |= n >> 2;
|
||||
n |= n >> 4;
|
||||
n |= n >> 8;
|
||||
n |= n >> 16;
|
||||
return n - (n >> 1);
|
||||
}
|
||||
|
||||
// http://stackoverflow.com/questions/109023/how-to-count-the-number-of-set-bits-in-a-32-bit-integer
|
||||
public static int hammingWeight(int n) {
|
||||
n = n - ((n >>> 1) & 0x55555555);
|
||||
n = (n & 0x33333333) + ((n >>> 2) & 0x33333333);
|
||||
return (((n + (n >>> 4)) & 0x0F0F0F0F) * 0x01010101) >>> 24;
|
||||
}
|
||||
|
||||
/**
|
||||
* divide and ceil
|
||||
*/
|
||||
@@ -310,54 +174,6 @@ public final class MathUtils {
|
||||
return (a + b - 1) / b;
|
||||
}
|
||||
|
||||
/**
|
||||
* divide and ceil
|
||||
*/
|
||||
public static long ceilDivide(long a, long b) {
|
||||
return (a + b - 1) / b;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get coverage radius of a area
|
||||
*
|
||||
* @param w the width of the area
|
||||
* @param h the height of the area
|
||||
* @param x the x of point in area
|
||||
* @param y the y of point in area
|
||||
* @return the radius
|
||||
*/
|
||||
public static float coverageRadius(float w, float h, float x, float y) {
|
||||
float x2;
|
||||
float y2;
|
||||
if (x > w / 2) {
|
||||
x2 = 0;
|
||||
} else {
|
||||
x2 = w;
|
||||
}
|
||||
if (y > h / 2) {
|
||||
y2 = 0;
|
||||
} else {
|
||||
y2 = h;
|
||||
}
|
||||
return dist(x, y, x2, y2);
|
||||
}
|
||||
|
||||
public static float positiveModulo(float x, float y) {
|
||||
float result = x % y;
|
||||
if (x < 0) {
|
||||
result += y;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static float negativeModulo(float x, float y) {
|
||||
float result = x % y;
|
||||
if (x > 0) {
|
||||
result -= y;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* [0, howbig)
|
||||
*/
|
||||
@@ -389,8 +205,4 @@ public final class MathUtils {
|
||||
return howsmall;
|
||||
return lerp(howsmall, howbig, sRandom.nextFloat());
|
||||
}
|
||||
|
||||
public static void randomSeed(long seed) {
|
||||
sRandom.setSeed(seed);
|
||||
}
|
||||
}
|
||||
@@ -124,15 +124,6 @@ public final class StringUtils {
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public static boolean endsWith(String string, String[] suffixs) {
|
||||
for (int i = 0, n = suffixs.length; i < n; i++) {
|
||||
if (string.endsWith(suffixs[i])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Splits the provided text into an array, separator specified.
|
||||
* This is an alternative to using StringTokenizer.</p>
|
||||
@@ -217,20 +208,6 @@ public final class StringUtils {
|
||||
return value == null ? defaultValue : value;
|
||||
}
|
||||
|
||||
public static boolean isAllDigit(String str) {
|
||||
if (TextUtils.isEmpty(str)) {
|
||||
return false;
|
||||
} else {
|
||||
for (int i = 0, n = str.length(); i < n; i++) {
|
||||
char ch = str.charAt(i);
|
||||
if (ch < '0' || ch > '9') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static int length(String str) {
|
||||
return null == str ? 0 : str.length();
|
||||
}
|
||||
@@ -243,19 +220,6 @@ public final class StringUtils {
|
||||
(!TextUtils.isEmpty(str1) && !TextUtils.isEmpty(str2) && str1.equals(str2));
|
||||
}
|
||||
|
||||
public static int ordinalIndexOf(String str, char c, int n) {
|
||||
if (null == str || n < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int pos = -1;
|
||||
do {
|
||||
pos = str.indexOf(c, pos + 1);
|
||||
} while (n-- > 0 && pos != -1);
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Works like {@link String#trim()}, but more white space is excluded.
|
||||
* The white space characters is {@link #WHITE_SPACE_ARRAY}.
|
||||
|
||||
@@ -19,27 +19,6 @@ package com.hippo.ehviewer.yorozuya;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
public final class Utilities {
|
||||
|
||||
/**
|
||||
* Whether the array contain the element
|
||||
*
|
||||
* @param array the array
|
||||
* @param obj the element
|
||||
* @return true for the array contain the element
|
||||
*/
|
||||
public static boolean contain(@Nullable Object[] array, @Nullable Object obj) {
|
||||
if (null == array) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (Object o : array) {
|
||||
if (ObjectUtils.equal(o, obj)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the array contain the element
|
||||
*
|
||||
|
||||
@@ -16,22 +16,15 @@
|
||||
|
||||
package com.hippo.ehviewer.yorozuya;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.Activity;
|
||||
import android.app.Dialog;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.os.Build;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.ViewParent;
|
||||
import android.view.ViewTreeObserver;
|
||||
|
||||
import androidx.annotation.IdRes;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public final class ViewUtils {
|
||||
@@ -41,201 +34,6 @@ public final class ViewUtils {
|
||||
private ViewUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get view center location in window
|
||||
*
|
||||
* @param view the view to check
|
||||
* @param location an array of two integers in which to hold the coordinates
|
||||
*/
|
||||
public static void getCenterInWindows(View view, int[] location) {
|
||||
getLocationInWindow(view, location);
|
||||
location[0] += view.getWidth() / 2;
|
||||
location[1] += view.getHeight() / 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get view location in window
|
||||
*
|
||||
* @param view the view to check
|
||||
* @param location an array of two integers in which to hold the coordinates
|
||||
*/
|
||||
public static void getLocationInWindow(View view, int[] location) {
|
||||
getLocationInAncestor(view, location, android.R.id.content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get view center in ths ancestor
|
||||
*
|
||||
* @param view the view to start with
|
||||
* @param location the container of result
|
||||
* @param ancestorId the ancestor id
|
||||
*/
|
||||
public static void getCenterInAncestor(View view, int[] location, int ancestorId) {
|
||||
getLocationInAncestor(view, location, ancestorId);
|
||||
location[0] += view.getWidth() / 2;
|
||||
location[1] += view.getHeight() / 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get view location in ths ancestor
|
||||
*
|
||||
* @param view the view to start with
|
||||
* @param location the container of result
|
||||
* @param ancestorId the ancestor id
|
||||
*/
|
||||
public static void getLocationInAncestor(View view, int[] location, int ancestorId) {
|
||||
if (location == null || location.length < 2) {
|
||||
throw new IllegalArgumentException(
|
||||
"location must be an array of two integers");
|
||||
}
|
||||
|
||||
float[] position = new float[2];
|
||||
|
||||
position[0] = view.getLeft();
|
||||
position[1] = view.getTop();
|
||||
|
||||
ViewParent viewParent = view.getParent();
|
||||
while (viewParent instanceof View) {
|
||||
view = (View) viewParent;
|
||||
if (view.getId() == ancestorId) {
|
||||
break;
|
||||
}
|
||||
|
||||
position[0] -= view.getScrollX();
|
||||
position[1] -= view.getScrollY();
|
||||
|
||||
position[0] += view.getLeft();
|
||||
position[1] += view.getTop();
|
||||
|
||||
viewParent = view.getParent();
|
||||
}
|
||||
|
||||
location[0] = (int) (position[0] + 0.5f);
|
||||
location[1] = (int) (position[1] + 0.5f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get view center in ths ancestor
|
||||
*
|
||||
* @param view the view to start with
|
||||
* @param location the container of result
|
||||
* @param ancestor the ancestor
|
||||
*/
|
||||
public static void getCenterInAncestor(View view, int[] location, View ancestor) {
|
||||
getLocationInAncestor(view, location, ancestor);
|
||||
location[0] += view.getWidth() / 2;
|
||||
location[1] += view.getHeight() / 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get view location in ths ancestor
|
||||
*
|
||||
* @param view the view to start with
|
||||
* @param location the container of result
|
||||
* @param ancestor the ancestor
|
||||
*/
|
||||
public static void getLocationInAncestor(View view, int[] location, View ancestor) {
|
||||
if (location == null || location.length < 2) {
|
||||
throw new IllegalArgumentException(
|
||||
"location must be an array of two integers");
|
||||
}
|
||||
|
||||
float[] position = new float[2];
|
||||
|
||||
position[0] = view.getLeft();
|
||||
position[1] = view.getTop();
|
||||
|
||||
ViewParent viewParent = view.getParent();
|
||||
while (viewParent instanceof View) {
|
||||
view = (View) viewParent;
|
||||
if (viewParent == ancestor) {
|
||||
break;
|
||||
}
|
||||
|
||||
position[0] -= view.getScrollX();
|
||||
position[1] -= view.getScrollY();
|
||||
|
||||
position[0] += view.getLeft();
|
||||
position[1] += view.getTop();
|
||||
|
||||
viewParent = view.getParent();
|
||||
}
|
||||
|
||||
location[0] = (int) (position[0] + 0.5f);
|
||||
location[1] = (int) (position[1] + 0.5f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Look for a ancestor view with the given id. If this view has the given
|
||||
* id, return this view.
|
||||
*
|
||||
* @param view the view to start with
|
||||
* @param id The id to search for.
|
||||
* @return The view that has the given id in the hierarchy or null
|
||||
*/
|
||||
public static View getAncestor(View view, int id) {
|
||||
if (view.getId() == id) {
|
||||
return view;
|
||||
}
|
||||
|
||||
ViewParent viewParent = view.getParent();
|
||||
while (viewParent instanceof View) {
|
||||
view = (View) viewParent;
|
||||
if (view.getId() == id) {
|
||||
return view;
|
||||
}
|
||||
viewParent = view.getParent();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Look for a child view with the given id. If this view has the given
|
||||
* id, return this view.
|
||||
*
|
||||
* @param view the view to start with
|
||||
* @param id the id to search for
|
||||
* @return the view that has the given id in the hierarchy or null
|
||||
*/
|
||||
public static View getChild(View view, int id) {
|
||||
if (view.getId() == id) {
|
||||
return view;
|
||||
}
|
||||
|
||||
if (view instanceof ViewGroup viewGroup) {
|
||||
for (int i = 0, n = viewGroup.getChildCount(); i < n; i++) {
|
||||
View child = viewGroup.getChildAt(i);
|
||||
View result = getChild(child, id);
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a bitmap showing a screenshot of the view passed in.
|
||||
*
|
||||
* @param v The view to get screenshot
|
||||
* @return The screenshot
|
||||
*/
|
||||
public static Bitmap getBitmapFromView(View v) {
|
||||
int width = v.getWidth();
|
||||
int height = v.getHeight();
|
||||
if (width == 0 && height == 0) {
|
||||
width = v.getMeasuredWidth();
|
||||
height = v.getMeasuredHeight();
|
||||
}
|
||||
Bitmap bitmap = Bitmap.createBitmap(width, height,
|
||||
Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
canvas.translate(-v.getScrollX(), -v.getScrollY());
|
||||
v.draw(canvas);
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove view from its parent
|
||||
*
|
||||
@@ -247,188 +45,6 @@ public final class ViewUtils {
|
||||
((ViewGroup) vp).removeView(view);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method that removes the support for HardwareAcceleration from a
|
||||
* {@link View}.<br/>
|
||||
* <br/>
|
||||
* Check AOSP notice:<br/>
|
||||
*
|
||||
* <pre>
|
||||
* 'ComposeShader can only contain shaders of different types (a BitmapShader and a
|
||||
* LinearGradient for instance, but not two instances of BitmapShader)'. But, 'If your
|
||||
* application is affected by any of these missing features or limitations, you can turn
|
||||
* off hardware acceleration for just the affected portion of your application by calling
|
||||
* setLayerType(View.LAYER_TYPE_SOFTWARE, null).'
|
||||
* </pre>
|
||||
*
|
||||
* @param v The view
|
||||
*/
|
||||
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
||||
public static void removeHardwareAccelerationSupport(View v) {
|
||||
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
|
||||
if (v.getLayerType() != View.LAYER_TYPE_SOFTWARE) {
|
||||
v.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
||||
public static void addHardwareAccelerationSupport(View v) {
|
||||
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
|
||||
if (v.getLayerType() != View.LAYER_TYPE_HARDWARE) {
|
||||
v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void measureView(View v, int width, int height) {
|
||||
int widthMeasureSpec;
|
||||
int heightMeasureSpec;
|
||||
if (width == ViewGroup.LayoutParams.WRAP_CONTENT)
|
||||
widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(0,
|
||||
View.MeasureSpec.UNSPECIFIED);
|
||||
else
|
||||
widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(Math.max(width, 0),
|
||||
View.MeasureSpec.EXACTLY);
|
||||
if (height == ViewGroup.LayoutParams.WRAP_CONTENT)
|
||||
heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0,
|
||||
View.MeasureSpec.UNSPECIFIED);
|
||||
else
|
||||
heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(Math.max(height, 0),
|
||||
View.MeasureSpec.EXACTLY);
|
||||
|
||||
v.measure(widthMeasureSpec, heightMeasureSpec);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the supplied view is under the given point in the
|
||||
* parent view's coordinate system.
|
||||
*
|
||||
* @param view Child view of the parent to hit test
|
||||
* @param x X position to test in the parent's coordinate system
|
||||
* @param y Y position to test in the parent's coordinate system
|
||||
* @param slop the slop out of the view, or negative for inside
|
||||
* @return true if the supplied view is under the given point, false otherwise
|
||||
*/
|
||||
public static boolean isViewUnder(@Nullable View view, int x, int y, int slop) {
|
||||
if (view == null) {
|
||||
return false;
|
||||
} else {
|
||||
float translationX = 0.0f;
|
||||
float translationY = 0.0f;
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
||||
translationX = view.getTranslationX();
|
||||
translationY = view.getTranslationY();
|
||||
}
|
||||
return x >= view.getLeft() + translationX - slop &&
|
||||
x < view.getRight() + translationX + slop &&
|
||||
y >= view.getTop() + translationY - slop &&
|
||||
y < view.getBottom() + translationY + slop;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility to return a default size. Uses the supplied size if the
|
||||
* MeasureSpec imposed no constraints. Will get suitable if allowed
|
||||
* by the MeasureSpec.
|
||||
*
|
||||
* @param size Default size for this view
|
||||
* @param measureSpec Constraints imposed by the parent
|
||||
* @return The size this view should be.
|
||||
*/
|
||||
public static int getSuitableSize(int size, int measureSpec) {
|
||||
int result = size;
|
||||
int specMode = View.MeasureSpec.getMode(measureSpec);
|
||||
int specSize = View.MeasureSpec.getSize(measureSpec);
|
||||
|
||||
switch (specMode) {
|
||||
case View.MeasureSpec.UNSPECIFIED:
|
||||
result = size;
|
||||
break;
|
||||
case View.MeasureSpec.EXACTLY:
|
||||
result = specSize;
|
||||
break;
|
||||
case View.MeasureSpec.AT_MOST:
|
||||
return size == 0 ? specSize : Math.min(size, specSize);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* removeOnGlobalLayoutListener
|
||||
*
|
||||
* @param viewTreeObserver the ViewTreeObserver
|
||||
* @param l the OnGlobalLayoutListener
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public static void removeOnGlobalLayoutListener(ViewTreeObserver viewTreeObserver,
|
||||
ViewTreeObserver.OnGlobalLayoutListener l) {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
|
||||
viewTreeObserver.removeGlobalOnLayoutListener(l);
|
||||
} else {
|
||||
viewTreeObserver.removeOnGlobalLayoutListener(l);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get index in parent
|
||||
*
|
||||
* @param view The view
|
||||
* @return The index
|
||||
*/
|
||||
public static int getIndexInParent(View view) {
|
||||
ViewParent parent = view.getParent();
|
||||
if (parent instanceof ViewGroup viewParent) {
|
||||
int count = viewParent.getChildCount();
|
||||
for (int i = 0; i < count; i++) {
|
||||
View v = viewParent.getChildAt(i);
|
||||
if (v == view) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform point from parent to child
|
||||
*
|
||||
* @param point the point
|
||||
* @param parent the parent
|
||||
* @param child the child
|
||||
*/
|
||||
public static void transformPointToViewLocal(float[] point, View parent, View child) {
|
||||
point[0] += parent.getScrollX() - child.getLeft();
|
||||
point[1] += parent.getScrollY() - child.getTop();
|
||||
}
|
||||
|
||||
public static void setEnabledRecursively(View view, boolean enabled) {
|
||||
if (view instanceof ViewGroup viewGroup) {
|
||||
for (int i = 0, n = viewGroup.getChildCount(); i < n; i++) {
|
||||
setEnabledRecursively(viewGroup.getChildAt(i), enabled);
|
||||
}
|
||||
}
|
||||
view.setEnabled(enabled);
|
||||
}
|
||||
|
||||
public static void dumpViewHierarchy(View view, PrintWriter writer) {
|
||||
dumpViewHierarchy(view, writer, "");
|
||||
}
|
||||
|
||||
private static void dumpViewHierarchy(View view, PrintWriter writer, String prefix) {
|
||||
writer.write(prefix);
|
||||
writer.write(view.getClass().getName());
|
||||
writer.write('\n');
|
||||
if (view instanceof ViewGroup viewGroup) {
|
||||
String newPrefix = prefix + " ";
|
||||
for (int i = 0, count = viewGroup.getChildCount(); i < count; i++) {
|
||||
View child = viewGroup.getChildAt(i);
|
||||
dumpViewHierarchy(child, writer, newPrefix);
|
||||
}
|
||||
}
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a value suitable for use in {@link View#setId(int)}.
|
||||
* This value will not collide with ID values generated at build time by aapt for R.id.
|
||||
@@ -447,42 +63,6 @@ public final class ViewUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Offset this view translationX
|
||||
*/
|
||||
public static void translationXBy(View view, float offset) {
|
||||
view.setTranslationX(view.getTranslationX() + offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Offset this view translationY
|
||||
*/
|
||||
public static void translationYBy(View view, float offset) {
|
||||
view.setTranslationY(view.getTranslationY() + offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* The visual right position of this view, in pixels. This is equivalent to the
|
||||
* {@link View#setTranslationX(float) translationX} property plus the current
|
||||
* {@link View#getRight() right} property.
|
||||
*
|
||||
* @return The visual right position of this view, in pixels.
|
||||
*/
|
||||
public static float getX2(View view) {
|
||||
return view.getRight() + view.getTranslationX();
|
||||
}
|
||||
|
||||
/**
|
||||
* The visual bottom position of this view, in pixels. This is equivalent to the
|
||||
* {@link View#setTranslationY(float) translationY} property plus the current
|
||||
* {@link View#getBottom()} () bottom} property.
|
||||
*
|
||||
* @return The visual bottom position of this view, in pixels.
|
||||
*/
|
||||
public static float getY2(View view) {
|
||||
return view.getBottom() + view.getTranslationY();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static View $$(Activity activity, @IdRes int id) {
|
||||
View result = activity.findViewById(id);
|
||||
|
||||
Reference in new issue
Block a user