Example usage for android.graphics Rect offset

List of usage examples for android.graphics Rect offset

Introduction

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

Prototype

public void offset(int dx, int dy) 

Source Link

Document

Offset the rectangle by adding dx to its left and right coordinates, and adding dy to its top and bottom coordinates.

Usage

From source file:Main.java

static Rect getViewAbsRect(View view, int parentX, int parentY) {
    int[] loc = new int[2];
    view.getLocationInWindow(loc);/*w  w w  .j a v  a2 s  .  c  o  m*/
    Rect rect = new Rect();
    rect.set(loc[0], loc[1], loc[0] + view.getMeasuredWidth(), loc[1] + view.getMeasuredHeight());
    rect.offset(-parentX, -parentY);
    return rect;
}

From source file:Main.java

/**
 * Scales a rect about its centroid//from   www  . j a v a 2  s.c  o  m
 */
public static void scaleRectAboutCenter(Rect r, float scale) {
    if (scale != 1.0f) {
        int cx = r.centerX();
        int cy = r.centerY();
        r.offset(-cx, -cy);
        r.left = (int) (r.left * scale + 0.5f);
        r.top = (int) (r.top * scale + 0.5f);
        r.right = (int) (r.right * scale + 0.5f);
        r.bottom = (int) (r.bottom * scale + 0.5f);
        r.offset(cx, cy);
    }
}

From source file:Main.java

public static boolean inViewInBounds(View view, int x, int y) {
    Rect outRect = new Rect();
    int[] location = new int[2];

    view.getDrawingRect(outRect);/*from   w ww . j  av a 2 s  .  c  o  m*/
    view.getLocationOnScreen(location);
    outRect.offset(location[0], location[1]);
    return outRect.contains(x, y);
}

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}.
 */// w  w w  . j a v a 2s  .  c  o  m
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) {
        return null;
    }

    if (image.getWidth() != width || image.getHeight() != height) {
        int scaledWidth = (image.getWidth() * height) / image.getHeight();
        int scaledHeight; // = (image.getHeight() * width) / image.getWidth();
        if ((crop && scaledWidth > width) || (!crop && scaledWidth < width)) {
            scaledHeight = height;
        } else {
            scaledWidth = width;
            scaledHeight = (image.getHeight() * width) / image.getWidth();
        }
        //image = Bitmap.createScaledBitmap(image, scaledWidth, scaledHeight, true);

        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: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   ww w .  j  a v a 2 s  .  c om
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: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  ww  .ja v a 2 s .co  m
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) {
        return null;
    }

    if (image.getWidth() != width || image.getHeight() != height) {
        //Image need to be resized.
        //         int scaledWidth = image.getWidth();
        //         int scaledHeight = image.getHeight();
        //         final float factorWidth = scaledWidth / width;
        //         final float factorHeight = scaledHeight / height;
        //final float factor = (scaledWidth / width) - (scaledHeight / height);
        //         final long factor = (scaledWidth * height) - (scaledHeight * width);
        //         if ((crop && factor > 0) || (factor < 0)) {
        //            scaledHeight = (scaledHeight * width) / scaledWidth;
        //            scaledWidth = width;
        //         } else {
        //            scaledWidth = (scaledWidth * height) / scaledHeight;
        //            scaledHeight = height;
        //         }
        int scaledWidth = (image.getWidth() * height) / image.getHeight();
        int scaledHeight; // = (image.getHeight() * width) / image.getWidth();
        if ((crop && scaledWidth > width) || (!crop && scaledWidth < width)) {
            scaledHeight = height;
        } else {
            scaledWidth = width;
            scaledHeight = (image.getHeight() * width) / image.getWidth();
        }
        //image = Bitmap.createScaledBitmap(image, scaledWidth, scaledHeight, true);

        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:Main.java

public static void drawToCenterOfCanvas(Canvas canvas, Bitmap bp, int max_width, int max_height,
        Rect cacheRect) {
    final int b_w = bp.getWidth();
    final int b_h = bp.getHeight();
    if (b_w <= max_width && b_h <= max_height) { // center aligned
        canvas.drawBitmap(bp, (max_width - b_w) / 2, (max_height - b_h) / 2, null);
    } else { // scaled fix given rect size
        final float s_w = 1.0f * max_width / b_w;
        final float s_h = 1.0f * max_height / b_h;
        final float f_s = Math.min(s_w, s_h);
        final int f_w = (int) (b_w * f_s);
        final int f_h = (int) (b_h * f_s);
        cacheRect.set(0, 0, f_w, f_h);/*from  w  w  w .j a  v a2 s. c  om*/
        cacheRect.offset((max_width - f_w) / 2, (max_height - f_h) / 2);
        canvas.drawBitmap(bp, null, cacheRect, null);
    }
}

From source file:com.daycle.daycleapp.custom.swipelistview.itemmanipulation.swipedismiss.SwipeTouchListener.java

private static Rect getChildViewRect(final View parentView, final View childView) {
    Rect childRect = new Rect(childView.getLeft(), childView.getTop(), childView.getRight(),
            childView.getBottom());/* w w  w  . ja v a  2 s. c  o  m*/
    if (!parentView.equals(childView)) {
        View workingChildView = childView;
        ViewGroup parent;
        while (!(parent = (ViewGroup) workingChildView.getParent()).equals(parentView)) {
            childRect.offset(parent.getLeft(), parent.getTop());
            workingChildView = parent;
        }
    }
    return childRect;
}

From source file:com.atwal.wakeup.battery.util.Utilities.java

public static void scaleRectAboutCenter(Rect r, float scale) {
    int cx = r.centerX();
    int cy = r.centerY();
    r.offset(-cx, -cy);
    Utilities.scaleRect(r, scale);
    r.offset(cx, cy);//w  ww .  j a  v  a 2 s.co m
}

From source file:tyrantgit.explosionfield.ExplosionField.java

public void explode(final View view) {
    Rect r = new Rect();
    view.getGlobalVisibleRect(r);// w  ww.  ja  va 2  s.  co m
    int[] location = new int[2];
    getLocationOnScreen(location);
    r.offset(-location[0], -location[1]);
    r.inset(-mExpandInset[0], -mExpandInset[1]);
    int startDelay = 100;
    ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f).setDuration(150);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

        Random random = new Random();

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {

            ViewCompat.setTranslationX(view, (random.nextFloat() - 0.5f) * view.getWidth() * 0.05f);
            ViewCompat.setTranslationY(view, (random.nextFloat() - 0.5f) * view.getHeight() * 0.05f);

        }
    });
    animator.start();
    //ViewCompat.animate(view)
    ViewCompat.animate(view).setDuration(150).setStartDelay(startDelay).scaleX(0f).scaleY(0f).alpha(0f).start();
    explode(Utils.createBitmapFromView(view), r, startDelay, ExplosionAnimator.DEFAULT_DURATION);
}