Example usage for android.graphics Rect height

List of usage examples for android.graphics Rect height

Introduction

In this page you can find the example usage for android.graphics Rect height.

Prototype

public final int height() 

Source Link

Usage

From source file:Main.java

/**
 * Transform source Bitmap to targeted width and height.
 *///from w  w w . j  av  a 2  s  .c om
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)) {
        /*
         * In this case the bitmap is smaller, at least in one dimension,
         * than the target. Transform it by placing as much of the image as
         * possible into the target and leaving the top/bottom or left/right
         * (or both) black.
         */
        Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight, 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();
        }
        return b2;
    }
    float bitmapWidthF = source.getWidth();
    float bitmapHeightF = source.getHeight();

    float bitmapAspect = bitmapWidthF / bitmapHeightF;
    float viewAspect = (float) targetWidth / targetHeight;

    if (bitmapAspect > viewAspect) {
        float scale = targetHeight / bitmapHeightF;
        if (scale < .9F || scale > 1F) {
            scaler.setScale(scale, scale);
        } else {
            scaler = null;
        }
    } else {
        float scale = targetWidth / bitmapWidthF;
        if (scale < .9F || scale > 1F) {
            scaler.setScale(scale, scale);
        } else {
            scaler = null;
        }
    }

    Bitmap b1;
    if (scaler != null) {
        // this is used for minithumb and crop, so we want to filter here.
        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) {
        if (recycle || b1 != source) {
            b1.recycle();
        }
    }

    return b2;
}

From source file:android.support.design.testutils.TestUtilsMatchers.java

/**
 * Returns a matcher that matches TextViews whose start drawable is filled with the specified
 * fill color./*  w  w w  . jav  a2  s. c  om*/
 */
public static Matcher withStartDrawableFilledWith(final @ColorInt int fillColor,
        final int allowedComponentVariance) {
    return new BoundedMatcher<View, TextView>(TextView.class) {
        private String failedCheckDescription;

        @Override
        public void describeTo(final Description description) {
            description.appendText(failedCheckDescription);
        }

        @Override
        public boolean matchesSafely(final TextView view) {
            final Drawable[] compoundDrawables = view.getCompoundDrawables();
            final boolean isRtl = (ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL);
            final Drawable startDrawable = isRtl ? compoundDrawables[2] : compoundDrawables[0];
            if (startDrawable == null) {
                failedCheckDescription = "no start drawable";
                return false;
            }
            try {
                final Rect bounds = startDrawable.getBounds();
                TestUtils.assertAllPixelsOfColor("", startDrawable, bounds.width(), bounds.height(), true,
                        fillColor, allowedComponentVariance, true);
            } catch (Throwable t) {
                failedCheckDescription = t.getMessage();
                return false;
            }
            return true;
        }
    };
}

From source file:com.ruesga.rview.misc.BitmapUtils.java

public static void adjustRectToMinimumSize(Rect r, int size) {
    int w = r.width();
    int h = r.height();
    if (w > size || h > size) {
        if (w == h && w > size) {
            r.right = r.bottom = size;//w w  w.  j a  va 2 s.  c  om
        } else if (w < h && w > size) {
            r.right = w * size / h;
            r.bottom = size;
        } else {
            r.bottom = h * size / w;
            r.right = size;
        }
    }
}

From source file:com.cooltechworks.views.ScratchTextView.java

private static int[] getTextDimens(String text, Paint paint) {

    int end = text.length();
    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, end, bounds);
    int width = bounds.left + bounds.width();
    int height = bounds.bottom + bounds.height();

    return new int[] { width, height };
}

From source file:Main.java

/**
 * Transform source Bitmap to targeted width and height.
 *///from   w ww  . jav a  2  s.  co  m
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)) {
        /*
         * In this case the bitmap is smaller, at least in one dimension,
         * than the target.  Transform it by placing as much of the image
         * as possible into the target and leaving the top/bottom or
         * left/right (or both) black.
         */
        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();
        }
        return b2;
    }
    float bitmapWidthF = source.getWidth();
    float bitmapHeightF = source.getHeight();

    float bitmapAspect = bitmapWidthF / bitmapHeightF;
    float viewAspect = (float) targetWidth / targetHeight;

    if (bitmapAspect > viewAspect) {
        float scale = targetHeight / bitmapHeightF;
        if (scale < .9F || scale > 1F) {
            scaler.setScale(scale, scale);
        } else {
            scaler = null;
        }
    } else {
        float scale = targetWidth / bitmapWidthF;
        if (scale < .9F || scale > 1F) {
            scaler.setScale(scale, scale);
        } else {
            scaler = null;
        }
    }

    Bitmap b1;
    if (scaler != null) {
        // this is used for minithumb and crop, so we want to filter here.
        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) {
        if (recycle || b1 != source) {
            b1.recycle();
        }
    }

    return b2;
}

