List of usage examples for android.graphics Rect height
public final int height()
From source file:Main.java
public static byte[] makeFontBitmap(String font, String code, int size, float[] arrayOfPos) { Canvas c = new Canvas(); Paint p = new Paint(); float density = context.getResources().getDisplayMetrics().density; // Log.v(TAG, String.format("makeFontBitmap called(Java): font=%s code=%s density=%f", font, code, density)); p.setTextSize((float) size * density); p.setAntiAlias(true);// w w w. j a v a 2 s . c om Rect textBounds = new Rect(); p.getTextBounds(code, 0, code.length(), textBounds); // Log.v(TAG, String.format("makeFontBitmap textBounds: %d,%d,%d,%d", textBounds.left, textBounds.top, textBounds.right, textBounds.bottom)); Rect textBoundsAxA = new Rect(); String axa = String.format("A%sA", code); p.getTextBounds(axa, 0, axa.length(), textBoundsAxA); Rect textBoundsAA = new Rect(); String aa = "AA"; p.getTextBounds(aa, 0, aa.length(), textBoundsAA); // cache.distDelta = Vec2(0, 0); arrayOfPos[0] = textBounds.left; arrayOfPos[1] = textBounds.top; // cache.srcWidth = Vec2(16, 16); arrayOfPos[2] = textBounds.width(); arrayOfPos[3] = textBounds.height(); // cache.step = 16; // arrayOfPos[4] = textBounds.width() + 1; arrayOfPos[4] = textBoundsAxA.width() - textBoundsAA.width(); if (textBounds.width() == 0 || textBounds.height() == 0) { Log.v(TAG, "makeFontBitmap: empty"); return null; } Bitmap b = Bitmap.createBitmap(textBounds.width(), textBounds.height(), Bitmap.Config.ARGB_8888); c.setBitmap(b); Rect r = new Rect(0, 0, textBounds.width(), textBounds.height()); // p.setColor(Color.RED); p.setARGB(0, 0, 0, 0); c.drawRect(r, p); p.setARGB(255, 255, 255, 255); // Log.v(TAG, "makeFontBitmap: drawText"); c.drawText(code, -textBounds.left, -textBounds.top, p); // Log.v(TAG, String.format("makeFontBitmap: w=%.2f h=%.2f", arrayOfPos[2], arrayOfPos[3])); ByteBuffer buf = ByteBuffer.allocate(textBounds.width() * textBounds.height() * 4); // Log.v(TAG, String.format("makeFontBitmap: b.getRowBytes() %d", b.getRowBytes())); buf.position(0); b.copyPixelsToBuffer(buf); // Log.v(TAG, String.format("makeFontBitmap results: capacity=%d", buf.capacity())); return buf.array(); }
From source file:Main.java
public static byte[] makeFontBitmap(String font, String code, int size, float[] arrayOfPos) { Log.v(TAG, String.format("makeFontBitmap called(Java): font=%s code=%s", font, code)); Canvas c = new Canvas(); Paint p = new Paint(); // Log.v(TAG, "get density"); float density = context.getResources().getDisplayMetrics().density; Log.v(TAG, String.format("makeFontBitmap density: %f", density)); p.setTextSize((float) size * density); p.setAntiAlias(true);/*from w ww . j av a2 s . c om*/ Rect textBounds = new Rect(); p.getTextBounds(code, 0, code.length(), textBounds); Log.v(TAG, String.format("makeFontBitmap textBounds: %d,%d,%d,%d", textBounds.left, textBounds.top, textBounds.right, textBounds.bottom)); Rect textBoundsAxA = new Rect(); String axa = String.format("A%sA", code); p.getTextBounds(axa, 0, axa.length(), textBoundsAxA); Rect textBoundsAA = new Rect(); String aa = "AA"; p.getTextBounds(aa, 0, aa.length(), textBoundsAA); // cache.distDelta = Vec2(0, 0); arrayOfPos[0] = textBounds.left; arrayOfPos[1] = textBounds.top; // cache.srcWidth = Vec2(16, 16); arrayOfPos[2] = textBounds.width(); arrayOfPos[3] = textBounds.height(); // cache.step = 16; // arrayOfPos[4] = textBounds.width() + 1; arrayOfPos[4] = textBoundsAxA.width() - textBoundsAA.width(); if (textBounds.width() == 0 || textBounds.height() == 0) { Log.v(TAG, "makeFontBitmap: empty"); return null; } Bitmap b = Bitmap.createBitmap(textBounds.width(), textBounds.height(), Bitmap.Config.ARGB_8888); c.setBitmap(b); Rect r = new Rect(0, 0, textBounds.width(), textBounds.height()); // p.setColor(Color.RED); p.setARGB(0, 0, 0, 0); c.drawRect(r, p); p.setARGB(255, 255, 255, 255); // Log.v(TAG, "makeFontBitmap: drawText"); c.drawText(code, -textBounds.left, -textBounds.top, p); Log.v(TAG, String.format("makeFontBitmap: w=%.2f h=%.2f", arrayOfPos[2], arrayOfPos[3])); ByteBuffer buf = ByteBuffer.allocate(textBounds.width() * textBounds.height() * 4); // ByteBuffer buf = ByteBuffer.allocate(b.getRowBytes()); Log.v(TAG, String.format("makeFontBitmap: b.getRowBytes() %d", b.getRowBytes())); buf.position(0); b.copyPixelsToBuffer(buf); Log.v(TAG, String.format("makeFontBitmap results: capacity=%d", buf.capacity())); // byte bytes[] = buf.array(); // for (int i = 0; i < size * size * 2; i++) // bytes[i] = (byte)(Math.random() * 255); return buf.array(); }
From source file:com.lcl6.cn.imagepickerl.AndroidImagePicker.java
public static Bitmap makeCropBitmap(Bitmap bitmap, Rect rectBox, RectF imageMatrixRect, int expectSize) { Bitmap bmp = bitmap;//from ww w . j a v a 2 s . c o m RectF localRectF = imageMatrixRect; float f = localRectF.width() / bmp.getWidth(); int left = (int) ((rectBox.left - localRectF.left) / f); int top = (int) ((rectBox.top - localRectF.top) / f); int width = (int) (rectBox.width() / f); int height = (int) (rectBox.height() / f); if (left < 0) { left = 0; } if (top < 0) { top = 0; } if (left + width > bmp.getWidth()) { width = bmp.getWidth() - left; } if (top + height > bmp.getHeight()) { height = bmp.getHeight() - top; } int k = width; if (width < expectSize) { k = expectSize; } if (width > expectSize) { k = expectSize; } try { bmp = Bitmap.createBitmap(bmp, left, top, width, height); if (k != width && k != height) {//don't do this if equals bmp = Bitmap.createScaledBitmap(bmp, k, k, true);//scale the bitmap } } catch (OutOfMemoryError localOutOfMemoryError1) { Log.v(TAG, "OOM when create bitmap"); } return bmp; }
From source file:org.appspot.apprtc.util.ThumbnailsCacheManager.java
public static void LoadImage(final String url, LoadImageCallback callback, final Resources resources, final String displayname, final boolean rounded, boolean fromCache) { final LoadImageCallback mCallback = callback; final Handler uiHandler = new Handler(); final int FG_COLOR = 0xFFFAFAFA; final String name = displayname; if (fromCache) { Bitmap bitmap = ThumbnailsCacheManager.getBitmapFromDiskCache(url); if (bitmap != null) { if (rounded) { RoundedBitmapDrawable roundedBitmap = RoundedBitmapDrawableFactory.create(resources, bitmap); roundedBitmap.setCircular(true); mCallback.onImageLoaded(roundedBitmap.getBitmap()); } else { mCallback.onImageLoaded(bitmap); }/*from ww w . jav a 2s . co m*/ return; } } AsyncHttpURLConnection httpConnection = new AsyncHttpURLConnection("GET", url, "", new AsyncHttpURLConnection.AsyncHttpEvents() { @Override public void onHttpError(String errorMessage) { Log.d("LoadImage", errorMessage); } @Override public void onHttpComplete(String response) { int size = 256; Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); final String trimmedName = name == null ? "" : name.trim(); drawTile(canvas, trimmedName, 0, 0, size, size); ThumbnailsCacheManager.addBitmapToCache(url, bitmap); onHttpComplete(bitmap); } @Override public void onHttpComplete(final Bitmap response) { if (mCallback != null) { uiHandler.post(new Runnable() { @Override public void run() { if (rounded) { RoundedBitmapDrawable roundedBitmap = RoundedBitmapDrawableFactory .create(resources, response); roundedBitmap.setCircular(true); mCallback.onImageLoaded(roundedBitmap.getBitmap()); } else { mCallback.onImageLoaded(response); } } }); } } private boolean drawTile(Canvas canvas, String letter, int tileColor, int left, int top, int right, int bottom) { letter = letter.toUpperCase(Locale.getDefault()); Paint tilePaint = new Paint(), textPaint = new Paint(); tilePaint.setColor(tileColor); textPaint.setFlags(Paint.ANTI_ALIAS_FLAG); textPaint.setColor(FG_COLOR); textPaint.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL)); textPaint.setTextSize((float) ((right - left) * 0.8)); Rect rect = new Rect(); canvas.drawRect(new Rect(left, top, right, bottom), tilePaint); textPaint.getTextBounds(letter, 0, 1, rect); float width = textPaint.measureText(letter); canvas.drawText(letter, (right + left) / 2 - width / 2, (top + bottom) / 2 + rect.height() / 2, textPaint); return true; } private boolean drawTile(Canvas canvas, String name, int left, int top, int right, int bottom) { if (name != null) { final String letter = getFirstLetter(name); final int color = ThumbnailsCacheManager.getColorForName(name); drawTile(canvas, letter, color, left, top, right, bottom); return true; } return false; } }); httpConnection.setBitmap(); httpConnection.send(); }
From source file:org.appspot.apprtc.util.ThumbnailsCacheManager.java
public static void LoadMenuImage(final String url, MenuItem menuItem, String displayname, final boolean rounded, Resources resources, boolean fromCache) { final Resources mResources = resources; final WeakReference<MenuItem> menuItemImage = new WeakReference<MenuItem>(menuItem); final Handler uiHandler = new Handler(); final int FG_COLOR = 0xFFFAFAFA; final String name = displayname; if (fromCache) { Bitmap bitmap = ThumbnailsCacheManager.getBitmapFromDiskCache(url); if (bitmap != null) { if (rounded) { RoundedBitmapDrawable roundedBitmap = RoundedBitmapDrawableFactory.create(mResources, bitmap); roundedBitmap.setCircular(true); menuItemImage.get().setIcon(roundedBitmap); } else { menuItemImage.get().setIcon(new BitmapDrawable(bitmap)); }/* ww w . j a va 2 s . c o m*/ return; } } AsyncHttpURLConnection httpConnection = new AsyncHttpURLConnection("GET", url, "", new AsyncHttpURLConnection.AsyncHttpEvents() { @Override public void onHttpError(String errorMessage) { Log.d("LoadImage", errorMessage); } @Override public void onHttpComplete(String response) { int size = 96; Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); final String trimmedName = name == null ? "" : name.trim(); drawTile(canvas, trimmedName, 0, 0, size, size); ThumbnailsCacheManager.addBitmapToCache(url, bitmap); onHttpComplete(bitmap); } @Override public void onHttpComplete(final Bitmap response) { if (menuItemImage != null && menuItemImage.get() != null) { uiHandler.post(new Runnable() { @Override public void run() { if (rounded) { RoundedBitmapDrawable roundedBitmap = RoundedBitmapDrawableFactory .create(mResources, response); roundedBitmap.setCircular(true); menuItemImage.get().setIcon(roundedBitmap); } else { menuItemImage.get().setIcon(new BitmapDrawable(response)); } } }); } } private boolean drawTile(Canvas canvas, String letter, int tileColor, int left, int top, int right, int bottom) { letter = letter.toUpperCase(Locale.getDefault()); Paint tilePaint = new Paint(), textPaint = new Paint(); tilePaint.setColor(tileColor); textPaint.setFlags(Paint.ANTI_ALIAS_FLAG); textPaint.setColor(FG_COLOR); textPaint.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL)); textPaint.setTextSize((float) ((right - left) * 0.8)); Rect rect = new Rect(); canvas.drawRect(new Rect(left, top, right, bottom), tilePaint); textPaint.getTextBounds(letter, 0, 1, rect); float width = textPaint.measureText(letter); canvas.drawText(letter, (right + left) / 2 - width / 2, (top + bottom) / 2 + rect.height() / 2, textPaint); return true; } private boolean drawTile(Canvas canvas, String name, int left, int top, int right, int bottom) { if (name != null) { final String letter = getFirstLetter(name); final int color = ThumbnailsCacheManager.getColorForName(name); drawTile(canvas, letter, color, left, top, right, bottom); return true; } return false; } }); httpConnection.setBitmap(); httpConnection.send(); }
From source file:org.appspot.apprtc.util.ThumbnailsCacheManager.java
public static void LoadImage(final String url, ImageView image, final String displayname, final boolean rounded, boolean fromCache) { final WeakReference<ImageView> imageView = new WeakReference<ImageView>(image); final Handler uiHandler = new Handler(); final int FG_COLOR = 0xFFFAFAFA; final String name = displayname; if (fromCache) { Bitmap bitmap = ThumbnailsCacheManager.getBitmapFromDiskCache(url); if (bitmap != null) { if (rounded) { RoundedBitmapDrawable roundedBitmap = RoundedBitmapDrawableFactory .create(imageView.get().getResources(), bitmap); roundedBitmap.setCircular(true); imageView.get().setImageDrawable(roundedBitmap); } else { imageView.get().setImageBitmap(bitmap); }/*from w ww. j a v a 2 s . c o m*/ imageView.get().setContentDescription(displayname); return; } } AsyncHttpURLConnection httpConnection = new AsyncHttpURLConnection("GET", url, "", new AsyncHttpURLConnection.AsyncHttpEvents() { @Override public void onHttpError(String errorMessage) { Log.d("LoadImage", errorMessage); } @Override public void onHttpComplete(String response) { int size = 96; Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); final String trimmedName = name == null ? "" : name.trim(); drawTile(canvas, trimmedName, 0, 0, size, size); ThumbnailsCacheManager.addBitmapToCache(url, bitmap); onHttpComplete(bitmap); } @Override public void onHttpComplete(final Bitmap response) { if (imageView != null && imageView.get() != null) { uiHandler.post(new Runnable() { @Override public void run() { if (imageView.get() != null) { if (rounded) { RoundedBitmapDrawable roundedBitmap = RoundedBitmapDrawableFactory .create(imageView.get().getResources(), response); roundedBitmap.setCircular(true); imageView.get().setImageDrawable(roundedBitmap); } else { imageView.get().setImageBitmap(response); } imageView.get().setContentDescription(displayname); } } }); } } private boolean drawTile(Canvas canvas, String letter, int tileColor, int left, int top, int right, int bottom) { letter = letter.toUpperCase(Locale.getDefault()); Paint tilePaint = new Paint(), textPaint = new Paint(); tilePaint.setColor(tileColor); textPaint.setFlags(Paint.ANTI_ALIAS_FLAG); textPaint.setColor(FG_COLOR); textPaint.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL)); textPaint.setTextSize((float) ((right - left) * 0.8)); Rect rect = new Rect(); canvas.drawRect(new Rect(left, top, right, bottom), tilePaint); textPaint.getTextBounds(letter, 0, 1, rect); float width = textPaint.measureText(letter); canvas.drawText(letter, (right + left) / 2 - width / 2, (top + bottom) / 2 + rect.height() / 2, textPaint); return true; } private boolean drawTile(Canvas canvas, String name, int left, int top, int right, int bottom) { if (name != null) { final String letter = getFirstLetter(name); final int color = ThumbnailsCacheManager.getColorForName(name); drawTile(canvas, letter, color, left, top, right, bottom); return true; } return false; } }); httpConnection.setBitmap(); httpConnection.send(); }
From source file:com.pizidea.imagepicker.AndroidImagePicker.java
public static Bitmap makeCropBitmap(Bitmap bitmap, Rect rectBox, RectF imageMatrixRect, int expectSize) { Bitmap bmp = bitmap;//from ww w. j a v a2 s. c om RectF localRectF = imageMatrixRect; float f = localRectF.width() / bmp.getWidth(); int left = (int) ((rectBox.left - localRectF.left) / f); int top = (int) ((rectBox.top - localRectF.top) / f); int width = (int) (rectBox.width() / f); int height = (int) (rectBox.height() / f); if (left < 0) { left = 0; } if (top < 0) { top = 0; } if (left + width > bmp.getWidth()) { width = bmp.getWidth() - left; } if (top + height > bmp.getHeight()) { height = bmp.getHeight() - top; } int k = width; if (width < expectSize) { k = expectSize; } if (width > expectSize) { k = expectSize; } try { bmp = Bitmap.createBitmap(bmp, left, top, width, height); if (k != width && k != height) {//don't do this if equals bmp = Bitmap.createScaledBitmap(bmp, k, k, true);//scale the bitmap } } catch (OutOfMemoryError localOutOfMemoryError1) { Log.v(TAG, "OOM when create bitmap"); } return bmp; }
From source file:Main.java
/** * Crop image bitmap from given bitmap using the given points in the original bitmap and the given rotation.<br> * if the rotation is not 0,90,180 or 270 degrees then we must first crop a larger area of the image that * contains the requires rectangle, rotate and then crop again a sub rectangle. * * @param scale how much to scale the cropped image part, use 0.5 to lower the image by half (OOM handling) *//*from ww w. j a va 2 s . c o m*/ private static Bitmap cropBitmapObjectWithScale(Bitmap bitmap, float[] points, int degreesRotated, boolean fixAspectRatio, int aspectRatioX, int aspectRatioY, float scale, boolean flipHorizontally, boolean flipVertically) { // get the rectangle in original image that contains the required cropped area (larger for non rectangular crop) Rect rect = getRectFromPoints(points, bitmap.getWidth(), bitmap.getHeight(), fixAspectRatio, aspectRatioX, aspectRatioY); if (degreesRotated == 90 || degreesRotated == 270) { if (flipHorizontally != flipVertically) { boolean temp = flipHorizontally; flipHorizontally = flipVertically; flipVertically = temp; } } // crop and rotate the cropped image in one operation float scaleX = flipHorizontally ? -scale : scale; float scaleY = flipVertically ? -scale : scale; Matrix matrix = new Matrix(); matrix.setScale(scaleX, scaleY); matrix.postRotate(degreesRotated, bitmap.getWidth() / 2, bitmap.getHeight() / 2); Bitmap result = Bitmap.createBitmap(bitmap, rect.left, rect.top, rect.width(), rect.height(), matrix, true); if (result == bitmap) { // corner case when all bitmap is selected, no worth optimizing for it result = bitmap.copy(bitmap.getConfig(), false); } // rotating by 0, 90, 180 or 270 degrees doesn't require extra cropping if (degreesRotated % 90 != 0) { // extra crop because non rectangular crop cannot be done directly on the image without rotating first result = cropForRotatedImage(result, points, rect, degreesRotated, fixAspectRatio, aspectRatioX, aspectRatioY); } return result; }
From source file:io.appium.uiautomator2.handler.GetSize.java
@Override public AppiumResponse safeHandle(IHttpRequest request) { Logger.info("Get Size of element command"); String id = getElementId(request); final JSONObject result = new JSONObject(); AndroidElement element = KnownElements.getElementFromCache(id); if (element == null) { return new AppiumResponse(getSessionId(request), WDStatus.NO_SUCH_ELEMENT, "Element Not found"); }/*from www . j a v a 2 s .c om*/ try { final Rect rect = element.getBounds(); result.put("width", rect.width()); result.put("height", rect.height()); } catch (UiObjectNotFoundException e) { Logger.error("Element not found: ", e); return new AppiumResponse(getSessionId(request), WDStatus.NO_SUCH_ELEMENT, e); } catch (JSONException e) { Logger.error("Exception while reading JSON: ", e); Logger.error(WDStatus.JSON_DECODER_ERROR, e); return new AppiumResponse(getSessionId(request), WDStatus.JSON_DECODER_ERROR, e); } return new AppiumResponse(getSessionId(request), WDStatus.SUCCESS, result); }
From source file:com.google.android.apps.common.testing.accessibility.framework.TouchTargetSizeInfoCheck.java
@Override public List<AccessibilityInfoCheckResult> runCheckOnInfo(AccessibilityNodeInfo info, Context context, Bundle metadata) {//from www .ja va2 s. c om ArrayList<AccessibilityInfoCheckResult> results = new ArrayList<AccessibilityInfoCheckResult>(); // TODO(sjrush): Have all info checks use AccessibilityNodeInfoCompat AccessibilityNodeInfoCompat infoCompat = new AccessibilityNodeInfoCompat(info); if (!(AccessibilityNodeInfoUtils.isClickable(infoCompat) || AccessibilityNodeInfoUtils.isLongClickable(infoCompat))) { results.add(new AccessibilityInfoCheckResult(this.getClass(), AccessibilityCheckResultType.NOT_RUN, "View is not clickable", info)); return results; } if (context == null) { results.add(new AccessibilityInfoCheckResult(this.getClass(), AccessibilityCheckResultType.NOT_RUN, "This check needs a context", info)); return results; } // TODO(sjrush): Find a way to make this check work without a context // dp calculation is pixels/density float density = context.getResources().getDisplayMetrics().density; Rect bounds = new Rect(); info.getBoundsInScreen(bounds); float targetHeight = bounds.height() / density; float targetWidth = bounds.width() / density; if (targetHeight < TOUCH_TARGET_MIN_HEIGHT || targetWidth < TOUCH_TARGET_MIN_WIDTH) { String message = String.format(Locale.US, "View is too small of a touch target. Minimum touch target size is %dx%ddp. " + "Actual size is %.1fx%.1fdp (screen density is %.1f).", TOUCH_TARGET_MIN_WIDTH, TOUCH_TARGET_MIN_HEIGHT, targetWidth, targetHeight, density); results.add(new AccessibilityInfoCheckResult(this.getClass(), AccessibilityCheckResultType.ERROR, message, info)); } return results; }