Java Code Examples for android.graphics.Rect#width()

The following examples show how to use android.graphics.Rect#width() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PlaceholderDrawable.java    From AppCompat-Extension-Library with Apache License 2.0 6 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    Rect bounds = getBounds();
    // draw placeholder circle
    float radius = Math.min(bounds.width(), bounds.height()) / 2f;
    float xPos = bounds.left + (bounds.width() / 2f);
    canvas.drawCircle(xPos, bounds.top + bounds.height() / 2f, radius, mPlaceholderCirclePaint);
    if (mPlaceholderImage == null) {
        // draw placeholder text
        float yPos = (bounds.top + (bounds.height() / 2f) -
                ((mPlaceholderTextPaint.descent() + mPlaceholderTextPaint.ascent()) / 2f));
        canvas.drawText(mPlaceholderText, xPos, yPos, mPlaceholderTextPaint);
    } else {
        // draw placeholder image
        int horizontalPadding = (bounds.width() - mPlaceholderImageSize) / 2;
        int verticalPadding = (bounds.height() - mPlaceholderImageSize) / 2;
        mPlaceholderImage.setBounds(bounds.left + horizontalPadding, bounds.top + verticalPadding,
                bounds.right - horizontalPadding, bounds.bottom - verticalPadding);
        mPlaceholderImage.draw(canvas);
    }
}
 
Example 2
Source File: CameraManager.java    From vinci with Apache License 2.0 6 votes vote down vote up
/**
 * A factory method to build the appropriate LuminanceSource object based on the format
 * of the preview buffers, as described by Camera.Parameters.
 *
 * @param data   A preview frame.
 * @param width  The width of the image.
 * @param height The height of the image.
 * @return A PlanarYUVLuminanceSource instance.
 */
public PlanarYUVLuminanceSource buildLuminanceSource(byte[] data, int width, int height) {
    Rect rect = getFramingRectInPreview();
    int previewFormat = configManager.getPreviewFormat();
    String previewFormatString = configManager.getPreviewFormatString();
    switch (previewFormat) {
        // This is the standard Android format which all devices are REQUIRED to support.
        // In theory, it's the only one we should ever care about.
        case PixelFormat.YCbCr_420_SP:
            // This format has never been seen in the wild, but is compatible as we only care
            // about the Y channel, so allow it.
        case PixelFormat.YCbCr_422_SP:
            return new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top,
                    rect.width(), rect.height());
        default:
            // The Samsung Moment incorrectly uses this variant instead of the 'sp' version.
            // Fortunately, it too has all the Y data up front, so we can read it.
            if ("yuv420p".equals(previewFormatString)) {
                return new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top,
                        rect.width(), rect.height());
            }
    }
    throw new IllegalArgumentException("Unsupported picture format: " +
            previewFormat + '/' + previewFormatString);
}
 
Example 3
Source File: BarView.java    From SimplePomodoro-android with MIT License 6 votes vote down vote up
/**
     * dataList will be reset when called is method.
     * @param bottomStringList The String ArrayList in the bottom.
     */
    public void setBottomTextList(ArrayList<String> bottomStringList){
//        this.dataList = null;
        this.bottomTextList = bottomStringList;
        Rect r = new Rect();
        bottomTextDescent = 0;
        barWidth = MINI_BAR_WIDTH;
        for(String s:bottomTextList){
            textPaint.getTextBounds(s,0,s.length(),r);
            if(bottomTextHeight<r.height()){
                bottomTextHeight = r.height();
            }
            if(autoSetWidth&&(barWidth<r.width())){
                barWidth = r.width();
            }
            if(bottomTextDescent<(Math.abs(r.bottom))){
                bottomTextDescent = Math.abs(r.bottom);
            }
        }
        setMinimumWidth(2);
        postInvalidate();
    }
 
