List of usage examples for android.graphics.drawable Drawable copyBounds
public final void copyBounds(@NonNull Rect bounds)
From source file:Main.java
public static Bitmap asBitmap(Drawable drawable, int minWidth, int minHeight) { final Rect tmpRect = new Rect(); drawable.copyBounds(tmpRect); if (tmpRect.isEmpty()) { tmpRect.set(0, 0, Math.max(minWidth, drawable.getIntrinsicWidth()), Math.max(minHeight, drawable.getIntrinsicHeight())); drawable.setBounds(tmpRect);/*from ww w. j ava2 s. com*/ } Bitmap bitmap = Bitmap.createBitmap(tmpRect.width(), tmpRect.height(), Bitmap.Config.ARGB_8888); drawable.draw(new Canvas(bitmap)); return bitmap; }
From source file:org.telegram.ui.Components.Switch.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); final Rect padding = mTempRect; final Drawable trackDrawable = mTrackDrawable; if (trackDrawable != null) { trackDrawable.getPadding(padding); } else {//from www . ja va2 s. c o m padding.setEmpty(); } final int switchTop = mSwitchTop; final int switchBottom = mSwitchBottom; final Drawable thumbDrawable = mThumbDrawable; if (trackDrawable != null) { if (mSplitTrack && thumbDrawable != null) { final Insets insets = Insets.NONE; thumbDrawable.copyBounds(padding); padding.left += insets.left; padding.right -= insets.right; final int saveCount = canvas.save(); canvas.clipRect(padding, Region.Op.DIFFERENCE); trackDrawable.draw(canvas); canvas.restoreToCount(saveCount); } else { trackDrawable.draw(canvas); } } final int saveCount = canvas.save(); if (thumbDrawable != null) { thumbDrawable.draw(canvas); } canvas.restoreToCount(saveCount); }
From source file:android.support.v7.widget.SwitchCompat.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); final Rect padding = mTempRect; final Drawable trackDrawable = mTrackDrawable; if (trackDrawable != null) { trackDrawable.getPadding(padding); } else {//from w w w.j av a2s . com padding.setEmpty(); } final int switchTop = mSwitchTop; final int switchBottom = mSwitchBottom; final int switchInnerTop = switchTop + padding.top; final int switchInnerBottom = switchBottom - padding.bottom; final Drawable thumbDrawable = mThumbDrawable; if (trackDrawable != null) { if (mSplitTrack && thumbDrawable != null) { final Rect insets = DrawableUtils.getOpticalBounds(thumbDrawable); thumbDrawable.copyBounds(padding); padding.left += insets.left; padding.right -= insets.right; final int saveCount = canvas.save(); canvas.clipRect(padding, Region.Op.DIFFERENCE); trackDrawable.draw(canvas); canvas.restoreToCount(saveCount); } else { trackDrawable.draw(canvas); } } final int saveCount = canvas.save(); if (thumbDrawable != null) { thumbDrawable.draw(canvas); } final Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout; if (switchText != null) { final int drawableState[] = getDrawableState(); if (mTextColors != null) { mTextPaint.setColor(mTextColors.getColorForState(drawableState, 0)); } mTextPaint.drawableState = drawableState; final int cX; if (thumbDrawable != null) { final Rect bounds = thumbDrawable.getBounds(); cX = bounds.left + bounds.right; } else { cX = getWidth(); } final int left = cX / 2 - switchText.getWidth() / 2; final int top = (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2; canvas.translate(left, top); switchText.draw(canvas); } canvas.restoreToCount(saveCount); }
From source file:org.mariotaku.multivalueswitch.library.MultiValueSwitch.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); final Rect padding = mTempRect; final Drawable trackDrawable = mTrackDrawable; if (trackDrawable != null) { trackDrawable.getPadding(padding); } else {//from www . j a va 2s. c o m padding.setEmpty(); } final Drawable thumbDrawable = mThumbDrawable; if (trackDrawable != null) { if (mSplitTrack && thumbDrawable != null) { final Rect insets = MVS_DrawableUtilsAccessor.getOpticalBounds(thumbDrawable); thumbDrawable.copyBounds(padding); padding.left += insets.left; padding.right -= insets.right; final int saveCount = canvas.save(); canvas.clipRect(padding, Region.Op.DIFFERENCE); trackDrawable.draw(canvas); canvas.restoreToCount(saveCount); } else { trackDrawable.draw(canvas); } final int thumbScrollStart = mSwitchLeft + mThumbWidth - mThumbWidth / 2; final int thumbScrollRange = mSwitchRight - mSwitchLeft - mThumbWidth; final int y = trackDrawable.getBounds().centerY(); for (int i = 0, j = getMax(); i < j; i++) { if (i == 0 || i == j - 1) continue; canvas.drawCircle(thumbScrollStart + i * (thumbScrollRange / (j - 1)), y, mPointRadius, mPointPaint); } } final int saveCount = canvas.save(); if (thumbDrawable != null) { thumbDrawable.draw(canvas); } canvas.restoreToCount(saveCount); }
From source file:rikka.akashitoolkit.ui.widget.IconSwitchCompat.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); final Rect padding = mTempRect; final Drawable trackDrawable = mTrackDrawable; if (trackDrawable != null) { trackDrawable.getPadding(padding); } else {//w w w.j av a 2 s . c om padding.setEmpty(); } final int switchTop = mSwitchTop; final int switchBottom = mSwitchBottom; final int switchInnerTop = switchTop + padding.top; final int switchInnerBottom = switchBottom - padding.bottom; final Drawable thumbDrawable = mThumbDrawable; if (trackDrawable != null) { if (mSplitTrack && thumbDrawable != null) { final Rect insets = DrawableUtils.getOpticalBounds(thumbDrawable); thumbDrawable.copyBounds(padding); padding.left += insets.left; padding.right -= insets.right; final int saveCount = canvas.save(); canvas.clipRect(padding, Region.Op.DIFFERENCE); trackDrawable.draw(canvas); canvas.restoreToCount(saveCount); } else { trackDrawable.draw(canvas); } } final int saveCount = canvas.save(); if (thumbDrawable != null) { thumbDrawable.draw(canvas); } final Drawable iconDrawable = mIconDrawable; if (iconDrawable != null) { if (mSplitTrack) { final Rect insets = DrawableUtils.getOpticalBounds(iconDrawable); iconDrawable.copyBounds(padding); padding.left += insets.left; padding.right -= insets.right; //final int saveCount = canvas.save(); canvas.clipRect(padding, Region.Op.DIFFERENCE); iconDrawable.draw(canvas); canvas.restoreToCount(saveCount); } else { iconDrawable.draw(canvas); } } final Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout; if (switchText != null) { final int drawableState[] = getDrawableState(); if (mTextColors != null) { mTextPaint.setColor(mTextColors.getColorForState(drawableState, 0)); } mTextPaint.drawableState = drawableState; final int cX; if (thumbDrawable != null) { final Rect bounds = thumbDrawable.getBounds(); cX = bounds.left + bounds.right; } else { cX = getWidth(); } final int left = cX / 2 - switchText.getWidth() / 2; final int top = (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2; canvas.translate(left, top); switchText.draw(canvas); } canvas.restoreToCount(saveCount); }
From source file:com.coreform.open.android.formidablevalidation.SetErrorHandler.java
/** * Sets the Drawables (if any) to appear to the left of, above, * to the right of, and below the text. Use null if you do not * want a Drawable there. The Drawables must already have had * {@link Drawable#setBounds} called.//ww w .j a va 2s. c om * * @attr ref android.R.styleable#TextView_drawableLeft * @attr ref android.R.styleable#TextView_drawableTop * @attr ref android.R.styleable#TextView_drawableRight * @attr ref android.R.styleable#TextView_drawableBottom */ public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) { Drawables dr = mDrawables; final boolean drawables = left != null || top != null || right != null || bottom != null; if (!drawables) { // Clearing drawables... can we free the data structure? if (dr != null) { if (dr.mDrawablePadding == 0) { mDrawables = null; } else { // We need to retain the last set padding, so just clear // out all of the fields in the existing structure. dr.mDrawableLeft = null; dr.mDrawableTop = null; dr.mDrawableRight = null; dr.mDrawableBottom = null; dr.mDrawableSizeLeft = dr.mDrawableHeightLeft = 0; dr.mDrawableSizeRight = dr.mDrawableHeightRight = 0; dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0; dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0; } } } else { if (dr == null) { mDrawables = dr = new Drawables(); } dr.mDrawableLeft = left; dr.mDrawableTop = top; dr.mDrawableRight = right; dr.mDrawableBottom = bottom; final Rect compoundRect = dr.mCompoundRect; int[] state = null; state = mView.getDrawableState(); if (left != null) { left.setState(state); left.copyBounds(compoundRect); dr.mDrawableSizeLeft = compoundRect.width(); dr.mDrawableHeightLeft = compoundRect.height(); } else { dr.mDrawableSizeLeft = dr.mDrawableHeightLeft = 0; } if (right != null) { right.setState(state); right.copyBounds(compoundRect); dr.mDrawableSizeRight = compoundRect.width(); dr.mDrawableHeightRight = compoundRect.height(); } else { dr.mDrawableSizeRight = dr.mDrawableHeightRight = 0; } if (top != null) { top.setState(state); top.copyBounds(compoundRect); dr.mDrawableSizeTop = compoundRect.height(); dr.mDrawableWidthTop = compoundRect.width(); } else { dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0; } if (bottom != null) { bottom.setState(state); bottom.copyBounds(compoundRect); dr.mDrawableSizeBottom = compoundRect.height(); dr.mDrawableWidthBottom = compoundRect.width(); } else { dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0; } } mView.invalidate(); mView.requestLayout(); }
From source file:com.android.launcher3.Workspace.java
private static Rect getDrawableBounds(Drawable d) { Rect bounds = new Rect(); d.copyBounds(bounds); if (bounds.width() == 0 || bounds.height() == 0) { bounds.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); } else {/*from w ww. j a v a 2 s . co m*/ bounds.offsetTo(0, 0); } if (d instanceof PreloadIconDrawable) { int inset = -((PreloadIconDrawable) d).getOutset(); bounds.inset(inset, inset); } return bounds; }