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() 

Source Link

Document

Create a new empty Rect.

Usage

From source file:Main.java

private static synchronized Bitmap createScaledBitmap(Bitmap bitmap, final int width, final int height,
        float cornerRadius) {

    if (bitmap == null) {
        return null;
    }//from   w  w w .  ja  va 2s.c om

    int adjustedWidth = width;
    int adjustedHeight = height;

    final int bitmapWidth = bitmap.getWidth();
    final int bitmapHeight = bitmap.getHeight();

    //int inBytes = bitmap.getByteCount();
    if (width >= bitmapWidth && height >= bitmapHeight)
        return bitmap;

    if (width > 0 && height > 0) {
        //if (width < bitmapWidth || height < bitmapHeight) {
        final float ratio = (float) bitmapWidth / bitmapHeight;

        if (bitmapWidth > bitmapHeight) {
            adjustedHeight = (int) (width / ratio);
        } else if (bitmapHeight > bitmapWidth) {
            adjustedWidth = (int) (height * ratio);
        }

        final Bitmap.Config c = Bitmap.Config.ARGB_8888;
        final Bitmap thumb = Bitmap.createBitmap(width, height, c);
        final Canvas canvas = sScaleCanvas;
        final Paint paint = sPaint;
        canvas.setBitmap(thumb);
        paint.setDither(false);
        paint.setFilterBitmap(true);

        Rect sBounds = new Rect();
        Rect sOldBounds = new Rect();

        sBounds.set((width - adjustedWidth) >> 1, (height - adjustedHeight) >> 1, adjustedWidth,
                adjustedHeight);
        sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);

        if (cornerRadius != 0) {
            //Path p = new Path();
            RectF rect = new RectF(sBounds);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(Color.WHITE);
            canvas.drawRoundRect(rect, cornerRadius, cornerRadius, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            //p.addRoundRect(rect, cornerRadius, cornerRadius, Direction.CCW);
            //canvas.clipPath(p, Op.REPLACE);
        } else {
            paint.setXfermode(null);
            //canvas.clipRect(0, 0, thumb.getWidth(), thumb.getHeight());
        }

        canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);

        canvas.setBitmap(Bitmap.createBitmap(1, 1, Config.ALPHA_8));

        return thumb;

    }
    return bitmap;
}

From source file:Main.java

public static Bitmap getBitmap(Context context, int resId) {
    try {//from ww w. j  av a2  s .  co m
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inDither = true;
        InputStream is = context.getResources().openRawResource(resId);
        Bitmap bitmap = BitmapFactory.decodeStream(is, new Rect(), options);
        is.close();
        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:catchla.yep.adapter.decorator.DividerItemDecoration.java

public DividerItemDecoration(Context context, int orientation) {
    mPadding = new Rect();
    final TypedArray a = context.obtainStyledAttributes(ATTRS);
    mDivider = a.getDrawable(0);/* w  w  w .jav a2  s  . com*/
    a.recycle();
    setOrientation(orientation);
}

From source file:android.support.v7ox.widget.ContentFrameLayout.java

public ContentFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mDecorPadding = new Rect();
}

From source file:Main.java

/**
 * get face region, converted info image coordinated.
 *
 * @param width/*from   w w  w. j av  a  2s.c o  m*/
 *            image width
 * @param height
 *            image height
 * @param left
 *            left
 * @param top
 *            top
 * @param right
 *            right
 * @param bottom
 *            bottom
 * @return screen coordinated region
 */
public static Rect getFaceRect(double width, double height, double left, double top, double right,
        double bottom) {
    Log.d(TAG, "<getFaceRect> width:" + width + ",height:" + height + ",orientation:" + ",left:" + left
            + ",top:" + top + ",right:" + right + ",bottom:" + bottom);

    Rect res = new Rect();
    res.left = (int) ((left + HALF_RECT_LEN) * width / RECT_LEN);
    res.top = (int) ((top + HALF_RECT_LEN) * height / RECT_LEN);
    res.right = (int) ((right + HALF_RECT_LEN) * width / RECT_LEN);
    res.bottom = (int) ((bottom + HALF_RECT_LEN) * height / RECT_LEN);
    return res;
}

From source file:com.truebanana.bitmap.BitmapUtils.java

public static BitmapFactory.Options getBounds(InputStream inputStream, BitmapFactory.Options options) {
    options.inJustDecodeBounds = true;//from w w w  .java  2  s.c  o m
    BitmapFactory.decodeStream(inputStream, new Rect(), options);
    return options;
}

From source file:at.linuxtage.companion.widgets.ScrimInsetsFrameLayout.java

public ScrimInsetsFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ScrimInsetsFrameLayout, defStyleAttr,
            R.style.Widget_Design_ScrimInsetsFrameLayout);
    mInsetForeground = a.getDrawable(R.styleable.ScrimInsetsFrameLayout_insetForeground);
    a.recycle();//from   ww  w .  j a va2  s. c  o  m
    setWillNotDraw(true); // No need to draw until the insets are adjusted
    ViewCompat.setOnApplyWindowInsetsListener(this, new android.support.v4.view.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
            if (null == mInsets) {
                mInsets = new Rect();
            }
            mInsets.set(insets.getSystemWindowInsetLeft(), insets.getSystemWindowInsetTop(),
                    insets.getSystemWindowInsetRight(), insets.getSystemWindowInsetBottom());
            setWillNotDraw(mInsets.isEmpty() || mInsetForeground == null);
            ViewCompat.postInvalidateOnAnimation(ScrimInsetsFrameLayout.this);
            return insets.consumeSystemWindowInsets();
        }
    });
}

