List of usage examples for android.graphics Rect Rect
public Rect()
From source file:Main.java
public static Bitmap loadSizeLimitedBitmapFromUri(Uri imageUri, ContentResolver contentResolver) { try {//from w w w. j a v a2s . c om // Load the image into InputStream. InputStream imageInputStream = contentResolver.openInputStream(imageUri); // For saving memory, only decode the image meta and get the side length. BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Rect outPadding = new Rect(); BitmapFactory.decodeStream(imageInputStream, outPadding, options); // Calculate shrink rate when loading the image into memory. int maxSideLength = options.outWidth > options.outHeight ? options.outWidth : options.outHeight; options.inSampleSize = 1; options.inSampleSize = calculateSampleSize(maxSideLength, IMAGE_MAX_SIDE_LENGTH); options.inJustDecodeBounds = false; imageInputStream.close(); // Load the bitmap and resize it to the expected size length imageInputStream = contentResolver.openInputStream(imageUri); Bitmap bitmap = BitmapFactory.decodeStream(imageInputStream, outPadding, options); maxSideLength = bitmap.getWidth() > bitmap.getHeight() ? bitmap.getWidth() : bitmap.getHeight(); double ratio = IMAGE_MAX_SIDE_LENGTH / (double) maxSideLength; if (ratio < 1) { bitmap = Bitmap.createScaledBitmap(bitmap, (int) (bitmap.getWidth() * ratio), (int) (bitmap.getHeight() * ratio), false); } return rotateBitmap(bitmap, getImageRotationAngle(imageUri, contentResolver)); } catch (Exception e) { return null; } }
From source file:Main.java
public static Bitmap loadSizeLimitedBitmapFromUri(Uri imageUri, ContentResolver contentResolver) { try {//from w w w . jav a 2s . co m // Load the image into InputStream. InputStream imageInputStream = contentResolver.openInputStream(imageUri); // For saving memory, only decode the image meta and get the side length. BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Rect outPadding = new Rect(); BitmapFactory.decodeStream(imageInputStream, outPadding, options); // Calculate shrink rate when loading the image into memory. int maxSideLength = options.outWidth > options.outHeight ? options.outWidth : options.outHeight; options.inSampleSize = 1; options.inSampleSize = calculateSampleSize(maxSideLength, IMAGE_MAX_SIDE_LENGTH); options.inJustDecodeBounds = false; if (imageInputStream != null) { imageInputStream.close(); } // Load the bitmap and resize it to the expected size length imageInputStream = contentResolver.openInputStream(imageUri); Bitmap bitmap = BitmapFactory.decodeStream(imageInputStream, outPadding, options); maxSideLength = bitmap.getWidth() > bitmap.getHeight() ? bitmap.getWidth() : bitmap.getHeight(); double ratio = IMAGE_MAX_SIDE_LENGTH / (double) maxSideLength; if (ratio < 1) { bitmap = Bitmap.createScaledBitmap(bitmap, (int) (bitmap.getWidth() * ratio), (int) (bitmap.getHeight() * ratio), false); } return rotateBitmap(bitmap, getImageRotationAngle(imageUri, contentResolver)); } catch (Exception e) { return null; } }
From source file:Main.java
private static Bitmap loadAssetBitmap(Context context, String assetPath) { InputStream is = null;//from ww w. ja v a 2s . c om try { Resources resources = context.getResources(); Options options = new Options(); options.inDensity = DisplayMetrics.DENSITY_HIGH; options.inScreenDensity = resources.getDisplayMetrics().densityDpi; options.inTargetDensity = resources.getDisplayMetrics().densityDpi; is = context.getAssets().open(assetPath); Bitmap bitmap = BitmapFactory.decodeStream(is, new Rect(), options); if (bitmap != null) { drawableCache.put(assetPath, bitmap); } return bitmap; } catch (Exception e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }
From source file:Main.java
/** * @param top/*from w w w .ja v a 2s. c o m*/ * @param left * @param bottom * @param right * @param delegate */ public static void increaseHitRectBy(final int top, final int left, final int bottom, final int right, final View delegate) { final View parent = (View) delegate.getParent(); if (parent != null && delegate.getContext() != null) { parent.post(new Runnable() { // Post in the parent's message queue to make sure the parent // lays out its children before we call getHitRect() public void run() { final float densityDpi = delegate.getContext().getResources().getDisplayMetrics().densityDpi; final Rect r = new Rect(); delegate.getHitRect(r); r.top -= transformToDensityPixel(top, densityDpi); r.left -= transformToDensityPixel(left, densityDpi); r.bottom += transformToDensityPixel(bottom, densityDpi); r.right += transformToDensityPixel(right, densityDpi); parent.setTouchDelegate(new TouchDelegate(r, delegate)); } }); } }
From source file:Main.java
@Nullable private static Rect getRecyclerViewRect(ViewParent parent) { if (parent == null) { // view is not attached to RecyclerView return null; }/*from w w w . jav a 2 s . c om*/ if (!(parent instanceof View)) { return null; } Rect rect = new Rect(); Point offset = new Point(); ((View) parent).getGlobalVisibleRect(rect, offset); return rect; }
From source file:Main.java
public static Bitmap drawTextToBitmap(Context context, int resId, String text) { Resources resources = context.getResources(); float scale = resources.getDisplayMetrics().density; Bitmap bitmap = BitmapFactory.decodeResource(resources, resId); Bitmap.Config bitmapConfig = bitmap.getConfig(); if (bitmapConfig == null) bitmapConfig = Bitmap.Config.ARGB_8888; bitmap = bitmap.copy(bitmapConfig, true); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(context.getResources().getColor(android.R.color.white)); paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); paint.setTextSize((int) (12 * scale)); Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); int x = (bitmap.getWidth() - bounds.width()) / 4; int y = (bitmap.getHeight() + bounds.height()) / 5; canvas.drawText(text, x * scale, y * scale, paint); return bitmap; }
From source file:Main.java
public static Bitmap drawTextCenterToBitmap(Bitmap bitmap, String text, int textSize, int textColor) { android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig(); // set default bitmap config if none if (bitmapConfig == null) { bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888; }//from w ww .ja v a2 s . co m // resource bitmaps are imutable, // so we need to convert it to mutable one bitmap = bitmap.copy(bitmapConfig, true); Canvas canvas = new Canvas(bitmap); // new antialised Paint Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // text color - #3D3D3D paint.setColor(textColor); // text size in pixels paint.setTextSize(textSize); // text shadow paint.setShadowLayer(1f, 0f, 1f, Color.WHITE); // draw text to the Canvas center Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); //int x = (bitmap.getWidth() - bounds.width()) / 2; //int y = (bitmap.getHeight() + bounds.height()) / 2; //draw text to the bottom int x = (bitmap.getWidth() - bounds.width()) / 10 * 5; int y = (bitmap.getHeight() + bounds.height()) / 10 * 5; canvas.drawText(text, x, y, paint); return bitmap; }
From source file:Main.java
public static Rect getBoundsInScreen(AccessibilityNodeInfo nodeInfo) { Rect rect = new Rect(); nodeInfo.getBoundsInScreen(rect); return rect; }
From source file:Main.java
/** * Display a {@link Toast} letting the user know what an item does when long * pressed.// w ww . ja v a2 s. c om * * @param view The {@link View} to copy the content description from. */ public static void showCheatSheet(final View view) { final int[] screenPos = new int[2]; // origin is device display final Rect displayFrame = new Rect(); // includes decorations (e.g. // status bar) view.getLocationOnScreen(screenPos); view.getWindowVisibleDisplayFrame(displayFrame); final Context context = view.getContext(); final int viewWidth = view.getWidth(); final int viewHeight = view.getHeight(); final int viewCenterX = screenPos[0] + viewWidth / 2; final int screenWidth = context.getResources().getDisplayMetrics().widthPixels; final int estimatedToastHeight = (int) (48 * context.getResources().getDisplayMetrics().density); final Toast cheatSheet = Toast.makeText(context, view.getContentDescription(), Toast.LENGTH_SHORT); final boolean showBelow = screenPos[1] < estimatedToastHeight; if (showBelow) { // Show below // Offsets are after decorations (e.g. status bar) are factored in cheatSheet.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, viewCenterX - screenWidth / 2, screenPos[1] - displayFrame.top + viewHeight); } else { // Show above // Offsets are after decorations (e.g. status bar) are factored in cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, viewCenterX - screenWidth / 2, displayFrame.bottom - screenPos[1]); } cheatSheet.show(); }
From source file:Main.java
/** * get status bar height./* w ww.j av a 2s .c om*/ * <br /> * rely on the fact that te status bar is shown at the time you make your computation, * <strong> * getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN) * this will not work!! * </strong> * * @param activity * @return */ public static int getStatusBarHeight(Activity activity) { Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); return frame.top; }