List of usage examples for android.graphics Rect width
public final int width()
From source file:com.pizidea.imagepicker.AndroidImagePicker.java
public static Bitmap makeCropBitmap(Bitmap bitmap, Rect rectBox, RectF imageMatrixRect, int expectSize) { Bitmap bmp = bitmap;//from www . j a va 2s .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: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) *//* ww w . j a v a 2s . 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:com.fastbootmobile.encore.utils.Utils.java
/** * Calculates the optimal size of the text based on the text view width * * @param textView The text view in which the text should fit * @param desiredWidth The desired final text width, or -1 for the TextView's getMeasuredWidth *//*from w ww. ja v a 2 s . co m*/ public static void adjustTextSize(TextView textView, int desiredWidth) { if (desiredWidth <= 0) { desiredWidth = textView.getMeasuredWidth(); if (desiredWidth <= 0) { // Invalid width, don't do anything Log.w("Utils", "adjustTextSize: Not doing anything (measured width invalid)"); return; } } // Add some margin to width desiredWidth *= 0.8f; Paint paint = new Paint(); Rect bounds = new Rect(); paint.setTypeface(textView.getTypeface()); float textSize = textView.getTextSize() * 2.0f; paint.setTextSize(textSize); String text = textView.getText().toString(); paint.getTextBounds(text, 0, text.length(), bounds); while (bounds.width() > desiredWidth) { textSize--; paint.setTextSize(textSize); paint.getTextBounds(text, 0, text.length(), bounds); } textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); }
From source file:com.android.utils.traversal.DirectionalTraversalStrategy.java
/** * Find the distance on the minor axis w.r.t the direction to the nearest * edge of the destination rectangle.//from w w w . j a v a 2 s . com * @param direction the direction (up, down, left, right) * @param source The source rect. * @param dest The destination rect. * @return The distance. */ static int minorAxisDistance(int direction, Rect source, Rect dest) { switch (direction) { case TraversalStrategy.SEARCH_FOCUS_LEFT: case TraversalStrategy.SEARCH_FOCUS_RIGHT: // the distance between the center verticals return Math.abs(((source.top + source.height() / 2) - ((dest.top + dest.height() / 2)))); case TraversalStrategy.SEARCH_FOCUS_UP: case TraversalStrategy.SEARCH_FOCUS_DOWN: // the distance between the center horizontals return Math.abs(((source.left + source.width() / 2) - ((dest.left + dest.width() / 2)))); } throw new IllegalArgumentException("direction must be a SearchDirection"); }
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 ww w . jav a 2 s . co m*/ 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.android.camera.one.v2.OneCameraZslImpl.java
/** * Calculate the aspect ratio of the full size capture on this device. * * @param characteristics the characteristics of the camera device. * @return The aspect ration, in terms of width/height of the full capture * size./* w w w.j a v a 2 s .c o m*/ */ private static float calculateFullSizeAspectRatio(CameraCharacteristics characteristics) { Rect activeArraySize = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE); return ((float) activeArraySize.width()) / activeArraySize.height(); }
From source file:com.busdrone.android.ui.VehicleMarkerRenderer.java
private Bitmap render(int color, String text) { TextPaint textPaint = new TextPaint(); textPaint.setColor(Color.WHITE); textPaint.setStyle(Paint.Style.FILL); textPaint.setAntiAlias(true);/*from w ww . j a v a 2 s . c om*/ textPaint.setTextSize(mTextSize); Rect textBounds = new Rect(); textPaint.getTextBounds(text, 0, text.length(), textBounds); int width = mPadding + textBounds.width() + mPadding; int height = mPadding + textBounds.height() + mPadding; Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); paint.setColor(color); canvas.drawRoundRect(new RectF(0, 0, width, height), mCornerRadius, mCornerRadius, paint); canvas.drawText(text, (width / 2f) - (textBounds.width() / 2f), (height / 2f) + (textBounds.height() / 2f), textPaint); return bitmap; }
From source file:net.exclaimindustries.geohashdroid.wiki.WikiPictureEditor.java
private static void drawStrings(String[] strings, Canvas c, Paint textPaint, Paint backgroundPaint) { // FIXME: The math here is ugly and blunt and probably not too // efficient or flexible. It might even fail. This needs to be // fixed and made less-ugly later. // We need SOME strings. If we've got nothing, bail out. if (strings.length < 1) return;/*from w w w.ja va 2 s. c om*/ // First, init our variables. This is as good a place as any to do so. Rect textBounds = new Rect(); int[] heights = new int[strings.length]; int totalHeight = INFOBOX_MARGIN * 2; int longestWidth = 0; // Now, loop through the strings, adding to the height and keeping track // of the longest width. int i = 0; for (String s : strings) { textPaint.getTextBounds(s, 0, s.length(), textBounds); if (textBounds.width() > longestWidth) longestWidth = textBounds.width(); totalHeight += textBounds.height(); heights[i] = textBounds.height(); i++; } // Now, we have us a rectangle. Draw that. Rect drawBounds = new Rect(c.getWidth() - longestWidth - (INFOBOX_MARGIN * 2), 0, c.getWidth(), totalHeight); c.drawRect(drawBounds, backgroundPaint); // Now, place each of the strings. We'll assume the topmost one is in // index 0. They should all be left-justified, too. i = 0; int curHeight = 0; for (String s : strings) { Log.d(DEBUG_TAG, "Drawing " + s + " at " + (drawBounds.left + INFOBOX_MARGIN) + "," + (INFOBOX_MARGIN + (INFOBOX_PADDING * (i + 1)) + curHeight)); c.drawText(s, drawBounds.left + INFOBOX_MARGIN, INFOBOX_MARGIN + (INFOBOX_PADDING * (i + 1)) + curHeight, textPaint); curHeight += heights[i]; i++; } }
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 w ww .jav a 2 s. co m*/ 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; }
From source file:com.android.gallery3d.filtershow.info.InfoPanel.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (getDialog() != null) { getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); }/*from ww w .j a v a 2s. c om*/ mMainView = (LinearLayout) inflater.inflate(R.layout.filtershow_info_panel, null, false); mImageThumbnail = (ImageView) mMainView.findViewById(R.id.imageThumbnail); Bitmap bitmap = MasterImage.getImage().getFilteredImage(); mImageThumbnail.setImageBitmap(bitmap); mImageName = (TextView) mMainView.findViewById(R.id.imageName); mImageSize = (TextView) mMainView.findViewById(R.id.imageSize); mExifData = (TextView) mMainView.findViewById(R.id.exifData); TextView exifLabel = (TextView) mMainView.findViewById(R.id.exifLabel); HistogramView histogramView = (HistogramView) mMainView.findViewById(R.id.histogramView); histogramView.setBitmap(bitmap); Uri uri = MasterImage.getImage().getUri(); String path = ImageLoader.getLocalPathFromUri(getActivity(), uri); Uri localUri = null; if (path != null) { localUri = Uri.parse(path); } if (localUri != null) { mImageName.setText(localUri.getLastPathSegment()); } Rect originalBounds = MasterImage.getImage().getOriginalBounds(); mImageSize.setText("" + originalBounds.width() + " x " + originalBounds.height()); List<ExifTag> exif = MasterImage.getImage().getEXIF(); String exifString = ""; boolean hasExifData = false; if (exif != null) { for (ExifTag tag : exif) { exifString += createStringFromIfFound(tag, ExifInterface.TAG_MODEL, R.string.filtershow_exif_model); exifString += createStringFromIfFound(tag, ExifInterface.TAG_APERTURE_VALUE, R.string.filtershow_exif_aperture); exifString += createStringFromIfFound(tag, ExifInterface.TAG_FOCAL_LENGTH, R.string.filtershow_exif_focal_length); exifString += createStringFromIfFound(tag, ExifInterface.TAG_ISO_SPEED_RATINGS, R.string.filtershow_exif_iso); exifString += createStringFromIfFound(tag, ExifInterface.TAG_SUBJECT_DISTANCE, R.string.filtershow_exif_subject_distance); exifString += createStringFromIfFound(tag, ExifInterface.TAG_DATE_TIME_ORIGINAL, R.string.filtershow_exif_date); exifString += createStringFromIfFound(tag, ExifInterface.TAG_F_NUMBER, R.string.filtershow_exif_f_stop); exifString += createStringFromIfFound(tag, ExifInterface.TAG_EXPOSURE_TIME, R.string.filtershow_exif_exposure_time); exifString += createStringFromIfFound(tag, ExifInterface.TAG_COPYRIGHT, R.string.filtershow_exif_copyright); hasExifData = true; } } if (hasExifData) { exifLabel.setVisibility(View.VISIBLE); mExifData.setText(Html.fromHtml(exifString)); } else { exifLabel.setVisibility(View.GONE); } return mMainView; }