Example usage for android.graphics Rect height

List of usage examples for android.graphics Rect height

Introduction

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

Prototype

public final int height() 

Source Link

Usage

From source file:lollipop.iconics.IconicsDrawable.java

/**
 * Update the TextSize// w w  w. j  a va  2  s. co  m
 *
 * @param viewBounds
 */
private void updateTextSize(Rect viewBounds) {
    float textSize = (float) viewBounds.height() * 2;
    mIconPaint.setTextSize(textSize);

    String textValue = mIcon != null ? String.valueOf(mIcon.getCharacter()) : String.valueOf(mPlainIcon);
    mIconPaint.getTextPath(textValue, 0, textValue.length(), 0, viewBounds.height(), mPath);
    mPath.computeBounds(mPathBounds, true);

    float deltaWidth = ((float) mPaddingBounds.width() / mPathBounds.width());
    float deltaHeight = ((float) mPaddingBounds.height() / mPathBounds.height());
    float delta = (deltaWidth < deltaHeight) ? deltaWidth : deltaHeight;
    textSize *= delta;

    mIconPaint.setTextSize(textSize);

    mIconPaint.getTextPath(textValue, 0, textValue.length(), 0, viewBounds.height(), mPath);
    mPath.computeBounds(mPathBounds, true);
}

From source file:com.ruesga.rview.widget.TagEditTextView.java

private Bitmap createTagChip(Tag tag) {
    // Create the tag string (prepend/append spaces to better ux). Create a clickable
    // area for deleting the tag in non-readonly mode
    String tagText = String.format(" %s " + (mReadOnly || !isEnabled() ? "" : CHIP_REMOVE_TEXT), tag.mTag);

    // Create a new color for the tag if necessary
    if (tag.mColor == 0) {
        if (mChipBackgroundColor == 0) {
            tag.mColor = newRandomColor();
        } else {/*ww w.  j a v  a  2s  .c o m*/
            tag.mColor = mChipBackgroundColor;
        }
    }
    mChipBgPaint.setColor((isEnabled()) ? tag.mColor : Color.LTGRAY);

    // Measure the chip rect
    Rect bounds = new Rect();
    mChipFgPaint.getTextBounds("|", 0, 1, bounds);
    int minHeight = bounds.height();
    mChipFgPaint.getTextBounds(tagText, 0, tagText.length(), bounds);
    int padding = (int) ONE_PIXEL * 2;
    int w = (int) (mChipFgPaint.measureText(tagText) + (padding * 2));
    int h = Math.max(bounds.height() + (padding * 4), minHeight + (padding * 4));
    float baseline = h / 2 + bounds.height() / 2;

    // Create the bitmap
    Bitmap bitmap = Bitmap.createBitmap(w + padding, h + padding, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

    // Draw the bitmap
    canvas.drawRoundRect(new RectF(0, (padding / 2), w, h), 6, 6, mChipBgPaint);
    canvas.drawText(tagText, (padding / 2), baseline, mChipFgPaint);
    return bitmap;
}

From source file:com.dastanapps.camera2.view.Cam2AutoFitTextureView.java

protected void pinchToZoom(MotionEvent event) {
    maximumZoomLevel = mCharacteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM) * 10;
    Rect rect = mCharacteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
    float currentFingerSpacing;

    if (event.getPointerCount() > 1) {
        // Multi touch logic
        currentFingerSpacing = getFingerSpacing(event);
        if (fingerSpacing != 0) {
            if (currentFingerSpacing > fingerSpacing && maximumZoomLevel > zoomLevel) {
                zoomLevel = zoomLevel + .4;
            } else if (currentFingerSpacing < fingerSpacing && zoomLevel > 1) {
                zoomLevel = zoomLevel - .4;
            }/* w ww. ja v  a2  s  . c  o  m*/
            int minW = (int) (rect.width() / maximumZoomLevel);
            int minH = (int) (rect.height() / maximumZoomLevel);
            int difW = rect.width() - minW;
            int difH = rect.height() - minH;
            int cropW = difW / 100 * (int) zoomLevel;
            int cropH = difH / 100 * (int) zoomLevel;
            cropW -= cropW & 3;
            cropH -= cropH & 3;
            Rect zoom = new Rect(cropW, cropH, rect.width() - cropW, rect.height() - cropH);
            mPreviewBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoom);
        }
        fingerSpacing = currentFingerSpacing;
    }
}

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

private void makePathBubble() {

    if (mPathBubble == null) {
        mPathBubble = new Path();
    }/*  w  ww .j av  a  2 s .co 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:com.codetroopers.shakemytours.ui.activity.TripActivity.java

private Bitmap createMarker(int radius, int strokeWidth, String letter, int strokeColor, int backgroundColor) {
    int width = (radius * 2) + (strokeWidth * 2);
    Bitmap marker = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(marker);

    Paint strokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    strokePaint.setColor(strokeColor);//from w  w  w  .java 2  s.  c o  m
    strokePaint.setShadowLayer(strokeWidth, 1.0f, 1.0f, Color.BLACK);
    canvas.drawCircle(width / 2, width / 2, radius, strokePaint);

    Paint backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    backgroundPaint.setColor(backgroundColor);
    canvas.drawCircle(width / 2, width / 2, radius - strokeWidth, backgroundPaint);

    if (letter != null) {
        Rect result = new Rect();
        Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setTextSize(getResources().getDimensionPixelSize(R.dimen.map_marker_text_size));
        textPaint.setColor(strokeColor);
        textPaint.getTextBounds(letter, 0, letter.length(), result);
        int yOffset = result.height() / 2;

        canvas.drawText(letter, width / 2, (width / 2) + yOffset, textPaint);
    }
    return marker;
}

From source file:com.tbse.mywearapplication.WatchFaceDrawer.java

void onDraw(Context context, IWatchFaceConfig config, Canvas canvas, Rect bounds) {
    final Calendar calendar = config.getCalendar();
    final boolean isAmbient = config.isAmbient();
    final boolean isRound = config.isRound();
    final boolean useLightTheme = !isAmbient && config.isLightTheme();

    mBackgroundPaint.setColor(ContextCompat.getColor(context,
            useLightTheme ? R.color.watchface_background_light : R.color.watchface_background));

    /////////////////////////////////////////////////////////////////////
    // Draw your watch face here, using the provided canvas and bounds //
    /////////////////////////////////////////////////////////////////////

    final int width = bounds.width();
    final int height = bounds.height();

    // Find the center. Ignore the window insets so that, on round
    // watches with a "chin", the watch face is centered on the entire
    // screen, not just the usable portion.
    final float centerX = width / 2f;
    final float centerY = height / 2f;

    // Draw the background.
    if (mIsMobilePreview) {
        if (isRound) {
            canvas.drawCircle(centerX, centerY, centerX, mPreviewBorderPaint);
        } else {/*from  w w w.ja  v a  2  s  . c  o m*/
            final float radius = mPreviewSquareRadius;
            final RectF rectF = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
            canvas.drawRoundRect(rectF, radius, radius, mPreviewBorderPaint);
        }

        final float translateXY = width * 0.05f;
        canvas.translate(translateXY, translateXY);
        canvas.scale(0.9f, 0.9f);

        if (isRound) {
            canvas.drawCircle(centerX, centerY, centerX, mBackgroundPaint);
        } else {
            canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mBackgroundPaint);
        }
    } else {
        canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mBackgroundPaint);
    }

    // Draw weather icon
    if (image != null) {
        Matrix matrix = new Matrix();
        matrix.setTranslate(50, 90);
        matrix.preScale(0.2f, 0.2f);
        canvas.drawBitmap(image, matrix, null);
    }

    final float secRot = calendar.get(Calendar.SECOND) / 30f * (float) Math.PI;
    final int minutes = calendar.get(Calendar.MINUTE);
    final float minRot = minutes / 30f * (float) Math.PI;
    final float hrRot = ((calendar.get(Calendar.HOUR) + (minutes / 60f)) / 6f) * (float) Math.PI;

    final float secLength = centerX - mSecondOuterOffset;
    final float minLength = centerX - mMinuteOuterOffset;
    final float hrLength = centerX - mHourOuterOffset;

    if (!isAmbient) {
        final float secX = (float) Math.sin(secRot) * secLength;
        final float secY = (float) -Math.cos(secRot) * secLength;
        canvas.drawLine(centerX, centerY, centerX + secX, centerY + secY, mSecondHandPaint);
    }

    final float minX = (float) Math.sin(minRot) * minLength;
    final float minY = (float) -Math.cos(minRot) * minLength;
    canvas.drawLine(centerX, centerY, centerX + minX, centerY + minY, mMinuteHandPaint);

    final float hrX = (float) Math.sin(hrRot) * hrLength;
    final float hrY = (float) -Math.cos(hrRot) * hrLength;
    canvas.drawLine(centerX, centerY, centerX + hrX, centerY + hrY, mHourHandPaint);

    // Draw weather text
    canvas.drawText(day, 50, 50, isAmbient ? ambientTextPaint : textPaint);
    canvas.drawText(low, 50, 65, isAmbient ? ambientTextPaint : textPaint);
    canvas.drawText(high, 80, 65, isAmbient ? ambientTextPaint : textPaint);
    canvas.drawText(weatherDescription, 50, 80, isAmbient ? ambientTextPaint : textPaint);

}

From source file:com.mediatek.galleryfeature.stereo.fancycolor.FancyColorActivity.java

private Point calcClickPosition(int viewW, int viewH, float x, float y) {
    int thumbW = mThumbBitmapWidth;
    int thumbH = mThumbBitmapHeight;
    int gap;// ww w .  j  a va2  s .c om
    int mapX;
    int mapY;
    Rect validRange = new Rect();
    if (thumbH >= thumbW) {
        // bitmap height fulfills view height
        gap = (viewW - viewH * thumbW / thumbH) / 2;
        validRange.left = gap;
        validRange.right = viewW - gap;
        validRange.top = 0;
        validRange.bottom = viewH;
        mapX = thumbW * ((int) x - gap) / validRange.width();
        mapY = thumbH * (int) y / validRange.height();
    } else {
        // bitmap width fullfills view width
        gap = (viewH - viewW * thumbH / thumbW) / 2;
        validRange.left = 0;
        validRange.right = viewW;
        validRange.top = gap;
        validRange.bottom = viewH - gap;
        mapX = thumbW * (int) x / validRange.width();
        mapY = thumbH * ((int) y - gap) / validRange.height();
    }
    MtkLog.d(TAG,
            "<calcClickPosition> thumbW " + thumbW + ", thumbH " + thumbH + ", gap " + gap + ", rect lrtb "
                    + validRange.left + ", " + validRange.right + ", " + validRange.top + ", "
                    + validRange.bottom + ", mapX " + mapX + ", mapY " + mapY);
    if (!validRange.contains((int) x, (int) y)) {
        MtkLog.d(TAG, "<calcClickPosition> invalid click");
        return null;
    }
    return new Point(mapX, mapY);
}

From source file:com.chauthai.overscroll.BouncyAdapter.java

/**
 * Try to estimate the content size of the adapter inside the RecyclerView.
 * @return The average size of {@link BouncyConfig#viewCountEstimateSize} views in the adapter.
 *///w w  w  .j  a v  a2s . c  o  m
private int estimateContentSize() {
    int total = 0;
    int count = 0;

    for (int i = mAdapter.getItemCount() - 1; i >= 0 && count < mConfig.viewCountEstimateSize; i--) {
        View view = mLayoutManager.findViewByPosition(i + 1);

        if (view != null) {
            Rect rect = new Rect();
            mLayoutManager.getDecoratedBoundsWithMargins(view, rect);
            int itemHeight = Math.abs(directionVertical() ? rect.height() : rect.width());

            count++;
            total += itemHeight;
        }
    }

    if (count > 0) {
        double average = (double) total / count;
        return (int) (average * mAdapter.getItemCount());
    }

    return 0;
}

From source file:com.kabootar.GlassMemeGenerator.ImageOverlay.java

/**
 * Draws the given string centered, as big as possible, on either the top or
 * bottom 20% of the image given.//from  w w w.j a  va 2s.c o  m
 */
