Java Code Examples for android.graphics.drawable.Drawable#getBounds()

The following examples show how to use android.graphics.drawable.Drawable#getBounds() . 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: SettingsSearchCell.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fontMetricsInt) {
    Drawable drawable = getDrawable();
    Rect rect = drawable.getBounds();
    if (fontMetricsInt != null) {
        Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
        int fontHeight = fmPaint.descent - fmPaint.ascent;
        int drHeight = rect.bottom - rect.top;
        int centerY = fmPaint.ascent + fontHeight / 2;

        fontMetricsInt.ascent = centerY - drHeight / 2;
        fontMetricsInt.top = fontMetricsInt.ascent;
        fontMetricsInt.bottom = centerY + drHeight / 2;
        fontMetricsInt.descent = fontMetricsInt.bottom;
    }
    return rect.right;
}
 
Example 2
Source File: KeyboardView.java    From LokiBoard-Android-Keylogger with Apache License 2.0 6 votes vote down vote up
protected void onDrawKeyBackground(final Key key, final Canvas canvas,
        final Drawable background) {
    final int keyWidth = key.getDrawWidth();
    final int keyHeight = key.getHeight();
    final Rect padding = mKeyBackgroundPadding;
    final int bgWidth = keyWidth + padding.left + padding.right;
    final int bgHeight = keyHeight + padding.top + padding.bottom;
    final int bgX = -padding.left;
    final int bgY = -padding.top;
    final Rect bounds = background.getBounds();
    if (bgWidth != bounds.right || bgHeight != bounds.bottom) {
        background.setBounds(0, 0, bgWidth, bgHeight);
    }
    canvas.translate(bgX, bgY);
    background.draw(canvas);
    canvas.translate(-bgX, -bgY);
}
 
Example 3
Source File: EmojiSpan.java    From aurora-imui with MIT License 5 votes vote down vote up
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
    Drawable drawable = getDrawable();
    canvas.save();
    int transY = ((bottom - top) - drawable.getBounds().bottom) / 2 + top;
    canvas.translate(x, transY);
    drawable.draw(canvas);
    canvas.restore();
}
 
Example 4
Source File: AsyncDrawable.java    From Markwon with Apache License 2.0 5 votes vote down vote up
/**
 * @since 4.3.0
 */
@NonNull
private static Rect noDimensionsBounds(@Nullable Drawable result) {
    if (result != null) {
        final Rect bounds = result.getBounds();
        if (!bounds.isEmpty()) {
            return bounds;
        }
        final Rect intrinsicBounds = DrawableUtils.intrinsicBounds(result);
        if (!intrinsicBounds.isEmpty()) {
            return intrinsicBounds;
        }
    }
    return new Rect(0, 0, 1, 1);
}
 
Example 5
Source File: CenterImageSpan.java    From RichEditor with MIT License 5 votes vote down vote up
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y,
                 int bottom, Paint paint) {
    Drawable drawable = getCachedDrawable();
    canvas.save();
    Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
    int fontHeight = fmPaint.descent - fmPaint.ascent;
    int centerY = y + fmPaint.descent - fontHeight / 2;
    int transY = centerY - (drawable.getBounds().bottom - drawable.getBounds().top) / 2;
    canvas.translate(x, transY);
    drawable.draw(canvas);
    canvas.restore();
}
 
Example 6
Source File: AnimatedProperties.java    From litho with Apache License 2.0 5 votes vote down vote up
@Override
public float get(Object mountContent) {
  if (mountContent instanceof LithoView) {
    return ((LithoView) mountContent).getX();
  } else if (mountContent instanceof View) {
    return getPositionRelativeToLithoView((View) mountContent, true);
  } else if (mountContent instanceof Drawable) {
    final Drawable drawable = (Drawable) mountContent;
    float parentX = getPositionRelativeToLithoView(getHostView(drawable), true);
    return parentX + drawable.getBounds().left;
  } else {
    throw new UnsupportedOperationException(
        "Getting X from unsupported mount content: " + mountContent);
  }
}
 
Example 7
Source File: AnimatedImageSpan.java    From imsdk-android with MIT License 5 votes vote down vote up
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
    Drawable b = getDrawable();
    canvas.save();

    int transY = bottom - b.getBounds().bottom;
    if (mVerticalAlignment == ALIGN_BASELINE) {
        transY -= paint.getFontMetricsInt().descent;
    }

    canvas.translate(x, transY);
    b.draw(canvas);
    canvas.restore();

}
 
