Example usage for android.graphics RectF RectF

List of usage examples for android.graphics RectF RectF

Introduction

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

Prototype

public RectF(float left, float top, float right, float bottom) 

Source Link

Document

Create a new rectangle with the specified coordinates.

Usage

From source file:com.todddavies.components.progressbar.ProgressWheel.java

/**
 * Set the bounds of the component//from  w w w .  j  a  v  a2  s .c o m
 */
private void setupBounds() {
    // Width should equal to Height, find the min value to steup the circle
    int minValue = Math.min(layout_width, layout_height);

    // Calc the Offset if needed
    int xOffset = layout_width - minValue;
    int yOffset = layout_height - minValue;

    // Add the offset
    paddingTop = this.getPaddingTop() + (yOffset / 2);
    paddingBottom = this.getPaddingBottom() + (yOffset / 2);
    paddingLeft = this.getPaddingLeft() + (xOffset / 2);
    paddingRight = this.getPaddingRight() + (xOffset / 2);

    int width = getWidth(); //this.getLayoutParams().width;
    int height = getHeight(); //this.getLayoutParams().height;

    log("layout_width is " + layout_width + " layout_height is " + layout_height + " paddingTop is "
            + paddingTop + " paddingBottom is " + paddingBottom + " paddingLeft is " + paddingLeft
            + " paddingRight is " + paddingRight + " width is " + width + " height is " + height

    );

    rectBounds = new RectF(paddingLeft, paddingTop, width - paddingRight, height - paddingBottom);

    circleBounds = new RectF(paddingLeft + barWidth, paddingTop + barWidth, width - paddingRight - barWidth,
            height - paddingBottom - barWidth);
    circleInnerContour = new RectF(circleBounds.left + (rimWidth / 2.0f) + (contourSize / 2.0f),
            circleBounds.top + (rimWidth / 2.0f) + (contourSize / 2.0f),
            circleBounds.right - (rimWidth / 2.0f) - (contourSize / 2.0f),
            circleBounds.bottom - (rimWidth / 2.0f) - (contourSize / 2.0f));
    circleOuterContour = new RectF(circleBounds.left - (rimWidth / 2.0f) - (contourSize / 2.0f),
            circleBounds.top - (rimWidth / 2.0f) - (contourSize / 2.0f),
            circleBounds.right + (rimWidth / 2.0f) + (contourSize / 2.0f),
            circleBounds.bottom + (rimWidth / 2.0f) + (contourSize / 2.0f));

    fullRadius = (width - paddingRight - barWidth) / 2;
    circleRadius = (fullRadius - barWidth) + 1;
}

From source file:com.github.jokar.rxupload.widget.ProgressDownloadView.java

private void makePathBubble() {

    if (mPathBubble == null) {
        mPathBubble = new Path();
    }//  w ww .j a va 2  s. c o m

    int width = mBubbleWidth;
    int height = mBubbleHeight;
    int arrowWidth = width / 3;

    //Rect r = new Rect(Math.max(getPaddingLeft()-width/2-arrowWidth/4, mProgress*mWidth/100-width/2-arrowWidth/4), mHeight/2-height + calculatedeltaY(), Math.max(getPaddingLeft()+width/2-arrowWidth/4, mProgress*mWidth/100+width/2-arrowWidth/4), mHeight/2+height-height + calculatedeltaY());
    Rect r = new Rect((int) (Math.max(getPaddingLeft() - width / 2, mProgress * mWidth / 100 - width / 2)),
            (int) (mHeight / 2 - height + calculateDeltaY()),
            (int) (Math.max(getPaddingLeft() + width / 2, mProgress * mWidth / 100 + width / 2)),
            (int) (mHeight / 2 + height - height + calculateDeltaY()));
    int arrowHeight = (int) (arrowWidth / 1.5f);
    int radius = 8;

    Path path = new Path();

    // Down arrow
    path.moveTo(r.left + r.width() / 2 - arrowWidth / 2, r.top + r.height() - arrowHeight);
    bubbleAnchorX = r.left + r.width() / 2;
    bubbleAnchorY = r.top + r.height();
    path.lineTo(bubbleAnchorX, bubbleAnchorY);
    path.lineTo(r.left + r.width() / 2 + arrowWidth / 2, r.top + r.height() - arrowHeight);

    // Go to bottom-right
    path.lineTo(r.left + r.width() - radius, r.top + r.height() - arrowHeight);

    // Bottom-right arc
    path.arcTo(new RectF(r.left + r.width() - 2 * radius, r.top + r.height() - arrowHeight - 2 * radius,
            r.left + r.width(), r.top + r.height() - arrowHeight), 90, -90);

    // Go to upper-right
    path.lineTo(r.left + r.width(), r.top + arrowHeight);

    // Upper-right arc
    path.arcTo(new RectF(r.left + r.width() - 2 * radius, r.top, r.right, r.top + 2 * radius), 0, -90);

    // Go to upper-left
    path.lineTo(r.left + radius, r.top);

    // Upper-left arc
    path.arcTo(new RectF(r.left, r.top, r.left + 2 * radius, r.top + 2 * radius), 270, -90);

    // Go to bottom-left
    path.lineTo(r.left, r.top + r.height() - arrowHeight - radius);

    // Bottom-left arc
    path.arcTo(new RectF(r.left, r.top + r.height() - arrowHeight - 2 * radius, r.left + 2 * radius,
            r.top + r.height() - arrowHeight), 180, -90);

    path.close();

    mPathBubble.set(path);
}

From source file:org.mozilla.gecko.gfx.GeckoSoftwareLayerClient.java

public Rect beginDrawing(int width, int height, int tileWidth, int tileHeight, String metadata,
        boolean hasDirectTexture) {
    setHasDirectTexture(hasDirectTexture);

    // Make sure the tile-size matches. If it doesn't, we could crash trying
    // to access invalid memory.
    if (mHasDirectTexture) {
        if (tileWidth != 0 || tileHeight != 0) {
            Log.e(LOGTAG, "Aborting draw, incorrect tile size of " + tileWidth + "x" + tileHeight);
            return null;
        }/*  w w w  .  j av  a 2s.  c  om*/
    } else {
        if (tileWidth != TILE_SIZE.width || tileHeight != TILE_SIZE.height) {
            Log.e(LOGTAG, "Aborting draw, incorrect tile size of " + tileWidth + "x" + tileHeight);
            return null;
        }
    }

    LayerController controller = getLayerController();

    try {
        JSONObject viewportObject = new JSONObject(metadata);
        mNewGeckoViewport = new ViewportMetrics(viewportObject);

        // Update the background color, if it's present.
        String backgroundColorString = viewportObject.optString("backgroundColor");
        if (backgroundColorString != null) {
            controller.setCheckerboardColor(parseColorFromGecko(backgroundColorString));
        }
    } catch (JSONException e) {
        Log.e(LOGTAG, "Aborting draw, bad viewport description: " + metadata);
        return null;
    }

    // Make sure we don't spend time painting areas we aren't interested in.
    // Only do this if the Gecko viewport isn't going to override our viewport.
    Rect bufferRect = new Rect(0, 0, width, height);

    if (!mUpdateViewportOnEndDraw) {
        // First, find out our ideal displayport. We do this by taking the
        // clamped viewport origin and taking away the optimum viewport offset.
        // This would be what we would send to Gecko if adjustViewport were
        // called now.
        ViewportMetrics currentMetrics = controller.getViewportMetrics();
        PointF currentBestOrigin = RectUtils.getOrigin(currentMetrics.getClampedViewport());
        PointF viewportOffset = currentMetrics.getOptimumViewportOffset(new IntSize(width, height));
        currentBestOrigin.offset(-viewportOffset.x, -viewportOffset.y);

        Rect currentRect = RectUtils.round(new RectF(currentBestOrigin.x, currentBestOrigin.y,
                currentBestOrigin.x + width, currentBestOrigin.y + height));

        // Second, store Gecko's displayport.
        PointF currentOrigin = mNewGeckoViewport.getDisplayportOrigin();
        bufferRect = RectUtils.round(
                new RectF(currentOrigin.x, currentOrigin.y, currentOrigin.x + width, currentOrigin.y + height));

        // Take the intersection of the two as the area we're interested in rendering.
        if (!bufferRect.intersect(currentRect)) {
            // If there's no intersection, we have no need to render anything,
            // but make sure to update the viewport size.
            beginTransaction(mTileLayer);
            try {
                updateViewport(true);
            } finally {
                endTransaction(mTileLayer);
            }
            return null;
        }
        bufferRect.offset(Math.round(-currentOrigin.x), Math.round(-currentOrigin.y));
    }

    beginTransaction(mTileLayer);

    // Synchronise the buffer size with Gecko.
    if (mBufferSize.width != width || mBufferSize.height != height) {
        mBufferSize = new IntSize(width, height);

        // Reallocate the buffer if necessary
        if (mTileLayer instanceof MultiTileLayer) {
            int bpp = CairoUtils.bitsPerPixelForCairoFormat(mFormat) / 8;
            int size = mBufferSize.getArea() * bpp;
            if (mBuffer == null || mBuffer.capacity() != size) {
                // Free the old buffer
                if (mBuffer != null) {
                    GeckoAppShell.freeDirectBuffer(mBuffer);
                    mBuffer = null;
                }

                mBuffer = GeckoAppShell.allocateDirectBuffer(size);
            }
        }
    }

    return bufferRect;
}

From source file:android.support.design.widget.ShadowDrawableWrapper.java

private void buildShadowCorners() {
    RectF innerBounds = new RectF(-mCornerRadius, -mCornerRadius, mCornerRadius, mCornerRadius);
    RectF outerBounds = new RectF(innerBounds);
    outerBounds.inset(-mShadowSize, -mShadowSize);

    if (mCornerShadowPath == null) {
        mCornerShadowPath = new Path();
    } else {/* w  w w .j av a 2s  . co m*/
        mCornerShadowPath.reset();
    }
    mCornerShadowPath.setFillType(Path.FillType.EVEN_ODD);
    mCornerShadowPath.moveTo(-mCornerRadius, 0);
    mCornerShadowPath.rLineTo(-mShadowSize, 0);
    // outer arc
    mCornerShadowPath.arcTo(outerBounds, 180f, 90f, false);
    // inner arc
    mCornerShadowPath.arcTo(innerBounds, 270f, -90f, false);
    mCornerShadowPath.close();

    float shadowRadius = -outerBounds.top;
    if (shadowRadius > 0f) {
        float startRatio = mCornerRadius / shadowRadius;
        float midRatio = startRatio + ((1f - startRatio) / 2f);
        mCornerShadowPaint.setShader(new RadialGradient(0, 0, shadowRadius,
                new int[] { 0, mShadowStartColor, mShadowMiddleColor, mShadowEndColor },
                new float[] { 0f, startRatio, midRatio, 1f }, Shader.TileMode.CLAMP));
    }

    // we offset the content shadowSize/2 pixels up to make it more realistic.
    // this is why edge shadow shader has some extra space
    // When drawing bottom edge shadow, we use that extra space.
    mEdgeShadowPaint.setShader(new LinearGradient(0, innerBounds.top, 0, outerBounds.top,
            new int[] { mShadowStartColor, mShadowMiddleColor, mShadowEndColor }, new float[] { 0f, .5f, 1f },
            Shader.TileMode.CLAMP));
    mEdgeShadowPaint.setAntiAlias(false);
}

From source file:de.hs_bremen.aurora_hunter.ui.views.PredictionGraphView.java

public void onDraw(Canvas canvas) {
    if (mPoints.size() == 0) {
        return;//from   ww w. j  av  a 2 s .c  o  m
    }
    int yOffsetForLabels = 0;

    if (mMax * mScaleFactor > 0.8) {
        yOffsetForLabels = 15;
    }
    Path path = new Path();
    int height = canvas.getHeight();
    int width = canvas.getWidth();

    for (Point point : mPoints) {
        point.x = point.percentageX * width + 60;
        // Log.i("mScaleFactor", " : " + mScaleFactor);
        //Log.i("percent", " : " + percent);

        point.y = (float) ((1 - point.percentageY * mScaleFactor) * height) + yOffsetForLabels * 3;
    }

    if (mPoints.size() > 1) {
        //calcuate x/y based on size of canvas
        for (int i = 0; i < mPoints.size(); i++) {
            if (i >= 0) {
                Point point = mPoints.get(i);
                //  Log.i("dx",point.x + " - " + point.y );
                if (i == 0) {
                    Point next = mPoints.get(i + 1);
                    point.dx = ((next.x - point.x) / 5);
                    point.dy = ((next.y - point.y) / 5);
                } else if (i == mPoints.size() - 1) {
                    Point prev = mPoints.get(i - 1);
                    point.dx = ((point.x - prev.x) / 5);
                    point.dy = ((point.y - prev.y) / 5);
                } else {
                    Point next = mPoints.get(i + 1);
                    Point prev = mPoints.get(i - 1);
                    point.dx = ((next.x - prev.x) / 5);
                    point.dy = ((next.y - prev.y) / 5);
                }
            }
        }
    }

    if (mPoints.size() > 0) {
        path.moveTo(0, (float) (canvas.getHeight() * 0.8));
        path.lineTo(0, mPoints.get(0).y);

    }
    boolean first = true;

    for (int i = 0; i < mPoints.size(); i++) {
        Point point = mPoints.get(i);
        if (first) {
            first = false;
            path.cubicTo(point.x - point.x * 2, point.y - point.y / 5, point.x - point.dx, point.y - point.dy,
                    point.x, point.y);
        } else {
            Point prev = mPoints.get(i - 1);
            //  Log.i("Draw", point.dx  + " " + point.dy);
            path.cubicTo(prev.x + prev.dx, prev.y + prev.dy, point.x - point.dx, point.y - point.dy, point.x,
                    point.y);
        }
    }

    if (mPoints.size() > 0) {
        path.lineTo(width, mPoints.get(mPoints.size() - 1).y);
    }
    path.lineTo(width, height);
    path.lineTo(0, height);

    canvas.drawPath(path, mGraphPaint);

    int detlaY = 30;

    for (Point p : mPoints) {

        int val = (int) Math.round(p.percentageY * 100);
        //if last element

        if (mPoints.indexOf(p) == mPoints.size() - 1) {
            //Log.i("last", p.toString());
            if (val == 0 || p.y > getHeight()) {
                canvas.drawText(val + "%", p.x - 150, getHeight() - detlaY, mTextPaint);
            } else {
                canvas.drawText(val + "%", p.x - 150, p.y - detlaY + yOffsetForLabels, mTextPaint);
            }

        } else {
            if (val == 0 || p.y > getHeight()) {
                canvas.drawText(val + "%", p.x - 20, getHeight() - detlaY, mTextPaint);
            } else {
                canvas.drawText(val + "%", p.x - 20, p.y - detlaY + yOffsetForLabels, mTextPaint);
            }

        }
        //Log.i("point", p.toString());
    }
    // Log.i("Lenght", mPoints.size() + " ");

    float levelStart = (float) (canvas.getHeight() - (mNotificationLevel * canvas.getHeight()));
    float lineHeight = DpToPixelUtil.convertDpToPixel(2, getContext());

    if (mViewMode == PredictionFragment.NOTIFICATION_MODE.SET_NOTIFICATION_LEVEL) {
        mNotificationLevelPaint.setAlpha(150);
        float leftOffset = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            float arcHeight = DpToPixelUtil.convertDpToPixel(30, getContext());
            leftOffset = arcHeight;
            RectF rectF = new RectF(getWidth() - arcHeight, levelStart - arcHeight / 2, getWidth(),
                    levelStart + arcHeight / 2);
            canvas.drawArc(rectF, 0, 360, true, mNotificationLevelPaint);

        }
        canvas.drawRect(0, levelStart, canvas.getWidth() - leftOffset, levelStart + lineHeight,
                mNotificationLevelPaint);

        mNotificationLevelPaint.setAlpha(20);
        canvas.drawRect(0, levelStart + lineHeight, canvas.getWidth(), canvas.getHeight(),
                mNotificationLevelPaint);
        String text = Math.round(mNotificationLevel * 100) + "%";
        canvas.drawText(text, DpToPixelUtil.convertDpToPixel(10, getContext()),
                levelStart + DpToPixelUtil.convertDpToPixel(25, getContext()), mNotificationLevelTextPaint);
    } else {
        mNotificationLevelPaint.setAlpha(30);
        canvas.drawRect(0, levelStart, canvas.getWidth(), levelStart + lineHeight, mNotificationLevelPaint);
    }

}

From source file:com.example.android.animationsdemo.CameraActivity.java

/**
 * Convert touch position x:y to {@link android.hardware.Camera.Area} position -1000:-1000 to 1000:1000.
 *///from ww  w  .  j  ava2  s  .c  o  m
private Rect calculateTapArea(float x, float y) {

    // define focus area (36dp)
    Display display = ((WindowManager) getSystemService(Activity.WINDOW_SERVICE)).getDefaultDisplay();
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    int areaSize = Float.valueOf(36 * metrics.density).intValue();
    DKLog.d(TAG, String.format("x=%f, y=%f, areaSize=%d", x, y, areaSize));

    int width = mPreview.getWidth();
    int height = mPreview.getHeight();

    // Convert touch area to new area -1000:-1000 to 1000:1000.
    float left = ((x - areaSize) / width) * 2000 - 1000;
    float top = ((y - areaSize) / height) * 2000 - 1000;
    float right = ((x + areaSize) / width) * 2000 - 1000;
    float bottom = ((y + areaSize) / height) * 2000 - 1000;

    // adjust boundary
    if (left < -1000) {
        right += ((-1000) - left);
        left = (-1000);
    }
    if (top < -1000) {
        bottom += ((-1000) - top);
        top = (-1000);
    }
    if (right > 1000) {
        left -= (right - 1000);
        right = 1000;
    }
    if (bottom > 1000) {
        top -= (bottom - 1000);
        bottom = 1000;
    }

    // rotate matrix if portrait
    RectF rectF = new RectF(left, top, right, bottom);
    Matrix matrix = new Matrix();
    int degree = (display.getRotation() == Surface.ROTATION_0) ? (-90) : (0);
    matrix.setRotate(degree);
    matrix.mapRect(rectF);
    return new Rect(Math.round(rectF.left), Math.round(rectF.top), Math.round(rectF.right),
            Math.round(rectF.bottom));
}

From source file:net.networksaremadeofstring.rhybudd.RhybuddDock.java

private void DrawDevices() {
    Bitmap charty = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
    Canvas DeviceCanvas = new Canvas(charty);
    final Paint paint = new Paint();

    paint.setStyle(Paint.Style.FILL);
    paint.setColor(getResources().getColor(R.color.DarkBlue));
    paint.setAntiAlias(true);/*from w ww. j av a2  s .  c o m*/
    DeviceCanvas.drawOval(new RectF(1, 1, 199, 199), paint);

    RadialGradient gradient = new RadialGradient(100, 50, 150, 0xFF6b7681, 0xFF000000,
            android.graphics.Shader.TileMode.CLAMP);
    paint.setDither(true);
    paint.setShader(gradient);
    DeviceCanvas.drawOval(new RectF(6, 6, 194, 194), paint);

    int Scale = 10;

    //Special Bits
    if (DeviceCount < 500)
        Scale = 750;

    if (DeviceCount < 250)
        Scale = 350;

    if (DeviceCount < 100)
        Scale = 150;

    if (DeviceCount < 50)
        Scale = 50;
    try {
        drawScale(DeviceCanvas, false, DeviceCount, Scale);
        drawGaugeTitle(DeviceCanvas, "Device Count");
        drawGaugeNeedle(DeviceCanvas, DeviceCount, Scale);
        //drawGloss(EventsCanvas);
        ((ImageView) findViewById(R.id.DeviceGauge)).setImageBitmap(charty);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.alimuzaffar.lib.pin.PinEntryEditText.java

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    mOriginalTextColors = getTextColors();
    if (mOriginalTextColors != null) {
        mLastCharPaint.setColor(mOriginalTextColors.getDefaultColor());
        mCharPaint.setColor(mOriginalTextColors.getDefaultColor());
        mSingleCharPaint.setColor(getCurrentHintTextColor());
    }/*from  ww  w . j av a2  s . c  o m*/
    int availableWidth = getWidth() - ViewCompat.getPaddingEnd(this) - ViewCompat.getPaddingStart(this);
    if (mSpace < 0) {
        mCharSize = (availableWidth / (mNumChars * 2 - 1));
    } else {
        mCharSize = (availableWidth - (mSpace * (mNumChars - 1))) / mNumChars;
    }
    mLineCoords = new RectF[(int) mNumChars];
    mCharBottom = new float[(int) mNumChars];
    int startX;
    int bottom = getHeight() - getPaddingBottom();
    int rtlFlag;
    final boolean isLayoutRtl = TextUtilsCompat
            .getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL;
    if (isLayoutRtl) {
        rtlFlag = -1;
        startX = (int) (getWidth() - ViewCompat.getPaddingStart(this) - mCharSize);
    } else {
        rtlFlag = 1;
        startX = ViewCompat.getPaddingStart(this);
    }
    for (int i = 0; i < mNumChars; i++) {
        mLineCoords[i] = new RectF(startX, bottom, startX + mCharSize, bottom);
        if (mPinBackground != null) {
            if (mIsDigitSquare) {
                mLineCoords[i].top = getPaddingTop();
                mLineCoords[i].right = startX + mLineCoords[i].height();
            } else {
                mLineCoords[i].top -= mTextHeight.height() + mTextBottomPadding * 2;
            }
        }

        if (mSpace < 0) {
            startX += rtlFlag * mCharSize * 2;
        } else {
            startX += rtlFlag * (mCharSize + mSpace);
        }
        mCharBottom[i] = mLineCoords[i].bottom - mTextBottomPadding;
    }
}

From source file:com.funzio.pure2D.atlas.JsonAtlas.java

protected AtlasFrame parseFrame(final int index, final JSONObject frameJson, final float scale)
        throws JSONException {
    final JSONObject frame = frameJson.getJSONObject("frame");
    final boolean trimmed = frameJson.getBoolean("trimmed");
    final boolean rotated = frameJson.getBoolean("rotated");

    final int left = frame.getInt("x");
    final int top = frame.getInt("y");
    final int w = frame.getInt("w");
    final int h = frame.getInt("h");
    final int right = left + (rotated ? h : w);
    final int bottom = top + (rotated ? w : h);

    final AtlasFrame atlasFrame = new AtlasFrame(this, index, frameJson.getString("filename"),
            new RectF(left * scale, top * scale, right * scale, bottom * scale));
    if (trimmed) {

        final JSONObject spriteSourceSize = frameJson.getJSONObject("spriteSourceSize");
        final int offsetX = spriteSourceSize.getInt("x");
        int offsetY = spriteSourceSize.getInt("y");
        // check axis
        if (mAxisSystem == Scene.AXIS_BOTTOM_LEFT) {
            final JSONObject sourceSize = frameJson.getJSONObject("sourceSize");
            // flip for axis
            offsetY = sourceSize.getInt("h") - (spriteSourceSize.getInt("y") + spriteSourceSize.getInt("h"));
        }/*from www .  j a v  a 2  s .c om*/

        if (offsetX != 0 || offsetY != 0) {
            atlasFrame.mOffset = new PointF(offsetX * scale, offsetY * scale);
        }
    }
    if (rotated) {
        atlasFrame.rotateCCW();
    }

    return atlasFrame;
}

From source file:com.landenlabs.all_UiDemo.frag.ImageScalesFrag.java

/**
 * Scale using Center Crop (source scaled till filling both new dimensions)
 * @param source//ww  w  .  j  a  va2  s.  c  o  m
 * @param newWidth
 * @param newHeight
 * @return Center Crop scaled image.
 */
public static Bitmap scaleCenterCrop2(Bitmap source, int newWidth, int newHeight) {
    int sourceWidth = source.getWidth();
    int sourceHeight = source.getHeight();

    // Compute the scaling factors to fit the new height and width, respectively.
    // To cover the final image, the final scaling will be the bigger
    // of these two.
    float xScale = (float) newWidth / sourceWidth;
    float yScale = (float) newHeight / sourceHeight;
    float scale = Math.max(xScale, yScale);

    // Now get the size of the source bitmap when scaled
    float scaledWidth = scale * sourceWidth;
    float scaledHeight = scale * sourceHeight;

    // Let's find out the upper left coordinates if the scaled bitmap
    // should be centered in the new size give by the parameters
    float left = (newWidth - scaledWidth) / 2;
    float top = (newHeight - scaledHeight) / 2;

    // The target rectangle for the scaled source bitmap
    RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);

    if (newWidth > 0 && newHeight > 0) {
        // Finally, we create a new bitmap of the specified size and draw our new,
        // scaled bitmap onto it.
        Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
        Canvas canvas = new Canvas(dest);
        canvas.drawBitmap(source, null, targetRect, null);
        return dest;
    }

    return null;
}