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

The following examples show how to use android.graphics.drawable.Drawable#getPadding() . 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: BaseEffectBridgeWrapper.java    From Android-tv-widget with Apache License 2.0 6 votes vote down vote up
/**
 * 绘制外部阴影.
 */
public void onDrawShadow(Canvas canvas) {
	Drawable drawableShadow = getShadowDrawable();
	if (drawableShadow != null) {
		RectF shadowPaddingRect = getDrawShadowRect();
		int width = getMainUpView().getWidth();
		int height = getMainUpView().getHeight();
		Rect padding = new Rect();
		drawableShadow.getPadding(padding);
           //
           int left = (int)Math.rint(shadowPaddingRect.left);
           int right = (int)Math.rint(shadowPaddingRect.right);
           int bottom = (int)Math.rint(shadowPaddingRect.bottom);
           int top = (int)Math.rint(shadowPaddingRect.top);
           //
		drawableShadow.setBounds(-padding.left - (left), -padding.top - (top),
				width + padding.right + (right),
				height + padding.bottom + (bottom));
		drawableShadow.draw(canvas);
	}
}
 
Example 2
Source File: BaseEffectBridgeWrapper.java    From Android-tv-widget with Apache License 2.0 6 votes vote down vote up
/**
 * 绘制最上层的移动边框.
 */
public void onDrawUpRect(Canvas canvas) {
	Drawable drawableUp = getUpRectDrawable();
	if (drawableUp != null) {
		RectF paddingRect = getDrawUpRect();
		int width = getMainUpView().getWidth();
		int height = getMainUpView().getHeight();
		Rect padding = new Rect();
		// 边框的绘制.
		drawableUp.getPadding(padding);
           //
           int left = (int)Math.rint(paddingRect.left);
           int right = (int)Math.rint(paddingRect.right);
           int bottom = (int)Math.rint(paddingRect.bottom);
           int top = (int)Math.rint(paddingRect.top);
           //
		drawableUp.setBounds(-padding.left - (left), -padding.top - (top),
				width + padding.right + (right), height + padding.bottom + (bottom));
		drawableUp.draw(canvas);
	}
}
 
Example 3
Source File: KeyPreviewView.java    From openboard with GNU General Public License v3.0 6 votes vote down vote up
private void setTextAndScaleX(final String text) {
    setTextScaleX(1.0f);
    setText(text);
    if (sNoScaleXTextSet.contains(text)) {
        return;
    }
    // TODO: Override {@link #setBackground(Drawable)} that is supported from API 16 and
    // calculate maximum text width.
    final Drawable background = getBackground();
    if (background == null) {
        return;
    }
    background.getPadding(mBackgroundPadding);
    final int maxWidth = background.getIntrinsicWidth() - mBackgroundPadding.left
            - mBackgroundPadding.right;
    final float width = getTextWidth(text, getPaint());
    if (width <= maxWidth) {
        sNoScaleXTextSet.add(text);
        return;
    }
    setTextScaleX(maxWidth / width);
}
 
Example 4
Source File: IconGenerator.java    From android-maps-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Set the background to a given Drawable, or remove the background.
 *
 * @param background the Drawable to use as the background, or null to remove the background.
 */
@SuppressWarnings("deprecation")
// View#setBackgroundDrawable is compatible with pre-API level 16 (Jelly Bean).
public void setBackground(Drawable background) {
    mContainer.setBackgroundDrawable(background);

    // Force setting of padding.
    // setBackgroundDrawable does not call setPadding if the background has 0 padding.
    if (background != null) {
        Rect rect = new Rect();
        background.getPadding(rect);
        mContainer.setPadding(rect.left, rect.top, rect.right, rect.bottom);
    } else {
        mContainer.setPadding(0, 0, 0, 0);
    }
}
 
Example 5
Source File: KeyPreviewView.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 6 votes vote down vote up
private void setTextAndScaleX(final String text) {
    setTextScaleX(1.0f);
    setText(text);
    if (sNoScaleXTextSet.contains(text)) {
        return;
    }
    // TODO: Override {@link #setBackground(Drawable)} that is supported from API 16 and
    // calculate maximum text width.
    final Drawable background = getBackground();
    if (background == null) {
        return;
    }
    background.getPadding(mBackgroundPadding);
    final int maxWidth = background.getIntrinsicWidth() - mBackgroundPadding.left
            - mBackgroundPadding.right;
    final float width = getTextWidth(text, getPaint());
    if (width <= maxWidth) {
        sNoScaleXTextSet.add(text);
        return;
    }
    setTextScaleX(maxWidth / width);
}
 
Example 6
Source File: KeyPreviewView.java    From Indic-Keyboard with Apache License 2.0 6 votes vote down vote up
private void setTextAndScaleX(final String text) {
    setTextScaleX(1.0f);
    setText(text);
    if (sNoScaleXTextSet.contains(text)) {
        return;
    }
    // TODO: Override {@link #setBackground(Drawable)} that is supported from API 16 and
    // calculate maximum text width.
    final Drawable background = getBackground();
    if (background == null) {
        return;
    }
    background.getPadding(mBackgroundPadding);
    final int maxWidth = background.getIntrinsicWidth() - mBackgroundPadding.left
            - mBackgroundPadding.right;
    final float width = getTextWidth(text, getPaint());
    if (width <= maxWidth) {
        sNoScaleXTextSet.add(text);
        return;
    }
    setTextScaleX(maxWidth / width);
}
 
Example 7
Source File: PreloadIconDrawable.java    From LB-Launcher with Apache License 2.0 5 votes vote down vote up
/**
 * The size of the indicator is same as the content region of the {@link #mBgDrawable} minus
 * half the stroke size to accommodate the indicator.
 */
private void initIndicatorRect()
  {
  final Drawable d=mBgDrawable;
  final Rect bounds=d.getBounds();
  d.getPadding(sTempRect);
  // Amount by which padding has to be scaled
  final float paddingScaleX=(float)bounds.width()/d.getIntrinsicWidth();
  final float paddingScaleY=(float)bounds.height()/d.getIntrinsicHeight();
  mIndicatorRect.set(bounds.left+sTempRect.left*paddingScaleX,bounds.top+sTempRect.top*paddingScaleY,bounds.right-sTempRect.right*paddingScaleX,bounds.bottom-sTempRect.bottom*paddingScaleY);
  final float inset=mPaint.getStrokeWidth()/2;
  mIndicatorRect.inset(inset,inset);
  mIndicatorRectDirty=false;
  }
 
Example 8
Source File: AutocompletePopup.java    From Autocomplete with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the height of the popup window by the size of its content. The final height may be
 * larger to accommodate styled window dressing.
 * @param height Desired height of content in pixels.
 */
@SuppressWarnings("unused")
void setContentHeight(int height) {
    Drawable popupBackground = mPopup.getBackground();
    if (popupBackground != null) {
        popupBackground.getPadding(mTempRect);
        height += mTempRect.top + mTempRect.bottom;
    }
    setHeight(height);
}
 
Example 9
Source File: ListViewCompat.java    From material with Apache License 2.0 5 votes vote down vote up
@Override
public void setSelector(Drawable sel) {
    mSelector = sel != null ? new GateKeeperDrawable(sel) : null;
    super.setSelector(mSelector);
    final Rect padding = new Rect();
    if (sel != null) {
        sel.getPadding(padding);
    }
    mSelectionLeftPadding = padding.left;
    mSelectionTopPadding = padding.top;
    mSelectionRightPadding = padding.right;
    mSelectionBottomPadding = padding.bottom;
}
 
Example 10
Source File: MultiSelectEditText.java    From AutoCompleteBubbleText with Apache License 2.0 5 votes vote down vote up
protected int calculateLineHeight(){
    Drawable bubbleDrawable = getResources().getDrawable(getBubbleResource());

    int lineHeight = getLineHeight();

    Rect rect = new Rect();
    if(bubbleDrawable.getPadding(rect)){
        return lineHeight + rect.top + rect.bottom;
    }
    return lineHeight;
}
 
Example 11
Source File: IcsListPopupWindow.java    From android-apps with MIT License 5 votes vote down vote up
public void setContentWidth(int width) {
    Drawable popupBackground = mPopup.getBackground();
    if (popupBackground != null) {
        popupBackground.getPadding(mTempRect);
        mDropDownWidth = mTempRect.left + mTempRect.right + width;
    } else {
        mDropDownWidth = width;
    }
}
 
Example 12
Source File: IcsListPopupWindow.java    From zen4android with MIT License 5 votes vote down vote up
public void setContentWidth(int width) {
    Drawable popupBackground = mPopup.getBackground();
    if (popupBackground != null) {
        popupBackground.getPadding(mTempRect);
        mDropDownWidth = mTempRect.left + mTempRect.right + width;
    } else {
        mDropDownWidth = width;
    }
}
 
Example 13
Source File: TwoWayAbsListView.java    From recent-images with MIT License 5 votes vote down vote up
public void setSelector(Drawable sel) {
	if (mSelector != null) {
		mSelector.setCallback(null);
		unscheduleDrawable(mSelector);
	}
	mSelector = sel;
	Rect padding = new Rect();
	sel.getPadding(padding);
	mSelectionLeftPadding = padding.left;
	mSelectionTopPadding = padding.top;
	mSelectionRightPadding = padding.right;
	mSelectionBottomPadding = padding.bottom;
	sel.setCallback(this);
	sel.setState(getDrawableState());
}
 
Example 14
Source File: ForegroundImageView.java    From ifican with Apache License 2.0 5 votes vote down vote up
/**
 * Supply a Drawable that is to be rendered on top of all of the child
 * views in the frame layout.  Any padding in the Drawable will be taken
 * into account by ensuring that the children are inset to be placed
 * inside of the padding area.
 *
 * @param drawable The Drawable to be drawn on top of the children.
 */
public void setForeground(Drawable drawable) {
    if (mForeground != drawable) {
        if (mForeground != null) {
            mForeground.setCallback(null);
            unscheduleDrawable(mForeground);
        }

        mForeground = drawable;

        if (drawable != null) {
            setWillNotDraw(false);
            drawable.setCallback(this);
            if (drawable.isStateful()) {
                drawable.setState(getDrawableState());
            }
            if (mForegroundGravity == Gravity.FILL) {
                Rect padding = new Rect();
                drawable.getPadding(padding);
            }
        } else {
            setWillNotDraw(true);
        }
        requestLayout();
        invalidate();
    }
}
 
Example 15
Source File: ListPopupWindow.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the width of the popup window by the size of its content. The final width may be
 * larger to accommodate styled window dressing.
 *
 * @param width Desired width of content in pixels.
 */
public void setContentWidth(int width) {
    Drawable popupBackground = mPopup.getBackground();
    if (popupBackground != null) {
        popupBackground.getPadding(mTempRect);
        mDropDownWidth = mTempRect.left + mTempRect.right + width;
    } else {
        setWidth(width);
    }
}
 
Example 16
Source File: PLAAbsListView.java    From Lay-s with MIT License 5 votes vote down vote up
public void setSelector(Drawable sel) {
    if (mSelector != null) {
        mSelector.setCallback(null);
        unscheduleDrawable(mSelector);
    }
    mSelector = sel;
    Rect padding = new Rect();
    sel.getPadding(padding);
    mSelectionLeftPadding = padding.left;
    mSelectionTopPadding = padding.top;
    mSelectionRightPadding = padding.right;
    mSelectionBottomPadding = padding.bottom;
    sel.setCallback(this);
    sel.setState(getDrawableState());
}
 
Example 17
Source File: MaterialAutoCompleteTextView.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
private int measureContentWidth() {
  ListAdapter adapter = getAdapter();
  TextInputLayout textInputLayout = findTextInputLayoutAncestor();
  if (adapter == null || textInputLayout == null) {
    return 0;
  }

  int width = 0;
  View itemView = null;
  int itemType = 0;
  final int widthMeasureSpec =
      MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.UNSPECIFIED);
  final int heightMeasureSpec =
      MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.UNSPECIFIED);

  // Cap the number of items that will be measured.
  int start = Math.max(0, modalListPopup.getSelectedItemPosition());
  final int end = Math.min(adapter.getCount(), start + MAX_ITEMS_MEASURED);
  start = Math.max(0, end - MAX_ITEMS_MEASURED);
  for (int i = start; i < end; i++) {
    final int positionType = adapter.getItemViewType(i);
    if (positionType != itemType) {
      itemType = positionType;
      itemView = null;
    }
    itemView = adapter.getView(i, itemView, textInputLayout);
    if (itemView.getLayoutParams() == null) {
      itemView.setLayoutParams(new LayoutParams(
          LayoutParams.WRAP_CONTENT,
          LayoutParams.WRAP_CONTENT));
    }
    itemView.measure(widthMeasureSpec, heightMeasureSpec);
    width = Math.max(width, itemView.getMeasuredWidth());
  }
  // Add background padding to measured width.
  Drawable background = modalListPopup.getBackground();
  if (background != null) {
    background.getPadding(tempRect);
    width += tempRect.left + tempRect.right;
  }
  // Add icon width to measured width.
  int iconWidth = textInputLayout.getEndIconView().getMeasuredWidth();
  width += iconWidth;

  return width;
}
 