Example 4
Source File: ScalingUtils.java    From fresco with MIT License 6 votes vote down vote up
@Override
public void getTransformImpl(
    Matrix outTransform,
    Rect parentRect,
    int childWidth,
    int childHeight,
    float focusX,
    float focusY,
    float scaleX,
    float scaleY) {
  float scale = Math.min(scaleX, scaleY);
  float dx = parentRect.left + (parentRect.width() - childWidth * scale) * 0.5f;
  float dy = parentRect.top + (parentRect.height() - childHeight * scale) * 0.5f;
  outTransform.setScale(scale, scale);
  outTransform.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
}
 
Example 5
Source File: HSVColorPickerDialog.java    From padland with Apache License 2.0 6 votes vote down vote up
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    rect = new Rect( innerPadding, innerPadding, w - innerPadding, h - innerPadding );
    bitmap = Bitmap.createBitmap( w - 2 * innerPadding, h - 2 * innerPadding, Config.ARGB_8888 );

    fullCircleRadius = Math.min( rect.width(), rect.height() ) / 2;
    innerCircleRadius = fullCircleRadius * ( 1 - FADE_OUT_FRACTION );

    scaledWidth = rect.width() / scale;
    scaledHeight = rect.height() / scale;
    scaledFullCircleRadius = Math.min( scaledWidth, scaledHeight ) / 2;
    scaledInnerCircleRadius = scaledFullCircleRadius * ( 1 - FADE_OUT_FRACTION );
    scaledFadeOutSize = scaledFullCircleRadius - scaledInnerCircleRadius;
    scaledPixels = new int[ scaledWidth * scaledHeight ];
    pixels = new int[ rect.width() * rect.height() ];

    createBitmap();
}
 
Example 6
Source File: ScalingUtils.java    From fresco with MIT License 6 votes vote down vote up
@Override
public void getTransformImpl(
    Matrix outTransform,
    Rect parentRect,
    int childWidth,
    int childHeight,
    float focusX,
    float focusY,
    float scaleX,
    float scaleY) {
  float scale = Math.min(Math.min(scaleX, scaleY), 1.0f);
  float dx = parentRect.left + (parentRect.width() - childWidth * scale) * 0.5f;
  float dy = parentRect.top + (parentRect.height() - childHeight * scale) * 0.5f;
  outTransform.setScale(scale, scale);
  outTransform.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
}
 
Example 7
Source File: HighlightView.java    From MyBlogDemo with Apache License 2.0 6 votes vote down vote up
void handleMotion(int edge, float dx, float dy) {
    Rect r = computeLayout();
    if (edge == MOVE) {
        // Convert to image space before sending to moveBy()
        moveBy(dx * (cropRect.width() / r.width()),
                dy * (cropRect.height() / r.height()));
    } else {
        if (((GROW_LEFT_EDGE | GROW_RIGHT_EDGE) & edge) == 0) {
            dx = 0;
        }

        if (((GROW_TOP_EDGE | GROW_BOTTOM_EDGE) & edge) == 0) {
            dy = 0;
        }

        // Convert to image space before sending to growBy()
        float xDelta = dx * (cropRect.width() / r.width());
        float yDelta = dy * (cropRect.height() / r.height());
        growBy((((edge & GROW_LEFT_EDGE) != 0) ? -1 : 1) * xDelta,
                (((edge & GROW_TOP_EDGE) != 0) ? -1 : 1) * yDelta);
    }
}
 
Example 8
Source File: CropImageActivity.java    From XERUNG with Apache License 2.0 5 votes vote down vote up
private void onSaveClicked() {
    if (cropView == null || isSaving) {
        return;
    }
    isSaving = true;

    Bitmap croppedImage;
    Rect r = cropView.getScaledCropRect(sampleSize);
    int width = r.width();
    int height = r.height();

    int outWidth = width;
    int outHeight = height;
    if (maxX > 0 && maxY > 0 && (width > maxX || height > maxY)) {
        float ratio = (float) width / (float) height;
        if ((float) maxX / (float) maxY > ratio) {
            outHeight = maxY;
            outWidth = (int) ((float) maxY * ratio + .5f);
        } else {
            outWidth = maxX;
            outHeight = (int) ((float) maxX / ratio + .5f);
        }
    }

    try {
        croppedImage = decodeRegionCrop(r, outWidth, outHeight);
    } catch (IllegalArgumentException e) {
        setResultException(e);
        finish();
        return;
    }

    if (croppedImage != null) {
        imageView.setImageRotateBitmapResetBase(new RotateBitmap(croppedImage, exifRotation), true);
        imageView.center();
        imageView.highlightViews.clear();
    }
    saveImage(croppedImage);
}
 
Example 9
Source File: LineText.java    From HTextView with Apache License 2.0 5 votes vote down vote up
@Override protected void animatePrepare(CharSequence text) {

        Rect bounds = new Rect();
        mPaint.getTextBounds(mText.toString(), 0, mText.length(), bounds);
        mTextHeight = bounds.height();

        distWidth = bounds.width() + padding * 2 + xLineWidth;
        distHeight = bounds.height() + padding * 2 + xLineWidth;

        xLineLength = mHTextView.getWidth();
        yLineLength = mHTextView.getHeight();
    }
 
Example 10
Source File: DecodeHandler.java    From AirFree-Client with GNU General Public License v3.0 5 votes vote down vote up
/**
 * A factory method to build the appropriate LuminanceSource object based on
 * the format of the preview buffers, as described by Camera.Parameters.
 *
 * @param data   A preview frame.
 * @param width  The width of the image.
 * @param height The height of the image.
 * @return A PlanarYUVLuminanceSource instance.
 */
public PlanarYUVLuminanceSource buildLuminanceSource(byte[] data, int width, int height) {
    Rect rect = activity.getCropRect();
    if (rect == null) {
        return null;
    }
    // Go ahead and assume it's YUV rather than die.
    return new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top, rect.width(), rect
            .height(), false);
}
 
Example 11
Source File: AutoFocusHelper.java    From Camera2 with Apache License 2.0 5 votes vote down vote up
/**
 * Compute 3A regions for a sensor-referenced touch coordinate.
 * Returns a MeteringRectangle[] with length 1.
 *
 * @param nx                x coordinate of the touch point, in normalized portrait coordinates.
 * @param ny                y coordinate of the touch point, in normalized portrait coordinates.
 * @param fraction          Fraction in [0,1]. Multiplied by min(cropRegion.width(), cropRegion.height())
 *                          to determine the side length of the square MeteringRectangle.
 * @param cropRegion        Crop region of the image.
 * @param sensorOrientation sensor orientation as defined by
 *                          CameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION).
 */
private static MeteringRectangle[] regionsForNormalizedCoord(float nx, float ny,
                                                             float fraction, final Rect cropRegion, int
                                                                     sensorOrientation)
{
    // Compute half side length in pixels.
    int minCropEdge = Math.min(cropRegion.width(), cropRegion.height());
    int halfSideLength = (int) (0.5f * fraction * minCropEdge);

    // Compute the output MeteringRectangle in sensor space.
    // nx, ny is normalized to the screen.
    // Crop region itself is specified in sensor coordinates.

    // Normalized coordinates, now rotated into sensor space.
    PointF nsc = CameraUtil.normalizedSensorCoordsForNormalizedDisplayCoords(
            nx, ny, sensorOrientation);

    int xCenterSensor = (int) (cropRegion.left + nsc.x * cropRegion.width());
    int yCenterSensor = (int) (cropRegion.top + nsc.y * cropRegion.height());

    Rect meteringRegion = new Rect(xCenterSensor - halfSideLength,
            yCenterSensor - halfSideLength,
            xCenterSensor + halfSideLength,
            yCenterSensor + halfSideLength);

    // Clamp meteringRegion to cropRegion.
    meteringRegion.left = CameraUtil.clamp(meteringRegion.left, cropRegion.left, cropRegion.right);
    meteringRegion.top = CameraUtil.clamp(meteringRegion.top, cropRegion.top, cropRegion.bottom);
    meteringRegion.right = CameraUtil.clamp(meteringRegion.right, cropRegion.left, cropRegion.right);
    meteringRegion.bottom = CameraUtil.clamp(meteringRegion.bottom, cropRegion.top, cropRegion.bottom);

    return new MeteringRectangle[]{new MeteringRectangle(meteringRegion, CAMERA2_REGION_WEIGHT)};
}
 
Example 12
Source File: BitmapUtils.java    From timecat with Apache License 2.0 5 votes vote down vote up
/**
 * Fix the given rectangle if it doesn't confirm to aspect ration rule.<br>
 * Make sure that width and height are equal if 1:1 fixed aspect ratio is requested.
 */
private static void fixRectForAspectRatio(Rect rect, int aspectRatioX, int aspectRatioY) {
    if (aspectRatioX == aspectRatioY && rect.width() != rect.height()) {
        if (rect.height() > rect.width()) {
            rect.bottom -= rect.height() - rect.width();
        } else {
            rect.right -= rect.width() - rect.height();
        }
    }
}
 
Example 13
Source File: ShadowUtils.java    From AndroidUtilCode with Apache License 2.0 5 votes vote down vote up
private void buildComponents(Rect bounds) {
    // Card is offset mShadowMultiplier * maxShadowSize to account for the shadow shift.
    // We could have different top-bottom offsets to avoid extra gap above but in that case
    // center aligning Views inside the CardView would be problematic.
    if (isCircle) {
        mCornerRadius = bounds.width() / 2;
    }
    final float verticalOffset = mRawMaxShadowSize * mShadowMultiplier;
    mContentBounds.set(bounds.left + mRawMaxShadowSize, bounds.top + verticalOffset,
            bounds.right - mRawMaxShadowSize, bounds.bottom - verticalOffset);

    getWrappedDrawable().setBounds((int) mContentBounds.left, (int) mContentBounds.top,
            (int) mContentBounds.right, (int) mContentBounds.bottom);
    buildShadowCorners();
}
 
Example 14
Source File: CircleGraphView.java    From HzGrapher with Apache License 2.0 5 votes vote down vote up
/**
 * calc rotated point
 * @param dot
 * @param radAngle
 */
private PointF getRotatePoint(PointF dot, float radAngle, Paint textPaint, String rateText) {
	PointF rotateDot = new PointF();
	Rect rect = new Rect();
	textPaint.getTextBounds(rateText, 0, rateText.length(), rect);
	rotateDot.x = (float) (chartCenter.x + radius/1.4 *Math.cos(radAngle) - rect.width()/2);
	rotateDot.y = (float) (chartCenter.y + radius/1.4 *Math.sin(radAngle) + rect.height()/2);
	return rotateDot;
}
 
Example 15
Source File: ContactChipDrawable.java    From material with Apache License 2.0 5 votes vote down vote up
private void updateMatrix(){
    if(mBitmap == null)
        return;

    Rect bounds = getBounds();
    if(bounds.width() == 0 || bounds.height() == 0)
        return;

    mMatrix.reset();
    float scale = bounds.height() / (float)Math.min(mBitmap.getWidth(), mBitmap.getHeight());
    mMatrix.setScale(scale, scale, 0, 0);
    mMatrix.postTranslate((bounds.height()  - mBitmap.getWidth() * scale) / 2, (bounds.height() - mBitmap.getHeight() * scale) / 2);

    mBitmapShader.setLocalMatrix(mMatrix);
}
 
Example 16
Source File: DiscreteSeekBar.java    From sealrtc-android with MIT License 5 votes vote down vote up
private boolean startDragging(MotionEvent ev, boolean ignoreTrackIfInScrollContainer) {
    final Rect bounds = mTempRect;
    mThumb.copyBounds(bounds);
    // Grow the current thumb rect for a bigger touch area
    bounds.inset(-mAddedTouchBounds, -mAddedTouchBounds);
    mIsDragging = (bounds.contains((int) ev.getX(), (int) ev.getY()));
    if (!mIsDragging && mAllowTrackClick && !ignoreTrackIfInScrollContainer) {
        // If the user clicked outside the thumb, we compute the current position
        // and force an immediate drag to it.
        mIsDragging = true;
        mDragOffset = (bounds.width() / 2) - mAddedTouchBounds;
        updateDragging(ev);
        // As the thumb may have moved, get the bounds again
        mThumb.copyBounds(bounds);
        bounds.inset(-mAddedTouchBounds, -mAddedTouchBounds);
    }
    if (mIsDragging) {
        setPressed(true);
        attemptClaimDrag();
        setHotspot(ev.getX(), ev.getY());
        mDragOffset = (int) (ev.getX() - bounds.left - mAddedTouchBounds);
        if (mPublicChangeListener != null) {
            mPublicChangeListener.onStartTrackingTouch(this);
        }
    }
    return mIsDragging;
}
 
Example 17
Source File: ScrollingBackgroundView.java    From ScrollingBackgroundView with Apache License 2.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    // to draw possible background, etc
    super.onDraw(canvas);

    final Drawable drawable = mDrawable;
    if (drawable == null) {
        return;
    }

    final int scrollX = mScrollX;
    final int scrollY = mScrollY;

    final int width = canvas.getWidth();
    final int height = canvas.getHeight();

    final Rect rect = drawable.getBounds();

    final int drawableWidth = rect.width();
    final int drawableHeight = rect.height();

    final int startX = start(scrollX, drawableWidth);
    final int iterationsX = iterations(width, startX, drawableWidth);

    final int startY = start(scrollY, drawableHeight);
    final int iterationsY = iterations(height, startY, drawableHeight);

    final int save = canvas.save();
    try {

        canvas.translate(startX, startY);

        for (int x = 0; x < iterationsX; x++) {
            for (int y = 0; y < iterationsY; y++) {
                drawable.draw(canvas);
                canvas.translate(.0F, drawableHeight);
            }
            canvas.translate(drawableWidth, -(drawableHeight * iterationsY));
        }

    } finally {
        canvas.restoreToCount(save);
    }
}
 
Example 18
Source File: Size.java    From Camera2 with Apache License 2.0 4 votes vote down vote up
public static Size of(Rect rectangle)
{
    return new Size(rectangle.width(), rectangle.height());
}
 
Example 19
Source File: PixelSunDrawable.java    From GeometricWeather with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void ensurePosition(Rect bounds) {
    float boundSize = Math.min(bounds.width(), bounds.height());
    radius = (float) ((Math.sin(Math.PI / 4) * boundSize / 2 + boundSize / 2) / 2 - 2);
    cx = (float) (1.0 * bounds.width() / 2 + bounds.left);
    cy = (float) (1.0 * bounds.height() / 2 + bounds.top);
}
 
Example 20
Source File: VectorDrawable.java    From Mover with Apache License 2.0 4 votes vote down vote up
@Override
public void draw(Canvas canvas) {
  final Rect bounds = getBounds();
  if (bounds.width() == 0 || bounds.height() == 0) {
    // too small to draw
    return;
  }

  final int saveCount = canvas.save();
  final boolean needMirroring = needMirroring();

  canvas.translate(bounds.left, bounds.top);
  if (needMirroring) {
    canvas.translate(bounds.width(), 0);
    canvas.scale(-1.0f, 1.0f);
  }

  // Color filters always override tint filters.
  final ColorFilter colorFilter = mColorFilter == null ? mTintFilter : mColorFilter;

  if (!mAllowCaching) {
    // AnimatedVectorDrawable
    if (!mVectorState.hasTranslucentRoot()) {
      mVectorState.mVPathRenderer.draw(
          canvas, bounds.width(), bounds.height(), colorFilter);
    } else {
      mVectorState.createCachedBitmapIfNeeded(bounds);
      mVectorState.updateCachedBitmap(bounds);
      mVectorState.drawCachedBitmapWithRootAlpha(canvas, colorFilter);
    }
  } else {
    // Static Vector Drawable case.
    mVectorState.createCachedBitmapIfNeeded(bounds);
    if (!mVectorState.canReuseCache()) {
      mVectorState.updateCachedBitmap(bounds);
      mVectorState.updateCacheStates();
    }
    mVectorState.drawCachedBitmapWithRootAlpha(canvas, colorFilter);
  }

  canvas.restoreToCount(saveCount);
}