Example 8
Source File: AbstractImageLoader.java    From RichText with MIT License 5 votes vote down vote up
@Override
public void onLoading() {
    log(TAG, "onLoading > " + holder.getSource());
    if (activityDestroyed()) {
        return;
    }
    DrawableWrapper drawableWrapper = drawableWrapperWeakReference.get();
    if (drawableWrapper == null) {
        return;
    }
    holder.setImageState(ImageHolder.ImageState.LOADING);
    Drawable placeHolder = holder.getPlaceHolder();
    Rect bounds = placeHolder.getBounds();
    drawableWrapper.setDrawable(placeHolder);

    if (config.imageFixCallback != null) {
        config.imageFixCallback.onLoading(holder);
    }

    if (drawableWrapper.isHasCache()) {
        placeHolder.setBounds(drawableWrapper.getBounds());
    } else {
        drawableWrapper.setScaleType(holder.getScaleType());
        drawableWrapper.setBorderHolder(holder.getBorderHolder());
        drawableWrapper.setBounds(0, 0, getHolderWidth(bounds.width()), getHolderHeight(bounds.height()));

        drawableWrapper.calculate();
    }

    resetText();

}
 
Example 9
Source File: AnimatedImageSpan.java    From LiveGiftLayout with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
    Drawable b = getDrawable();
    canvas.save();

    int transY = bottom - b.getBounds().bottom;
    if (mVerticalAlignment == ALIGN_BASELINE) {
        transY -= paint.getFontMetricsInt().descent;
    }

    canvas.translate(x, transY);
    b.draw(canvas);
    canvas.restore();

}
 
Example 10
Source File: MultiSlider.java    From MultiSlider with Apache License 2.0 5 votes vote down vote up
@Override
public void invalidateDrawable(Drawable dr) {
    if (!mInDrawing) {
        if (verifyDrawable(dr)) {
            final Rect dirty = dr.getBounds();
            final int scrollX = getScrollX() + getPaddingLeft();
            final int scrollY = getScrollY() + getPaddingTop();

            invalidate(dirty.left + scrollX, dirty.top + scrollY,
                    dirty.right + scrollX, dirty.bottom + scrollY);
        } else {
            super.invalidateDrawable(dr);
        }
    }
}
 
Example 11
Source File: IcsProgressBar.java    From zen4android with MIT License 5 votes vote down vote up
@Override
public void invalidateDrawable(Drawable dr) {
    if (!mInDrawing) {
        if (verifyDrawable(dr)) {
            final Rect dirty = dr.getBounds();
            final int scrollX = getScrollX() + getPaddingLeft();
            final int scrollY = getScrollY() + getPaddingTop();

            invalidate(dirty.left + scrollX, dirty.top + scrollY,
                    dirty.right + scrollX, dirty.bottom + scrollY);
        } else {
            super.invalidateDrawable(dr);
        }
    }
}
 
Example 12
Source File: TransitionUtils.java    From Transitions-Everywhere with Apache License 2.0 5 votes vote down vote up
/**
 * Get a copy of bitmap of given drawable, return null if intrinsic size is zero
 */
@Nullable
public static Bitmap createDrawableBitmap(@NonNull Drawable drawable) {
    int width = drawable.getIntrinsicWidth();
    int height = drawable.getIntrinsicHeight();
    if (width <= 0 || height <= 0) {
        return null;
    }
    float scale = Math.min(1f, ((float)MAX_IMAGE_SIZE) / (width * height));
    if (drawable instanceof BitmapDrawable && scale == 1f) {
        // return same bitmap if scale down not needed
        return ((BitmapDrawable) drawable).getBitmap();
    }
    int bitmapWidth = (int) (width * scale);
    int bitmapHeight = (int) (height * scale);
    Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Rect existingBounds = drawable.getBounds();
    int left = existingBounds.left;
    int top = existingBounds.top;
    int right = existingBounds.right;
    int bottom = existingBounds.bottom;
    drawable.setBounds(0, 0, bitmapWidth, bitmapHeight);
    drawable.draw(canvas);
    drawable.setBounds(left, top, right, bottom);
    return bitmap;
}
 
Example 13
Source File: EmojiconSpan.java    From imsdk-android with MIT License 5 votes vote down vote up
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
    //super.draw(canvas, text, start, end, x, top, y, bottom, paint);
    Drawable b = getCachedDrawable();
    canvas.save();

    int transY = bottom - b.getBounds().bottom;
    if (mVerticalAlignment == ALIGN_BASELINE) {
      transY = top + ((bottom - top) / 2) - ((b.getBounds().bottom - b.getBounds().top) / 2) - mTop;
    }

    canvas.translate(x, transY);
    b.draw(canvas);
    canvas.restore();
}
 
