Migrated some legacy functions to kotlin extensions
This commit is contained in:
parent
ee300722af
commit
08f4c62b4a
18 changed files with 290 additions and 662 deletions
|
@ -14,128 +14,17 @@
|
|||
|
||||
package code.name.monkey.retromusic.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Matrix;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.graphics.PorterDuffColorFilter;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
||||
import androidx.annotation.ColorInt;
|
||||
import androidx.annotation.DrawableRes;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.content.res.ResourcesCompat;
|
||||
import androidx.exifinterface.media.ExifInterface;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import code.name.monkey.appthemehelper.util.TintHelper;
|
||||
|
||||
/**
|
||||
* Created on : June 18, 2016 Author : zetbaitsu Name : Zetra GitHub : https://github.com/zetbaitsu
|
||||
*/
|
||||
public class ImageUtil {
|
||||
|
||||
private static final int TOLERANCE = 20;
|
||||
// Alpha amount for which values below are considered transparent.
|
||||
private static final int ALPHA_TOLERANCE = 50;
|
||||
private static int[] mTempBuffer;
|
||||
|
||||
private ImageUtil() {}
|
||||
|
||||
public static boolean isGrayscale(Bitmap bitmap) {
|
||||
final int height = bitmap.getHeight();
|
||||
final int width = bitmap.getWidth();
|
||||
int size = height * width;
|
||||
ensureBufferSize(size);
|
||||
bitmap.getPixels(mTempBuffer, 0, width, 0, 0, width, height);
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (!isGrayscale(mTempBuffer[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static Bitmap createBitmap(Drawable drawable) {
|
||||
return createBitmap(drawable, 1f);
|
||||
}
|
||||
|
||||
public static Bitmap createBitmap(Drawable drawable, float sizeMultiplier) {
|
||||
Bitmap bitmap =
|
||||
Bitmap.createBitmap(
|
||||
(int) (drawable.getIntrinsicWidth() * sizeMultiplier),
|
||||
(int) (drawable.getIntrinsicHeight() * sizeMultiplier),
|
||||
Bitmap.Config.ARGB_8888);
|
||||
Canvas c = new Canvas(bitmap);
|
||||
drawable.setBounds(0, 0, c.getWidth(), c.getHeight());
|
||||
drawable.draw(c);
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
public static Drawable getTintedVectorDrawable(
|
||||
@NonNull Resources res,
|
||||
@DrawableRes int resId,
|
||||
@Nullable Resources.Theme theme,
|
||||
@ColorInt int color) {
|
||||
return TintHelper.createTintedDrawable(getVectorDrawable(res, resId, theme), color);
|
||||
}
|
||||
|
||||
public static Drawable getTintedVectorDrawable(
|
||||
@NonNull Context context, @DrawableRes int id, @ColorInt int color) {
|
||||
return TintHelper.createTintedDrawable(
|
||||
getVectorDrawable(context.getResources(), id, context.getTheme()), color);
|
||||
}
|
||||
|
||||
public static Drawable getVectorDrawable(@NonNull Context context, @DrawableRes int id) {
|
||||
return getVectorDrawable(context.getResources(), id, context.getTheme());
|
||||
}
|
||||
|
||||
public static Drawable getVectorDrawable(
|
||||
@NonNull Resources res, @DrawableRes int resId, @Nullable Resources.Theme theme) {
|
||||
return ResourcesCompat.getDrawable(res,resId, theme);
|
||||
}
|
||||
|
||||
/** Makes sure that {@code mTempBuffer} has at least length {@code size}. */
|
||||
private static void ensureBufferSize(int size) {
|
||||
if (mTempBuffer == null || mTempBuffer.length < size) {
|
||||
mTempBuffer = new int[size];
|
||||
}
|
||||
}
|
||||
|
||||
public static Bitmap setBitmapColor(Bitmap bitmap, int color) {
|
||||
Bitmap result =
|
||||
Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth() - 1, bitmap.getHeight() - 1);
|
||||
Paint paint = new Paint();
|
||||
paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
|
||||
|
||||
Canvas canvas = new Canvas(result);
|
||||
canvas.drawBitmap(result, 0, 0, paint);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static boolean isGrayscale(int color) {
|
||||
int alpha = 0xFF & (color >> 24);
|
||||
if (alpha < ALPHA_TOLERANCE) {
|
||||
return true;
|
||||
}
|
||||
int r = 0xFF & (color >> 16);
|
||||
int g = 0xFF & (color >> 8);
|
||||
int b = 0xFF & color;
|
||||
return Math.abs(r - g) < TOLERANCE
|
||||
&& Math.abs(r - b) < TOLERANCE
|
||||
&& Math.abs(g - b) < TOLERANCE;
|
||||
} // Amount (max is 255) that two channels can differ before the color is no longer "gray".
|
||||
|
||||
public static Bitmap resizeBitmap(@NonNull Bitmap src, int maxForSmallerSize) {
|
||||
int width = src.getWidth();
|
||||
int height = src.getHeight();
|
||||
|
@ -185,108 +74,4 @@ public class ImageUtil {
|
|||
|
||||
return inSampleSize;
|
||||
}
|
||||
|
||||
static File compressImage(
|
||||
File imageFile,
|
||||
int reqWidth,
|
||||
int reqHeight,
|
||||
Bitmap.CompressFormat compressFormat,
|
||||
int quality,
|
||||
String destinationPath)
|
||||
throws IOException {
|
||||
FileOutputStream fileOutputStream = null;
|
||||
File file = new File(destinationPath).getParentFile();
|
||||
if (!file.exists()) {
|
||||
file.mkdirs();
|
||||
}
|
||||
try {
|
||||
fileOutputStream = new FileOutputStream(destinationPath);
|
||||
// write the compressed bitmap at the destination specified by destinationPath.
|
||||
decodeSampledBitmapFromFile(imageFile, reqWidth, reqHeight)
|
||||
.compress(compressFormat, quality, fileOutputStream);
|
||||
} finally {
|
||||
if (fileOutputStream != null) {
|
||||
fileOutputStream.flush();
|
||||
fileOutputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
return new File(destinationPath);
|
||||
}
|
||||
|
||||
static Bitmap decodeSampledBitmapFromFile(File imageFile, int reqWidth, int reqHeight)
|
||||
throws IOException {
|
||||
// First decode with inJustDecodeBounds=true to check dimensions
|
||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||
options.inJustDecodeBounds = true;
|
||||
BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
|
||||
|
||||
// Calculate inSampleSize
|
||||
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
|
||||
|
||||
// Decode bitmap with inSampleSize set
|
||||
options.inJustDecodeBounds = false;
|
||||
|
||||
Bitmap scaledBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
|
||||
|
||||
// check the rotation of the image and display it properly
|
||||
ExifInterface exif;
|
||||
exif = new ExifInterface(imageFile.getAbsolutePath());
|
||||
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
|
||||
Matrix matrix = new Matrix();
|
||||
if (orientation == 6) {
|
||||
matrix.postRotate(90);
|
||||
} else if (orientation == 3) {
|
||||
matrix.postRotate(180);
|
||||
} else if (orientation == 8) {
|
||||
matrix.postRotate(270);
|
||||
}
|
||||
scaledBitmap =
|
||||
Bitmap.createBitmap(
|
||||
scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
|
||||
return scaledBitmap;
|
||||
}
|
||||
|
||||
private static int calculateInSampleSize(
|
||||
BitmapFactory.Options options, int reqWidth, int reqHeight) {
|
||||
// Raw height and width of image
|
||||
final int height = options.outHeight;
|
||||
final int width = options.outWidth;
|
||||
int inSampleSize = 1;
|
||||
|
||||
if (height > reqHeight || width > reqWidth) {
|
||||
|
||||
final int halfHeight = height / 2;
|
||||
final int halfWidth = width / 2;
|
||||
|
||||
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
|
||||
// height and width larger than the requested height and width.
|
||||
while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
|
||||
inSampleSize *= 2;
|
||||
}
|
||||
}
|
||||
|
||||
return inSampleSize;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static Bitmap getResizedBitmap(@NonNull Bitmap image, int maxSize) {
|
||||
int width = image.getWidth();
|
||||
int height = image.getHeight();
|
||||
|
||||
float bitmapRatio = (float) width / (float) height;
|
||||
if (bitmapRatio > 1) {
|
||||
width = maxSize;
|
||||
height = (int) (width / bitmapRatio);
|
||||
} else {
|
||||
height = maxSize;
|
||||
width = (int) (height * bitmapRatio);
|
||||
}
|
||||
return Bitmap.createScaledBitmap(image, width, height, true);
|
||||
}
|
||||
|
||||
public static Bitmap resize(InputStream stream, int scaledWidth, int scaledHeight) {
|
||||
final Bitmap bitmap = BitmapFactory.decodeStream(stream);
|
||||
return Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,14 +15,7 @@ package code.name.monkey.retromusic.util
|
|||
|
||||
import android.content.Context
|
||||
import android.content.res.Configuration
|
||||
import android.content.res.Resources
|
||||
import android.content.res.Resources.Theme
|
||||
import android.graphics.Point
|
||||
import android.graphics.drawable.Drawable
|
||||
import androidx.annotation.ColorInt
|
||||
import androidx.annotation.DrawableRes
|
||||
import androidx.core.content.res.ResourcesCompat
|
||||
import code.name.monkey.appthemehelper.util.TintHelper
|
||||
import code.name.monkey.retromusic.App.Companion.getContext
|
||||
import java.net.InetAddress
|
||||
import java.net.NetworkInterface
|
||||
|
@ -30,8 +23,8 @@ import java.text.DecimalFormat
|
|||
import java.util.*
|
||||
|
||||
object RetroUtil {
|
||||
fun formatValue(value: Float): String {
|
||||
var value = value
|
||||
fun formatValue(numValue: Float): String {
|
||||
var value = numValue
|
||||
val arr = arrayOf("", "K", "M", "B", "T", "P", "E")
|
||||
var index = 0
|
||||
while (value / 1000 >= 1) {
|
||||
|
@ -76,30 +69,6 @@ object RetroUtil {
|
|||
return result
|
||||
}
|
||||
|
||||
fun getTintedVectorDrawable(
|
||||
context: Context,
|
||||
@DrawableRes id: Int,
|
||||
@ColorInt color: Int,
|
||||
): Drawable {
|
||||
return TintHelper.createTintedDrawable(
|
||||
getVectorDrawable(context.resources, id, context.theme), color)
|
||||
}
|
||||
|
||||
fun getTintedVectorDrawable(
|
||||
res: Resources,
|
||||
@DrawableRes resId: Int,
|
||||
theme: Theme?,
|
||||
@ColorInt color: Int,
|
||||
): Drawable {
|
||||
return TintHelper.createTintedDrawable(getVectorDrawable(res, resId, theme), color)
|
||||
}
|
||||
|
||||
private fun getVectorDrawable(
|
||||
res: Resources, @DrawableRes resId: Int, theme: Theme?,
|
||||
): Drawable? {
|
||||
return ResourcesCompat.getDrawable(res, resId, theme)
|
||||
}
|
||||
|
||||
val isLandscape: Boolean
|
||||
get() = (getContext().resources.configuration.orientation
|
||||
== Configuration.ORIENTATION_LANDSCAPE)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue