Skip to content

Instantly share code, notes, and snippets.

@alexandrius
Created November 18, 2016 08:03
Show Gist options
  • Save alexandrius/824f7bf6a0a9ae32c66b11f0a4720826 to your computer and use it in GitHub Desktop.
Save alexandrius/824f7bf6a0a9ae32c66b11f0a4720826 to your computer and use it in GitHub Desktop.
UiUtils
package ge.bog.cbtransactions.utils;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
/**
* Created by alex on 7/18/16.
*/
public class BitmapUtils {
public static Bitmap scaleDown(Bitmap realImage, float maxImageSize, boolean filter) {
float ratio = Math.min(maxImageSize / realImage.getWidth(), maxImageSize / realImage.getHeight());
int width = Math.round(ratio * realImage.getWidth());
int height = Math.round(ratio * realImage.getHeight());
return Bitmap.createScaledBitmap(realImage, width, height, filter);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap blurBitmap(Bitmap bitmap, Context context) {
Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
RenderScript rs = RenderScript.create(context);
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
blurScript.setRadius(25f);
blurScript.setInput(allIn);
blurScript.forEach(allOut);
allOut.copyTo(outBitmap);
bitmap.recycle();
rs.destroy();
return outBitmap;
}
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if (bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
public static void saveBlurredBackgroundImage(Activity activity) {
if (Utils.atLeastJellyBean17()) {
Bitmap screenShot = UiUtils.getActivityScreenshot(activity, true);
Bitmap blurredBitmap = BitmapUtils.blurBitmap(screenShot, activity);
IOUtils.saveBitmap(blurredBitmap, IOUtils.getBlurredBackgroundPath(activity));
}
}
}
package ge.bog.cbtransactions.utils;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;
import java.lang.reflect.Method;
import ge.bog.cbtransactions.R;
import ge.bog.cbtransactions.main.App;
/**
* Created by alex on 7/14/16.
*/
public class UiUtils {
public static void shakeView(View v, Animation.AnimationListener listener) {
Animation animation = AnimationUtils.loadAnimation(v.getContext(), R.anim.wiggle_anim);
animation.setAnimationListener(listener);
v.startAnimation(animation);
}
public static void shakeView(View v) {
shakeView(v, null);
}
public static void showToast(String toast) {
showToast(toast, Toast.LENGTH_SHORT);
}
public static void showToast(String toast, int length) {
Toast.makeText(App.getInstance(), toast, length).show();
}
public static int getStatusBarHeight(Context c) {
int result = 0;
int resourceId = c.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = c.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
public static int getDisplayHeight(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
return getDisplaySize(display).y;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Point getDisplaySize(Display display) {
if (Utils.atLeastJellyBean17()) {
Point outPoint = new Point();
DisplayMetrics metrics = new DisplayMetrics();
display.getRealMetrics(metrics);
outPoint.x = metrics.widthPixels;
outPoint.y = metrics.heightPixels;
return outPoint;
}
return getRealSize(display);
}
public static Point getRealSize(Display display) {
Point outPoint = new Point();
Method mGetRawH;
try {
mGetRawH = Display.class.getMethod("getRawHeight");
Method mGetRawW = Display.class.getMethod("getRawWidth");
outPoint.x = (Integer) mGetRawW.invoke(display);
outPoint.y = (Integer) mGetRawH.invoke(display);
return outPoint;
} catch (Throwable e) {
return null;
}
}
public static Bitmap getActivityScreenshot(Activity activity, boolean cropDecor) {
int statusBarHeight = 0;
int navBarHeight = Settings.getInt(Settings.NAVIGATION_BAR_HEIGHT);
if (cropDecor) {
statusBarHeight = UiUtils.getStatusBarHeight(activity);
}
View decor = activity.getWindow().getDecorView().getRootView();
Bitmap result = Bitmap.createBitmap(decor.getWidth(), decor.getHeight() - statusBarHeight - navBarHeight, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(result);
Bitmap bmp = getViewScreenshot(decor);
Rect src = new Rect(0, statusBarHeight, bmp.getWidth(), bmp.getHeight() - navBarHeight);
Rect dst = new Rect(0, 0, result.getWidth(), result.getHeight());
c.drawBitmap(bmp, src, dst, null);
result = BitmapUtils.scaleDown(result, result.getHeight() / 2, true);
return result;
}
public static Bitmap getViewScreenshot(View view) {
Bitmap bitmap;
view.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
return bitmap;
}
public static void forceEditTextLoseFocus(EditText editText, Context c, View toFocus) {
InputMethodManager imm = (InputMethodManager) c.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
editText.clearFocus();
toFocus.requestFocus();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment