List of usage examples for android.graphics Rect height
public final int height()
From source file:Main.java
private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize, final int color) { final Paint paint = new Paint(); paint.setAntiAlias(true);/*from w w w . j a va 2 s .c o m*/ paint.setTextAlign(Align.CENTER); paint.setTextSize(textSize); paint.setColor(color); final Rect bounds = new Rect(); paint.getTextBounds(MORE_SUGGESTIONS_HINT, 0, MORE_SUGGESTIONS_HINT.length(), bounds); final int width = Math.round(bounds.width() + 0.5f); final int height = Math.round(bounds.height() + 0.5f); final Bitmap buffer = Bitmap.createBitmap(width, (height * 3 / 2), Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(buffer); canvas.drawText(MORE_SUGGESTIONS_HINT, width / 2, height, paint); BitmapDrawable bitmapDrawable = new BitmapDrawable(res, buffer); bitmapDrawable.setTargetDensity(canvas); return bitmapDrawable; }
From source file:Main.java
public static int[] getApplicationWidthAndHeight(Activity activity) { Rect outRect = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect); System.out.println("top:" + outRect.top + " ; left: " + outRect.left); int info[] = new int[2]; info[0] = outRect.width();/*from w ww . ja va2 s. co m*/ info[1] = outRect.height(); return info; }
From source file:Main.java
public static Bitmap drawTextToBitmap(Context mContext, int resourceId, String mText) { try {/*from w ww .j a v a2 s . c o m*/ Resources resources = mContext.getResources(); float scale = resources.getDisplayMetrics().density; Bitmap bitmap = BitmapFactory.decodeResource(resources, resourceId); android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig(); // set default bitmap config if none if (bitmapConfig == null) { bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888; } // 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(Color.rgb(77, 77, 77)); // text size in pixels paint.setTextSize((int) (13 * scale)); // text shadow paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY); // draw text to the Canvas center Rect bounds = new Rect(); paint.getTextBounds(mText, 0, mText.length(), bounds); int x = (int) ((bitmap.getWidth() - bounds.width()) / 4); int y = (int) ((bitmap.getHeight() + bounds.height()) / 4); canvas.drawText(mText, x * scale, y * scale, paint); return bitmap; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * The most useful answer about text drawing ever. http://stackoverflow.com/a/32081250 *//*w ww . j a v a 2 s.co m*/ @Nullable public static Bitmap createTypefaceBitmap(final Context context, @NonNull final String text, final int color, final float textSizePx) { final Typeface robotoMedium = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"); final Paint paint = new Paint(); paint.setAntiAlias(true); paint.setSubpixelText(true); paint.setTypeface(robotoMedium); paint.setStyle(Paint.Style.FILL); paint.setColor(color); paint.setTextSize(textSizePx); final Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); Bitmap bitmap = null; if (!bounds.isEmpty()) { final int width = bounds.width(); final int height = bounds.height(); bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444); final float x = bounds.left; final float y = height - bounds.bottom; final Canvas canvas = new Canvas(bitmap); canvas.drawText(text, x, y, paint); } return bitmap; }
From source file:Main.java
public static Bitmap getFocusedBitmap(Context context, Camera camera, Bitmap bmp, Rect box) { Point CamRes = getCameraResolution(context, camera); Point ScrRes = getScreenResolution(context); int SW = ScrRes.x; int SH = ScrRes.y; int RW = box.width(); int RH = box.height(); int RL = box.left; int RT = box.top; float RSW = (float) (RW * Math.pow(SW, -1)); float RSH = (float) (RH * Math.pow(SH, -1)); float RSL = (float) (RL * Math.pow(SW, -1)); float RST = (float) (RT * Math.pow(SH, -1)); int CW = CamRes.x; int CH = CamRes.y; if (CW > CH) bmp = rotateBitmap(bmp, 90);//from w ww . j av a2 s. c o m int BW = bmp.getWidth(); int BH = bmp.getHeight(); int RBL = (int) (RSL * BW); int RBT = (int) (RST * BH); int RBW = (int) (RSW * BW); int RBH = (int) (RSH * BH); Bitmap res = Bitmap.createBitmap(bmp, RBL, RBT, RBW, RBH); bmp.recycle(); return res; }
From source file:Main.java
public static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, boolean scaleUp, boolean fitInScreen) { if (fitInScreen) { source = scaleTo(scaler, source, targetWidth, targetHeight, true); }//w w w . jav a 2 s. c o m int deltaX = source.getWidth() - targetWidth; int deltaY = source.getHeight() - targetHeight; if ((!scaleUp || fitInScreen) && (deltaX < 0 || deltaY < 0)) { Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b2); int deltaXHalf = Math.max(0, deltaX / 2); int deltaYHalf = Math.max(0, deltaY / 2); Rect src = new Rect(deltaXHalf, deltaYHalf, deltaXHalf + Math.min(targetWidth, source.getWidth()), deltaYHalf + Math.min(targetHeight, source.getHeight())); int dstX = (targetWidth - src.width()) / 2; int dstY = (targetHeight - src.height()) / 2; Rect dst = new Rect(dstX, dstY, targetWidth - dstX, targetHeight - dstY); c.drawBitmap(source, src, dst, null); source.recycle(); return b2; } Bitmap b1 = scaleTo(scaler, source, targetWidth, targetHeight, false); int dx1 = Math.max(0, b1.getWidth() - targetWidth); int dy1 = Math.max(0, b1.getHeight() - targetHeight); Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth, targetHeight); b1.recycle(); return b2; }
From source file:Main.java
private static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, int options) { boolean scaleUp = (options & OPTIONS_SCALE_UP) != 0; boolean recycle = (options & OPTIONS_RECYCLE_INPUT) != 0; int deltaX = source.getWidth() - targetWidth; int deltaY = source.getHeight() - targetHeight; if (!scaleUp && (deltaX < 0 || deltaY < 0)) { Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b2); int deltaXHalf = Math.max(0, deltaX / 2); int deltaYHalf = Math.max(0, deltaY / 2); Rect src = new Rect(deltaXHalf, deltaYHalf, deltaXHalf + Math.min(targetWidth, source.getWidth()), deltaYHalf + Math.min(targetHeight, source.getHeight())); int dstX = (targetWidth - src.width()) / 2; int dstY = (targetHeight - src.height()) / 2; Rect dst = new Rect(dstX, dstY, targetWidth - dstX, targetHeight - dstY); c.drawBitmap(source, src, dst, null); if (recycle) source.recycle();//from w ww . j a v a2s. c om return b2; } float bitmapWidthF = source.getWidth(); float bitmapHeightF = source.getHeight(); float bitmapAspect = bitmapWidthF / bitmapHeightF; float viewAspect = (float) targetWidth / targetHeight; float scale = bitmapAspect > viewAspect ? targetHeight / bitmapHeightF : targetWidth / bitmapWidthF; if (scale < .9F || scale > 1F) scaler.setScale(scale, scale); else scaler = null; Bitmap b1; if (scaler != null) b1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), scaler, true); else b1 = source; if (recycle && b1 != source) source.recycle(); int dx1 = Math.max(0, b1.getWidth() - targetWidth); int dy1 = Math.max(0, b1.getHeight() - targetHeight); Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth, targetHeight); if (b2 != b1 && (recycle || b1 != source)) b1.recycle(); return b2; }
From source file:enterprayz.megatools.Tools.java
public static int getTextHeight(String text, int maxWidth, float textSize, Typeface typeface) { TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG); paint.setTextSize(textSize);/*from ww w.j ava 2s . c o m*/ paint.setTypeface(typeface); int lineCount = 0; int index = 0; int length = text.length(); while (index < length - 1) { index += paint.breakText(text, index, length, true, maxWidth, null); lineCount++; } Rect bounds = new Rect(); paint.getTextBounds("Py", 0, 2, bounds); return (int) Math.floor(lineCount * bounds.height()); }
From source file:Main.java
public static Rect calculateActivationIndicatorSize(int sensitivity, int offsetPosition, int offsetSize, boolean isOnRightSide, Rect availableRect) { int top = availableRect.top; int left = availableRect.left; int right = availableRect.right; int bottom = availableRect.bottom; if (isOnRightSide) { left = right - sensitivity;//ww w . j a va 2 s. c o m } else { right = left + sensitivity; } int height = availableRect.height() - offsetSize; top = top + offsetPosition + (offsetSize / 2); bottom = top + height; Rect result = new Rect(left, top, right, bottom); if (!result.intersect(availableRect)) { return availableRect; } return result; }
From source file:Main.java
public static Bitmap asBitmap(Drawable drawable, int minWidth, int minHeight) { final Rect tmpRect = new Rect(); drawable.copyBounds(tmpRect);//w ww .ja v a2s. c om if (tmpRect.isEmpty()) { tmpRect.set(0, 0, Math.max(minWidth, drawable.getIntrinsicWidth()), Math.max(minHeight, drawable.getIntrinsicHeight())); drawable.setBounds(tmpRect); } Bitmap bitmap = Bitmap.createBitmap(tmpRect.width(), tmpRect.height(), Bitmap.Config.ARGB_8888); drawable.draw(new Canvas(bitmap)); return bitmap; }