From source file:android.support.design.internal.ScrimInsetsRelativeLayout.java

public ScrimInsetsRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ScrimInsetsFrameLayout, defStyleAttr,
            R.style.Widget_Design_ScrimInsetsFrameLayout);
    mInsetForeground = a.getDrawable(R.styleable.ScrimInsetsFrameLayout_insetForeground);
    a.recycle();// ww  w . j  av  a2 s .co  m
    setWillNotDraw(true); // No need to draw until the insets are adjusted

    ViewCompat.setOnApplyWindowInsetsListener(this, new android.support.v4.view.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
            if (null == mInsets) {
                mInsets = new Rect();
            }
            mInsets.set(insets.getSystemWindowInsetLeft(), insets.getSystemWindowInsetTop(),
                    insets.getSystemWindowInsetRight(), insets.getSystemWindowInsetBottom());
            setWillNotDraw(mInsets.isEmpty() || mInsetForeground == null);
            ViewCompat.postInvalidateOnAnimation(ScrimInsetsRelativeLayout.this);
            return insets.consumeSystemWindowInsets();
        }
    });
}

From source file:android.support.designox.internal.ScrimInsetsFrameLayout.java

public ScrimInsetsFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ScrimInsetsFrameLayout, defStyleAttr,
            R.style.Widget_Design_ScrimInsetsFrameLayout);
    mInsetForeground = a.getDrawable(R.styleable.ScrimInsetsFrameLayout_insetForeground);
    a.recycle();/*  w  ww .j  a  v  a 2s .c o m*/
    setWillNotDraw(true); // No need to draw until the insets are adjusted

    ViewCompat.setOnApplyWindowInsetsListener(this,
            new android.support.v4ox.view.OnApplyWindowInsetsListener() {
                @Override
                public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
                    if (null == mInsets) {
                        mInsets = new Rect();
                    }
                    mInsets.set(insets.getSystemWindowInsetLeft(), insets.getSystemWindowInsetTop(),
                            insets.getSystemWindowInsetRight(), insets.getSystemWindowInsetBottom());
                    onInsetsChanged(mInsets);
                    setWillNotDraw(mInsets.isEmpty() || mInsetForeground == null);
                    ViewCompat.postInvalidateOnAnimation(ScrimInsetsFrameLayout.this);
                    return insets.consumeSystemWindowInsets();
                }
            });
}

From source file:Main.java

/**
 * Computes a Camera.Area corresponding to the new focus area to focus the camera on. This is
 * done by deriving a square around the center of a MotionEvent pointer (with side length equal
 * to FOCUS_AREA_MOTION_EVENT_EDGE_LENGTH), then transforming this rectangle's/square's
 * coordinates into the (-1000, 1000) coordinate system used for camera focus areas.
 *
 * Also note that we operate on RectF instances for the most part, to avoid any integer
 * division rounding errors going forward. We only round at the very end for playing into
 * the final focus areas list./*from   w w w .ja  v  a  2s .  c o m*/
 *
 * @throws RuntimeException if unable to compute valid intersection between MotionEvent region
 * and SurfaceTexture region.
 */
protected static Camera.Area computeFocusAreaFromMotionEvent(final MotionEvent event,
        final int surfaceTextureWidth, final int surfaceTextureHeight) {
    // Get position of first touch pointer.
    final int pointerId = event.getPointerId(0);
    final int pointerIndex = event.findPointerIndex(pointerId);
    final float centerX = event.getX(pointerIndex);
    final float centerY = event.getY(pointerIndex);

    // Build event rect. Note that coordinates increase right and down, such that left <= right
    // and top <= bottom.
    final RectF eventRect = new RectF(centerX - FOCUS_AREA_MOTION_EVENT_EDGE_LENGTH, // left
            centerY - FOCUS_AREA_MOTION_EVENT_EDGE_LENGTH, // top
            centerX + FOCUS_AREA_MOTION_EVENT_EDGE_LENGTH, // right
            centerY + FOCUS_AREA_MOTION_EVENT_EDGE_LENGTH // bottom
    );

    // Intersect this rect with the rect corresponding to the full area of the parent surface
    // texture, making sure we are not placing any amount of the eventRect outside the parent
    // surface's area.
    final RectF surfaceTextureRect = new RectF((float) 0, // left
            (float) 0, // top
            (float) surfaceTextureWidth, // right
            (float) surfaceTextureHeight // bottom
    );
    final boolean intersectSuccess = eventRect.intersect(surfaceTextureRect);
    if (!intersectSuccess) {
        throw new RuntimeException("MotionEvent rect does not intersect with SurfaceTexture rect; unable to "
                + "compute focus area");
    }

    // Transform into (-1000, 1000) focus area coordinate system. See
    // https://developer.android.com/reference/android/hardware/Camera.Area.html.
    // Note that if this is ever changed to a Rect instead of RectF, be cautious of integer
    // division rounding!
    final RectF focusAreaRect = new RectF((eventRect.left / surfaceTextureWidth) * 2000 - 1000, // left
            (eventRect.top / surfaceTextureHeight) * 2000 - 1000, // top
            (eventRect.right / surfaceTextureWidth) * 2000 - 1000, // right
            (eventRect.bottom / surfaceTextureHeight) * 2000 - 1000 // bottom
    );
    Rect focusAreaRectRounded = new Rect();
    focusAreaRect.round(focusAreaRectRounded);
    return new Camera.Area(focusAreaRectRounded, FOCUS_AREA_WEIGHT);
}