From source file:com.ruesga.rview.misc.BitmapUtils.java

public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int dstWidth, int dstHeight,
        ScalingLogic scalingLogic) {//www . java 2s  . c o m
    if (unscaledBitmap.getWidth() == dstWidth && unscaledBitmap.getHeight() == dstHeight) {
        return unscaledBitmap;
    }
    Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight,
            scalingLogic);
    Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight,
            scalingLogic);
    Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(scaledBitmap);
    canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
    return scaledBitmap;
}

From source file:com.ruesga.rview.misc.BitmapUtils.java

@SuppressWarnings("ResultOfMethodCallIgnored")
public static File optimizeImage(Context context, InputStream source, Bitmap.CompressFormat format, int quality)
        throws IOException {
    // Copy original source to a temp file
    File f = null;/* ww  w . j  a  v a 2 s. c  o m*/
    OutputStream os = null;
    try {
        f = CacheHelper.createNewTemporaryFile(context, "." + format.name());
        if (f != null) {
            os = new BufferedOutputStream(new FileOutputStream(f), 4096);
            IOUtils.copy(source, os);
            IOUtils.closeQuietly(os);

            // Compress to webp format
            long start = System.currentTimeMillis();
            int[] size = decodeBitmapSize(f);
            if (size[0] != -1 && size[1] != -1) {
                Rect r = new Rect(0, 0, size[0], size[1]);
                adjustRectToMinimumSize(r, calculateMaxAvailableSize(context));
                Bitmap src = createUnscaledBitmap(f, r.width(), r.height());

                try {
                    long originalSize = f.length();
                    os = new BufferedOutputStream(new FileOutputStream(f), 4096);
                    src.compress(format, quality, os);
                    IOUtils.closeQuietly(os);

                    long end = System.currentTimeMillis();
                    Log.d(TAG,
                            "Compressed " + f + " to " + format.name() + " in " + (end - start) + "ms"
                                    + "; dimensions " + src.getWidth() + "x" + src.getHeight() + "; size: "
                                    + f.length() + "; original: " + originalSize);
                    return f;

                } finally {
                    src.recycle();
                }
            } else {
                f.delete();
            }
        }
    } catch (IOException ex) {
        IOUtils.closeQuietly(os);
        if (f != null) {
            f.delete();
        }
        throw ex;
    } finally {
        IOUtils.closeQuietly(os);
        IOUtils.closeQuietly(source);
    }

    return null;
}

From source file:Main.java

public static Bitmap cropMaxVisibleBitmap(Drawable drawable, int iconSize) {
    Bitmap tmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);//  w  ww .  j av  a  2 s.c om
    Canvas canvas = new Canvas(tmp);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    Rect crop = new Rect(tmp.getWidth(), tmp.getHeight(), -1, -1);
    for (int y = 0; y < tmp.getHeight(); y++) {
        for (int x = 0; x < tmp.getWidth(); x++) {
            int alpha = (tmp.getPixel(x, y) >> 24) & 255;
            if (alpha > 0) { // pixel is not 100% transparent
                if (x < crop.left)
                    crop.left = x;
                if (x > crop.right)
                    crop.right = x;
                if (y < crop.top)
                    crop.top = y;
                if (y > crop.bottom)
                    crop.bottom = y;
            }
        }
    }

    if (crop.width() <= 0 || crop.height() <= 0) {
        return Bitmap.createScaledBitmap(tmp, iconSize, iconSize, true);
    }

    // We want to crop a square region.
    float size = Math.max(crop.width(), crop.height());
    float xShift = (size - crop.width()) * 0.5f;
    crop.left -= Math.floor(xShift);
    crop.right += Math.ceil(xShift);

    float yShift = (size - crop.height()) * 0.5f;
    crop.top -= Math.floor(yShift);
    crop.bottom += Math.ceil(yShift);

    Bitmap finalImage = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888);
    canvas.setBitmap(finalImage);
    float scale = iconSize / size;

    canvas.scale(scale, scale);
    canvas.drawBitmap(tmp, -crop.left, -crop.top, new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
    canvas.setBitmap(null);
    return finalImage;
}

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  va 2 s.  co  m*/
 * @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:org.cocos2dx.lib.Cocos2dxBitmap.java

private static int getFontSizeAccordingHeight(int height) {
    Paint paint = new Paint();
    Rect bounds = new Rect();

    paint.setTypeface(Typeface.DEFAULT);
    int incr_text_size = 1;
    boolean found_desired_size = false;

    while (!found_desired_size) {

        paint.setTextSize(incr_text_size);
        String text = "SghMNy";
        paint.getTextBounds(text, 0, text.length(), bounds);

        incr_text_size++;/*from w  w w  .j av  a 2s.  c  o  m*/

        if (height - bounds.height() <= 2) {
            found_desired_size = true;
        }
        Log.d("font size", "incr size:" + incr_text_size);
    }
    return incr_text_size;
}