android.graphics.drawable.Drawable#getBounds()源码实例Demo

下面列出了android.graphics.drawable.Drawable#getBounds() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

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);
}
 
源代码2 项目: Telegram-FOSS   文件: SettingsSearchCell.java
@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;
}
 
源代码3 项目: AndroidWallet   文件: SpannableStringUtils.java
@Override
public void draw(@NonNull Canvas canvas, CharSequence text,
                 int start, int end, float x,
                 int top, int y, int bottom, @NonNull Paint paint) {
    Drawable d = getCachedDrawable();
    Rect rect = d.getBounds();
    canvas.save();
    final float fontHeight = paint.getFontMetrics().descent - paint.getFontMetrics().ascent;
    int transY = bottom - rect.bottom;
    if (rect.height() < fontHeight) { // this is the fucking code which I waste 3 days
        if (mVerticalAlignment == ALIGN_BASELINE) {
            transY -= paint.getFontMetricsInt().descent;
        } else if (mVerticalAlignment == ALIGN_CENTER) {
            transY -= (fontHeight - rect.height()) / 2;
        } else if (mVerticalAlignment == ALIGN_TOP) {
            transY -= fontHeight - rect.height();
        }
    } else {
        if (mVerticalAlignment == ALIGN_BASELINE) {
            transY -= paint.getFontMetricsInt().descent;
        }
    }
    canvas.translate(x, transY);
    d.draw(canvas);
    canvas.restore();
}
 
源代码4 项目: Transitions-Everywhere   文件: TransitionUtils.java
/**
 * 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;
}
 
源代码5 项目: ProjectX   文件: GridDrawable.java
@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;
}
 
源代码6 项目: UETool   文件: VerticalImageSpan.java
@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();
}
 
源代码7 项目: emojicon   文件: EmojiconSpan.java
@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();
}
 
源代码8 项目: EmojiChat   文件: EmojiconSpan.java
@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();
}
 
源代码9 项目: RichText   文件: ClickableImageSpan.java
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;
}
 
源代码10 项目: Indic-Keyboard   文件: KeyboardView.java
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);
}
 
源代码11 项目: imsdk-android   文件: EmojiconSpan.java
@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();
}
 
源代码12 项目: aurora-imui   文件: EmojiSpan.java
@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();
}
 
源代码13 项目: zen4android   文件: IcsProgressBar.java
@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);
        }
    }
}
 
源代码14 项目: MultiSlider   文件: MultiSlider.java
@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);
        }
    }
}
 
源代码15 项目: LiveGiftLayout   文件: AnimatedImageSpan.java
@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();

}
 
源代码16 项目: RichText   文件: AbstractImageLoader.java
@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();

}
 
源代码17 项目: imsdk-android   文件: AnimatedImageSpan.java
@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();

}
 
源代码18 项目: litho   文件: AnimatedProperties.java
@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);
  }
}
 
源代码19 项目: RichEditor   文件: CenterImageSpan.java
@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();
}
 
源代码20 项目: Markwon   文件: AsyncDrawable.java
/**
 * @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);
}