Example 14
Source File: KeyboardView.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
protected void onDrawKeyBackground(@Nonnull final Key key, @Nonnull final Canvas canvas,
        @Nonnull final Drawable background) {
    final int keyWidth = key.getDrawWidth();
    final int keyHeight = key.getHeight();
    final int bgWidth, bgHeight, bgX, bgY;
    if (key.needsToKeepBackgroundAspectRatio(mDefaultKeyLabelFlags)
            // HACK: To disable expanding normal/functional key background.
            && !key.hasCustomActionLabel()) {
        final int intrinsicWidth = background.getIntrinsicWidth();
        final int intrinsicHeight = background.getIntrinsicHeight();
        final float minScale = Math.min(
                keyWidth / (float)intrinsicWidth, keyHeight / (float)intrinsicHeight);
        bgWidth = (int)(intrinsicWidth * minScale);
        bgHeight = (int)(intrinsicHeight * minScale);
        bgX = (keyWidth - bgWidth) / 2;
        bgY = (keyHeight - bgHeight) / 2;
    } else {
        final Rect padding = mKeyBackgroundPadding;
        bgWidth = keyWidth + padding.left + padding.right;
        bgHeight = keyHeight + padding.top + padding.bottom;
        bgX = -padding.left;
        bgY = -padding.top;
    }
    final Rect bounds = background.getBounds();
    if (bgWidth != bounds.right || bgHeight != bounds.bottom) {
        background.setBounds(0, 0, bgWidth, bgHeight);
    }
    canvas.translate(bgX, bgY);
    background.draw(canvas);
    canvas.translate(-bgX, -bgY);
}
 
Example 15
Source File: ClickableImageSpan.java    From RichText with MIT License 5 votes vote down vote up
public boolean clicked(int position) {
    Drawable drawable = getDrawable();
    if (drawable != null) {
        Rect rect = drawable.getBounds();
        return position <= rect.right + x && position >= rect.left + x;
    }
    return false;
}
 
Example 16
Source File: EmojiconSpan.java    From EmojiChat with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
    //super.draw(canvas, text, start, end, x, top, y, bottom, paint);
    Drawable b = getCachedDrawable();
    canvas.save();

    int transY = bottom - b.getBounds().bottom;
    if (mVerticalAlignment == ALIGN_BASELINE) {
        transY = top + ((bottom - top) / 2) - ((b.getBounds().bottom - b.getBounds().top) / 2) - mTop;
    }

    canvas.translate(x, transY);
    b.draw(canvas);
    canvas.restore();
}
 
Example 17
Source File: EmojiconSpan.java    From emojicon with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
    //super.draw(canvas, text, start, end, x, top, y, bottom, paint);
    Drawable b = getCachedDrawable();
    canvas.save();

    int transY = bottom - b.getBounds().bottom;
    if (mVerticalAlignment == ALIGN_BASELINE) {
        transY = top + ((bottom - top) / 2) - ((b.getBounds().bottom - b.getBounds().top) / 2) - mTop;
    }

    canvas.translate(x, transY);
    b.draw(canvas);
    canvas.restore();
}
 
Example 18
Source File: VerticalImageSpan.java    From UETool with MIT License 5 votes vote down vote up
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
    Drawable drawable = getDrawable();
    canvas.save();
    Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
    int fontHeight = fmPaint.descent - fmPaint.ascent;
    int centerY = y + fmPaint.descent - fontHeight / 2;
    int transY = centerY - (drawable.getBounds().bottom - drawable.getBounds().top) / 2;
    canvas.translate(x, transY);
    drawable.draw(canvas);
    canvas.restore();
}
 
Example 19
Source File: GridDrawable.java    From ProjectX with Apache License 2.0 5 votes vote down vote up
@Override
public Region getTransparentRegion() {
    if (mRowCount <= 0 || mColumnCount <= 0)
        return null;
    final Drawable drawable = getWrappedDrawable();
    if (drawable == null)
        return null;
    final Region ir = drawable.getTransparentRegion();
    if (ir == null)
        return null;
    final Region region = new Region();
    final Rect itemBound = drawable.getBounds();
    final int itemWidth = itemBound.width();
    final int itemHeight = itemBound.height();
    int dx;
    int dy;
    for (int i = 0; i < mRowCount; i++) {
        for (int j = 0; j < mColumnCount; j++) {
            dx = Math.round(j * itemWidth + (j > 0 ? (mHorizontalSpacing * j) : 0));
            dy = Math.round(i * itemHeight + (i > 0 ? (mVerticalSpacing * i) : 0));
            ir.translate(dx, dy);
            region.op(ir, Region.Op.UNION);
            ir.translate(-dx, -dy);
        }
    }
    return region;
}