private static void drawStringCentered(Canvas g, String text, Bitmap image, boolean top, Context baseContext)
        throws InterruptedException {
    if (text == null)
        text = "";

    int height = 0;
    int fontSize = MAX_FONT_SIZE;
    int maxCaptionHeight = image.getHeight() / 5;
    int maxLineWidth = image.getWidth() - SIDE_MARGIN * 2;
    String formattedString = "";
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    Paint stkPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    stkPaint.setStyle(STROKE);
    stkPaint.setStrokeWidth(8);
    stkPaint.setColor(Color.BLACK);

    //Typeface tf = Typeface.create("Arial", Typeface.BOLD);
    Typeface tf = Typeface.createFromAsset(baseContext.getAssets(), "fonts/impact.ttf");

    paint.setTypeface(tf);
    stkPaint.setTypeface(tf);
    do {

        paint.setTextSize(fontSize);

        // first inject newlines into the text to wrap properly
        StringBuilder sb = new StringBuilder();
        int left = 0;
        int right = text.length() - 1;
        while (left < right) {

            String substring = text.substring(left, right + 1);
            Rect stringBounds = new Rect();
            paint.getTextBounds(substring, 0, substring.length(), stringBounds);
            while (stringBounds.width() > maxLineWidth) {
                if (Thread.currentThread().isInterrupted()) {
                    throw new InterruptedException();
                }

                // look for a space to break the line
                boolean spaceFound = false;
                for (int i = right; i > left; i--) {
                    if (text.charAt(i) == ' ') {
                        right = i - 1;
                        spaceFound = true;
                        break;
                    }
                }
                substring = text.substring(left, right + 1);
                paint.getTextBounds(substring, 0, substring.length(), stringBounds);

                // If we're down to a single word and we are still too wide,
                // the font is just too big.
                if (!spaceFound && stringBounds.width() > maxLineWidth) {
                    break;
                }
            }
            sb.append(substring).append("\n");
            left = right + 2;
            right = text.length() - 1;
        }

        formattedString = sb.toString();

        // now determine if this font size is too big for the allowed height
        height = 0;
        for (String line : formattedString.split("\n")) {
            Rect stringBounds = new Rect();
            paint.getTextBounds(line, 0, line.length(), stringBounds);
            height += stringBounds.height();
        }
        fontSize--;
    } while (height > maxCaptionHeight);

    // draw the string one line at a time
    int y = 0;
    if (top) {
        y = TOP_MARGIN;
    } else {
        y = image.getHeight() - height - BOTTOM_MARGIN;
    }
    for (String line : formattedString.split("\n")) {
        // Draw each string twice for a shadow effect
        Rect stringBounds = new Rect();
        paint.getTextBounds(line, 0, line.length(), stringBounds);
        //paint.setColor(Color.BLACK);
        //g.drawText(line, (image.getWidth() - (int) stringBounds.width()) / 2 + 2, y + stringBounds.height() + 2, paint);

        paint.setColor(Color.WHITE);
        g.drawText(line, (image.getWidth() - (int) stringBounds.width()) / 2, y + stringBounds.height(), paint);

        //stroke
        Rect strokeBounds = new Rect();
        stkPaint.setTextSize(fontSize);
        stkPaint.getTextBounds(line, 0, line.length(), strokeBounds);
        g.drawText(line, (image.getWidth() - (int) strokeBounds.width()) / 2, y + strokeBounds.height(),
                stkPaint);

        y += stringBounds.height();
    }
}

From source file:net.gsantner.opoc.util.ContextUtils.java

/**
 * Draw text in the center of the given {@link DrawableRes}
 * This may be useful for e.g. badge counts
 *///ww w.  j  a v  a  2  s  .  c  o  m
public Bitmap drawTextOnDrawable(@DrawableRes int drawableRes, String text, int textSize) {
    Resources resources = _context.getResources();
    float scale = resources.getDisplayMetrics().density;
    Bitmap bitmap = drawableToBitmap(drawableRes);

    bitmap = bitmap.copy(bitmap.getConfig(), true);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.rgb(61, 61, 61));
    paint.setTextSize((int) (textSize * scale));
    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    int x = (bitmap.getWidth() - bounds.width()) / 2;
    int y = (bitmap.getHeight() + bounds.height()) / 2;
    canvas.drawText(text, x, y, paint);

    return bitmap;
}