Example 18
Source File: RecipientEditTextView.java    From talk-android with MIT License 4 votes vote down vote up
private Bitmap createChipBitmap(RecipientEntry contact, TextPaint paint, Bitmap icon,
                                Drawable background) {
    if (background == null) {
        Log.w(TAG, "Unable to draw a background for the chips as it was never set");
        return Bitmap.createBitmap(
                (int) mChipHeight * 2, (int) mChipHeight, Bitmap.Config.ARGB_8888);
    }

    Rect backgroundPadding = new Rect();
    background.getPadding(backgroundPadding);

    // Ellipsize the text so that it takes AT MOST the entire width of the
    // autocomplete text entry area. Make sure to leave space for padding
    // on the sides.
    int height = (int) mChipHeight + getResources().getDimensionPixelSize(R.dimen.extra_chip_height);
    // Since the icon is a square, it's width is equal to the maximum height it can be inside
    // the chip.
    int iconWidth = height - backgroundPadding.top - backgroundPadding.bottom;
    float[] widths = new float[1];
    paint.getTextWidths(" ", widths);
    CharSequence ellipsizedText = ellipsizeText(createChipDisplayText(contact), paint,
            calculateAvailableWidth() - iconWidth - widths[0] - backgroundPadding.left
                    - backgroundPadding.right);
    int textWidth = (int) paint.measureText(ellipsizedText, 0, ellipsizedText.length());

    // Make sure there is a minimum chip width so the user can ALWAYS
    // tap a chip without difficulty.
    int width = Math.max(iconWidth * 2, textWidth + (mChipPadding * 2) + iconWidth
            + backgroundPadding.left + backgroundPadding.right);

    // Create the background of the chip.
    Bitmap tmpBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(tmpBitmap);

    // Draw the background drawable
    background.setBounds(height / 2, 0, width, height);
    background.draw(canvas);
    // Draw the text vertically aligned
    int textX = shouldPositionAvatarOnRight() ?
            mChipPadding + backgroundPadding.left :
            width - backgroundPadding.right - mChipPadding - textWidth;
    paint.setColor(0xFF5C5C5C);
    paint.setAntiAlias(true);
    canvas.drawText(ellipsizedText, 0, ellipsizedText.length(),
            textX, getTextYOffset(ellipsizedText.toString(), paint, height), paint);
    if (icon != null) {
        // Draw the icon
        icon = ChipsUtil.getClip(icon);
        int iconX = shouldPositionAvatarOnRight() ?
                width - backgroundPadding.right - iconWidth :
                backgroundPadding.left;
        RectF src = new RectF(0, 0, icon.getWidth(), icon.getHeight());
        RectF dst = new RectF(0, 0, height, height);
        drawIconOnCanvas(icon, canvas, paint, src, dst);
    }
    return tmpBitmap;
}
 
Example 19
Source File: IcsSpinner.java    From android-apps with MIT License 4 votes vote down vote up
int measureContentWidth(SpinnerAdapter adapter, Drawable background) {
    if (adapter == null) {
        return 0;
    }

    int width = 0;
    View itemView = null;
    int itemType = 0;
    final int widthMeasureSpec =
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    final int heightMeasureSpec =
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

    // Make sure the number of items we'll measure is capped. If it's a huge data set
    // with wildly varying sizes, oh well.
    int start = Math.max(0, getSelectedItemPosition());
    final int end = Math.min(adapter.getCount(), start + MAX_ITEMS_MEASURED);
    final int count = end - start;
    start = Math.max(0, start - (MAX_ITEMS_MEASURED - count));
    for (int i = start; i < end; i++) {
        final int positionType = adapter.getItemViewType(i);
        if (positionType != itemType) {
            itemType = positionType;
            itemView = null;
        }
        itemView = adapter.getView(i, itemView, this);
        if (itemView.getLayoutParams() == null) {
            itemView.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
        }
        itemView.measure(widthMeasureSpec, heightMeasureSpec);
        width = Math.max(width, itemView.getMeasuredWidth());
    }

    // Add background padding to measured width
    if (background != null) {
        background.getPadding(mTempRect);
        width += mTempRect.left + mTempRect.right;
    }

    return width;
}
 
Example 20
Source File: Spinner.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
int measureContentWidth(SpinnerAdapter adapter, Drawable background) {
    if (adapter == null) {
        return 0;
    }

    int width = 0;
    View itemView = null;
    int itemType = 0;
    final int widthMeasureSpec =
        MeasureSpec.makeSafeMeasureSpec(getMeasuredWidth(), MeasureSpec.UNSPECIFIED);
    final int heightMeasureSpec =
        MeasureSpec.makeSafeMeasureSpec(getMeasuredHeight(), MeasureSpec.UNSPECIFIED);

    // Make sure the number of items we'll measure is capped. If it's a huge data set
    // with wildly varying sizes, oh well.
    int start = Math.max(0, getSelectedItemPosition());
    final int end = Math.min(adapter.getCount(), start + MAX_ITEMS_MEASURED);
    final int count = end - start;
    start = Math.max(0, start - (MAX_ITEMS_MEASURED - count));
    for (int i = start; i < end; i++) {
        final int positionType = adapter.getItemViewType(i);
        if (positionType != itemType) {
            itemType = positionType;
            itemView = null;
        }
        itemView = adapter.getView(i, itemView, this);
        if (itemView.getLayoutParams() == null) {
            itemView.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
        }
        itemView.measure(widthMeasureSpec, heightMeasureSpec);
        width = Math.max(width, itemView.getMeasuredWidth());
    }

    // Add background padding to measured width
    if (background != null) {
        background.getPadding(mTempRect);
        width += mTempRect.left + mTempRect.right;
    }

    return width;
}