Example usage for android.graphics Rect Rect

List of usage examples for android.graphics Rect Rect

Introduction

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

Prototype

public Rect(int left, int top, int right, int bottom) 

Source Link

Document

Create a new rectangle with the specified coordinates.

Usage

From source file:Main.java

public static Rect calculateDstRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight,
        ScaleType scalingLogic) {/*from ww w .j a  va2s . co m*/
    if (scalingLogic == ScaleType.FIT_XY) {
        final float srcAspect = (float) srcWidth / (float) srcHeight;
        final float dstAspect = (float) dstWidth / (float) dstHeight;
        if (srcAspect > dstAspect) {
            return new Rect(0, 0, dstWidth, (int) (dstWidth / srcAspect));
        } else {
            return new Rect(0, 0, (int) (dstHeight * srcAspect), dstHeight);
        }
    } else {
        return new Rect(0, 0, dstWidth, dstHeight);
    }
}

From source file:Main.java

private static Rect getBitmapRectCenterInsideHelper(int bitmapWidth, int bitmapHeight, int viewWidth,
        int viewHeight) {
    double viewToBitmapWidthRatio = 1.0D / 0.0;
    double viewToBitmapHeightRatio = 1.0D / 0.0;
    if (viewWidth < bitmapWidth) {
        viewToBitmapWidthRatio = (double) viewWidth / (double) bitmapWidth;
    }/*from w ww .  j a v a2s.  c  om*/

    if (viewHeight < bitmapHeight) {
        viewToBitmapHeightRatio = (double) viewHeight / (double) bitmapHeight;
    }

    double resultWidth;
    double resultHeight;
    if (viewToBitmapWidthRatio == 1.0D / 0.0 && viewToBitmapHeightRatio == 1.0D / 0.0) {
        resultHeight = (double) bitmapHeight;
        resultWidth = (double) bitmapWidth;
    } else if (viewToBitmapWidthRatio <= viewToBitmapHeightRatio) {
        resultWidth = (double) viewWidth;
        resultHeight = (double) bitmapHeight * resultWidth / (double) bitmapWidth;
    } else {
        resultHeight = (double) viewHeight;
        resultWidth = (double) bitmapWidth * resultHeight / (double) bitmapHeight;
    }

    int resultX;
    int resultY;
    if (resultWidth == (double) viewWidth) {
        resultX = 0;
        resultY = (int) Math.round(((double) viewHeight - resultHeight) / 2.0D);
    } else if (resultHeight == (double) viewHeight) {
        resultX = (int) Math.round(((double) viewWidth - resultWidth) / 2.0D);
        resultY = 0;
    } else {
        resultX = (int) Math.round(((double) viewWidth - resultWidth) / 2.0D);
        resultY = (int) Math.round(((double) viewHeight - resultHeight) / 2.0D);
    }

    Rect result = new Rect(resultX, resultY, resultX + (int) Math.ceil(resultWidth),
            resultY + (int) Math.ceil(resultHeight));
    return result;
}

From source file:Main.java

@TargetApi(10)
private static Bitmap getSidebarWallpaperApi10(Context context, SharedPreferences preferences, int sidebarWidth,
        int sidebarHeight) {
    boolean useBackground = preferences.getBoolean("pref_sidebar_custom_background", false);
    if (!useBackground)
        return null;

    String path = preferences.getString("pref_sidebar_background_image", null);
    if (path == null)
        return null;

    try {/*from  ww w  . j a va2 s. c  o  m*/
        BitmapFactory.Options bounds = decodeBounds(createWallpaperInputStream(context, path));
        int decodeHeight = Math.min(bounds.outHeight, sidebarHeight);
        int decodeWidth = Math.min(bounds.outWidth, sidebarWidth);
        InputStream in = createWallpaperInputStream(context, path);
        try {
            BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(in, false);
            BitmapFactory.Options options = new BitmapFactory.Options();
            return decoder.decodeRegion(new Rect(0, 0, decodeWidth, decodeHeight), options);
        } finally {
            in.close();
        }
    } catch (IOException e) {
        return null;
    }

}

From source file:Main.java

private static List<Camera.Area> buildMiddleArea(int areaPer1000) {
    return Collections
            .singletonList(new Camera.Area(new Rect(-areaPer1000, -areaPer1000, areaPer1000, areaPer1000), 1));
}

From source file:eu.sathra.io.adapters.RectAdapter.java

@Override
public Rect load(String param, JSONObject parent) throws JSONException {

    try {/*from   www. j  a  va  2s. c  o m*/
        JSONObject jObj = parent.getJSONObject(param);

        int top = jObj.optInt(PARAM_TOP, 0);
        int left = jObj.optInt(PARAM_LEFT, 0);
        int width = jObj.optInt(PARAM_WIDTH, 0);
        int height = jObj.optInt(PARAM_HEIGHT, 0);

        return new Rect(left, top, left + width, top + height);
    } catch (JSONException e) {
        return null;
    }
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
private static List<Camera.Area> buildMiddleArea(int areaPer1000) {
    return Collections
            .singletonList(new Camera.Area(new Rect(-areaPer1000, -areaPer1000, areaPer1000, areaPer1000), 1));
}

From source file:Main.java

/**
 * Given an input bitmap, scales it to the given width/height and makes it round.
 *
 * @param input {@link Bitmap} to scale and crop
 * @param targetWidth desired output width
 * @param targetHeight desired output height
 * @return output bitmap scaled to the target width/height and cropped to an oval. The
 *         cropping algorithm will try to fit as much of the input into the output as possible,
 *         while preserving the target width/height ratio.
 *//*from   w w  w  . j  a  v  a2 s. c  o  m*/
public static Bitmap getRoundedBitmap(Bitmap input, int targetWidth, int targetHeight) {
    if (input == null) {
        return null;
    }
    final Bitmap.Config inputConfig = input.getConfig();
    final Bitmap result = Bitmap.createBitmap(targetWidth, targetHeight,
            inputConfig != null ? inputConfig : Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(result);
    final Paint paint = new Paint();
    canvas.drawARGB(0, 0, 0, 0);
    paint.setAntiAlias(true);
    canvas.drawOval(0, 0, targetWidth, targetHeight, paint);

    // Specifies that only pixels present in the destination (i.e. the drawn oval) should
    // be overwritten with pixels from the input bitmap.
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

    final int inputWidth = input.getWidth();
    final int inputHeight = input.getHeight();

    // Choose the largest scale factor that will fit inside the dimensions of the
    // input bitmap.
    final float scaleBy = Math.min((float) inputWidth / targetWidth, (float) inputHeight / targetHeight);

    final int xCropAmountHalved = (int) (scaleBy * targetWidth / 2);
    final int yCropAmountHalved = (int) (scaleBy * targetHeight / 2);

    final Rect src = new Rect(inputWidth / 2 - xCropAmountHalved, inputHeight / 2 - yCropAmountHalved,
            inputWidth / 2 + xCropAmountHalved, inputHeight / 2 + yCropAmountHalved);

    final RectF dst = new RectF(0, 0, targetWidth, targetHeight);
    canvas.drawBitmap(input, src, dst, paint);
    return result;
}

From source file:Main.java

/**
 * Given an input bitmap, scales it to the given width/height and makes it round.
 *
 * @param input {@link Bitmap} to scale and crop
 * @param targetWidth desired output width
 * @param targetHeight desired output height
 * @return output bitmap scaled to the target width/height and cropped to an oval. The
 *         cropping algorithm will try to fit as much of the input into the output as possible,
 *         while preserving the target width/height ratio.
 *//*from   w w  w.j a  v a2 s . c o  m*/
public static Bitmap getRoundedBitmap(Bitmap input, int targetWidth, int targetHeight) {
    if (input == null) {
        return null;
    }
    final Bitmap.Config inputConfig = input.getConfig();
    final Bitmap result = Bitmap.createBitmap(targetWidth, targetHeight,
            inputConfig != null ? inputConfig : Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(result);
    final Paint paint = new Paint();
    canvas.drawARGB(0, 0, 0, 0);
    paint.setAntiAlias(true);
    final RectF dst = new RectF(0, 0, targetWidth, targetHeight);
    canvas.drawOval(dst, paint);

    // Specifies that only pixels present in the destination (i.e. the drawn oval) should
    // be overwritten with pixels from the input bitmap.
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

    final int inputWidth = input.getWidth();
    final int inputHeight = input.getHeight();

    // Choose the largest scale factor that will fit inside the dimensions of the
    // input bitmap.
    final float scaleBy = Math.min((float) inputWidth / targetWidth, (float) inputHeight / targetHeight);

    final int xCropAmountHalved = (int) (scaleBy * targetWidth / 2);
    final int yCropAmountHalved = (int) (scaleBy * targetHeight / 2);

    final Rect src = new Rect(inputWidth / 2 - xCropAmountHalved, inputHeight / 2 - yCropAmountHalved,
            inputWidth / 2 + xCropAmountHalved, inputHeight / 2 + yCropAmountHalved);

    canvas.drawBitmap(input, src, dst, paint);
    return result;
}

From source file:Main.java

/**
 * Load the image at {@code imagePath} as a {@link Bitmap}, scaling it to
 * the specified size and preserving the aspect ratio.
 * @param imagePath Path of the image to load.
 * @param width Required width of the resulting {@link Bitmap}.
 * @param height Required height of the resulting {@link Bitmap}.
 * @param fill {@code true} to fill the empty space with transparent color.
 * @param crop {@code true} to crop the image, {@code false} to resize without cutting the image.
 * @return {@link Bitmap} representing the image at {@code imagePath}.
 *///from   w  w w  .j a va  2s .com
public static Bitmap loadResizedBitmap(String imagePath, int width, int height, boolean fill, boolean crop) {
    Bitmap retVal;

    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = getScale(imagePath, width, height);
    opts.inJustDecodeBounds = false;

    Bitmap image = BitmapFactory.decodeFile(imagePath, opts);

    if (image == null) {
        if (imagePath != null) {
            Log.w("Helper", "Cannot decode " + imagePath);
        } else {
            Log.w("Helper", "Path is null: Cannot decode");
        }
        return null;
    }

    if (image.getWidth() != width || image.getHeight() != height) {
        //Image need to be resized.
        int scaledWidth = (image.getWidth() * height) / image.getHeight();
        int scaledHeight;
        if ((crop && scaledWidth > width) || (!crop && scaledWidth < width)) {
            scaledHeight = height;
        } else {
            scaledWidth = width;
            scaledHeight = (image.getHeight() * width) / image.getWidth();
        }

        Rect src = new Rect(0, 0, image.getWidth(), image.getHeight());
        Rect dst = new Rect(0, 0, scaledWidth, scaledHeight);

        if (fill) {
            retVal = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            dst.offset((width - scaledWidth) / 2, (height - scaledHeight) / 2);
        } else {
            retVal = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
        }
        retVal.eraseColor(Color.TRANSPARENT);

        synchronized (canvas) {
            if (antiAliasPaint == null) {
                antiAliasPaint = new Paint();
                antiAliasPaint.setAntiAlias(true);
                antiAliasPaint.setFilterBitmap(true);
                antiAliasPaint.setDither(true);
            }
            canvas.setBitmap(retVal);
            canvas.drawBitmap(image, src, dst, antiAliasPaint);
        }

        image.recycle();
    } else {
        //No need to scale.
        retVal = image;
    }

    return retVal;
}

From source file:android.support.transition.ChangeClipBounds.java

private void captureValues(TransitionValues values) {
    View view = values.view;/*  w w w  .  j av a  2 s  . com*/
    if (view.getVisibility() == View.GONE) {
        return;
    }

    Rect clip = ViewCompat.getClipBounds(view);
    values.values.put(PROPNAME_CLIP, clip);
    if (clip == null) {
        Rect bounds = new Rect(0, 0, view.getWidth(), view.getHeight());
        values.values.put(PROPNAME_BOUNDS, bounds);
    }
}