下面列出了android.graphics.drawable.Drawable#getMinimumWidth() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private LinearLayout.LayoutParams generateLayoutParams(Drawable drawable, int size, int margin) {
if (mLayoutParams == null) {
final int minimumWidth = drawable.getMinimumWidth();
final int minimumHeight = drawable.getMinimumHeight();
LinearLayout.LayoutParams layoutParams;
if (minimumWidth == 0 || minimumHeight == 0) {
layoutParams = new LinearLayout.LayoutParams(size, size);
} else {
layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
}
layoutParams.leftMargin = margin;
mLayoutParams = layoutParams;
}
return mLayoutParams;
}
void drawOverscrollFooter( Canvas canvas, Drawable drawable, Rect bounds ) {
final int width = drawable.getMinimumWidth();
canvas.save();
canvas.clipRect( bounds );
final int span = bounds.right - bounds.left;
if ( span < width ) {
bounds.right = bounds.left + width;
}
drawable.setBounds( bounds );
drawable.draw( canvas );
canvas.restore();
}
void drawOverscrollHeader( Canvas canvas, Drawable drawable, Rect bounds ) {
final int width = drawable.getMinimumWidth();
canvas.save();
canvas.clipRect( bounds );
final int span = bounds.right - bounds.left;
if ( span < width ) {
bounds.left = bounds.right - width;
}
drawable.setBounds( bounds );
drawable.draw( canvas );
canvas.restore();
}
/**
* Set tint color on a given {@link Button}. This method will set an
* instance of this {@link PXButtonDrawable} as the {@link Button}s
* background drawable.
*
* @param button A {@link Button}. In case the button does not have a
* {@link PXButtonDrawable} as a background, this method will
* attach one and theme it.
* @param color
*/
@SuppressWarnings("deprecation")
public static void setTintColor(Button button, int color) {
Drawable drawable = button.getBackground();
if (drawable instanceof PXButtonDrawable) {
setTintColor(button, (PXButtonDrawable) drawable, color);
} else {
Drawable prevDrawable = button.getBackground();
PXButtonDrawable pxDrawable;
if (prevDrawable != null) {
pxDrawable = new PXButtonDrawable(prevDrawable.getMinimumHeight(),
prevDrawable.getMinimumWidth());
} else {
// TODO: API 16 supports getMinimunHeight and getMinimumWidth
pxDrawable = new PXButtonDrawable(button.getHeight(), button.getWidth());
}
button.setBackgroundDrawable(pxDrawable);
setTintColor(button, pxDrawable, color);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
viewSize = new PointF(width, height);
//��ȡ��ǰDrawable�Ĵ�С
Drawable drawable = getDrawable();
if (drawable == null) {
Log.e("no drawable", "drawable is nullPtr");
} else {
imageSize = new PointF(drawable.getMinimumWidth(), drawable.getMinimumHeight());
}
FitCenter();
}
public void setHeaderBackground(Drawable drawable) {
if (mBkgImgView != null) {
mBkgImgView.setImageDrawable(drawable);
int width = drawable.getMinimumWidth();
int height = drawable.getMinimumHeight();
if (width <= 0) {
return;
}
DisplayMetrics dm = new DisplayMetrics();
((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm);
int viewHeight = (height * dm.widthPixels) / width;
ViewGroup.LayoutParams lp = mBkgImgView.getLayoutParams();
lp.height = viewHeight;
mMaxHeaderHeight = viewHeight;
mBkgImgView.setLayoutParams(lp);
}
}
protected void computeConstantSize() {
mCheckedConstantSize = true;
createAllFutures();
final int count = mNumChildren;
final Drawable[] drawables = mDrawables;
mConstantWidth = mConstantHeight = -1;
mConstantMinimumWidth = mConstantMinimumHeight = 0;
for (int i = 0; i < count; i++) {
final Drawable dr = drawables[i];
int s = dr.getIntrinsicWidth();
if (s > mConstantWidth) mConstantWidth = s;
s = dr.getIntrinsicHeight();
if (s > mConstantHeight) mConstantHeight = s;
s = dr.getMinimumWidth();
if (s > mConstantMinimumWidth) mConstantMinimumWidth = s;
s = dr.getMinimumHeight();
if (s > mConstantMinimumHeight) mConstantMinimumHeight = s;
}
}
/**
* Compound buttons (ex. {{@link android.widget.CheckBox}}, {@link android.widget.ToggleButton})
* have a button drawable with minimum height and width specified for them.
* To align the behavior with CSS Flexbox we want to respect these minimum measurement to avoid
* these drawables from being cut off during calculation. When the compound button has a minimum
* width or height already specified we will not make any change since we assume those were
* voluntarily set by the user.
*
* @param compoundButton the compound button that need to be evaluated
*/
private void evaluateMinimumSizeForCompoundButton(CompoundButton compoundButton) {
FlexItem flexItem = (FlexItem) compoundButton.getLayoutParams();
int minWidth = flexItem.getMinWidth();
int minHeight = flexItem.getMinHeight();
Drawable drawable = CompoundButtonCompat.getButtonDrawable(compoundButton);
int drawableMinWidth = drawable == null ? 0 : drawable.getMinimumWidth();
int drawableMinHeight = drawable == null ? 0 : drawable.getMinimumHeight();
flexItem.setMinWidth(minWidth == NOT_SET ? drawableMinWidth : minWidth);
flexItem.setMinHeight(minHeight == NOT_SET ? drawableMinHeight : minHeight);
}
void drawOverscrollHeader(Canvas canvas, Drawable drawable, Rect bounds) {
int width = drawable.getMinimumWidth();
canvas.save();
canvas.clipRect(bounds);
if (bounds.right - bounds.left < width) {
bounds.left = bounds.right - width;
}
drawable.setBounds(bounds);
drawable.draw(canvas);
canvas.restore();
}
void drawOverscrollFooter(Canvas canvas, Drawable drawable, Rect bounds) {
int width = drawable.getMinimumWidth();
canvas.save();
canvas.clipRect(bounds);
if (bounds.right - bounds.left < width) {
bounds.right = bounds.left + width;
}
drawable.setBounds(bounds);
drawable.draw(canvas);
canvas.restore();
}
private void setHeaderIcon(ContextMenu menu, Drawable icon) {
int width = getHeaderIconWidth();
if (icon.getMinimumWidth() <= width) {
menu.setHeaderIcon(icon);
} else if (icon instanceof BitmapDrawable) {
Bitmap bitmap = Bitmap.createScaledBitmap(((BitmapDrawable) icon).getBitmap(), width, width, false);
menu.setHeaderIcon(new BitmapDrawable(getResources(), bitmap));
}
}
@Override
public int getWidth() {
Drawable drawable = _drawable;
if (drawable == null) {
return 0;
}
return drawable.getMinimumWidth();
}
@Override
public int getMinimumWidth() {
if (mColumnCount <= 0)
return -1;
final Drawable drawable = getWrappedDrawable();
if (drawable == null)
return -1;
final int itemWidth = drawable.getMinimumWidth();
return Math.round(mColumnCount * itemWidth + mHorizontalSpacing * (mColumnCount - 1));
}
private Bitmap convertToBitmap(Drawable drawable) {
Bitmap mutableBitmap;
if(drawable.getMinimumHeight() == 0 || drawable.getMinimumWidth() == 0)
mutableBitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
else
mutableBitmap = Bitmap.createBitmap(drawable.getMinimumWidth(), drawable.getMinimumHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mutableBitmap);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
drawable.draw(canvas);
return mutableBitmap;
}
private boolean hasSize(Drawable d) {
return (d.getIntrinsicHeight() > 0 && d.getIntrinsicWidth() > 0)
|| (d.getMinimumWidth() > 0 && d.getMinimumHeight() > 0)
|| (d.getBounds().width() > 0 && d.getBounds().height() > 0);
}
private static int getDrawableWidth(Drawable d) {
int width = d.getIntrinsicWidth();
if (width <= 0) width = d.getMinimumWidth();
if (width <= 0) width = d.getBounds().width();
return width;
}
private boolean hasSize(Drawable d) {
return (d.getIntrinsicHeight() > 0 && d.getIntrinsicWidth() > 0)
|| (d.getMinimumWidth() > 0 && d.getMinimumHeight() > 0)
|| (d.getBounds().width() > 0 && d.getBounds().height() > 0);
}
public PreviewTimeBar(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs,
com.google.android.exoplayer2.ui.R.styleable.DefaultTimeBar, 0, 0);
scrubberColor = typedArray.getInt(
com.google.android.exoplayer2.ui.R.styleable.DefaultTimeBar_scrubber_color,
DEFAULT_SCRUBBER_COLOR);
final Drawable scrubberDrawable = typedArray.getDrawable(
com.google.android.exoplayer2.ui.R.styleable.DefaultTimeBar_scrubber_drawable);
final int scrubberEnabledSize = typedArray.getDimensionPixelSize(
com.google.android.exoplayer2.ui.R.styleable.DefaultTimeBar_scrubber_enabled_size,
dpToPx(context.getResources().getDisplayMetrics(),
DEFAULT_SCRUBBER_ENABLED_SIZE_DP));
final int scrubberDisabledSize = typedArray.getDimensionPixelSize(
com.google.android.exoplayer2.ui.R.styleable.DefaultTimeBar_scrubber_disabled_size,
dpToPx(context.getResources().getDisplayMetrics(),
DEFAULT_SCRUBBER_DISABLED_SIZE_DP));
final int scrubberDraggedSize = typedArray.getDimensionPixelSize(
com.google.android.exoplayer2.ui.R.styleable.DefaultTimeBar_scrubber_dragged_size,
dpToPx(context.getResources().getDisplayMetrics(),
DEFAULT_SCRUBBER_DRAGGED_SIZE_DP));
// Calculate the scrubber padding based on the maximum size the scrubber can have
if (scrubberDrawable != null) {
scrubberPadding = (scrubberDrawable.getMinimumWidth() + 1) / 2;
} else {
scrubberPadding =
(Math.max(scrubberDisabledSize,
Math.max(scrubberEnabledSize, scrubberDraggedSize)) + 1) / 2;
}
typedArray.recycle();
typedArray = context.getTheme().obtainStyledAttributes(
attrs, R.styleable.PreviewTimeBar, 0, 0);
previewId = typedArray.getResourceId(
R.styleable.PreviewTimeBar_previewFrameLayout, View.NO_ID);
delegate = new PreviewDelegate(this);
delegate.setPreviewEnabled(isEnabled());
delegate.setAnimationEnabled(typedArray.getBoolean(
R.styleable.PreviewTimeBar_previewAnimationEnabled, true));
delegate.setPreviewEnabled(typedArray.getBoolean(
R.styleable.PreviewTimeBar_previewEnabled, true));
delegate.setAutoHidePreview(typedArray.getBoolean(
R.styleable.PreviewTimeBar_previewAutoHide, true));
typedArray.recycle();
addListener(new TimeBarDefaultOnScrubListener());
}
private Drawable createBadgeDrawable(Drawable d, int count) {
if (d == null) return null;
NumberFormat f = NumberFormat.getIntegerInstance();
String countStr = count > 99 ? "99+" : f.format(count);
Bitmap b = Utils.drawableToBitmap(d);
b = b.copy(Bitmap.Config.ARGB_8888, true);
Canvas c = new Canvas(b);
Paint p = new Paint();
p.setTextAlign(Paint.Align.CENTER);
p.setColor(Color.WHITE);
p.setAntiAlias(true);
p.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10,
mResources.getDisplayMetrics()));
Drawable bg = mGbResources.getDrawable(R.drawable.ic_notification_overlay, null);
final int w = b.getWidth();
final int h = b.getHeight();
final Rect r = new Rect();
p.getTextBounds(countStr, 0, countStr.length(), r);
final int tw = r.right - r.left;
final int th = r.bottom - r.top;
bg.getPadding(r);
int dw = r.left + tw + r.right;
if (dw < bg.getMinimumWidth()) {
dw = bg.getMinimumWidth();
}
int x = w-r.right-((dw-r.right-r.left)/2);
int dh = r.top + th + r.bottom;
if (dh < bg.getMinimumHeight()) {
dh = bg.getMinimumHeight();
}
if (dw < dh) dw = dh;
int y = h-r.bottom-((dh-r.top-th-r.bottom)/2);
bg.setBounds(w-dw, h-dh, w, h);
bg.draw(c);
c.drawText(countStr, x, y, p);
return new BitmapDrawable(mResources, b);
}
private static int getDrawableWidth(Drawable d) {
int width = d.getIntrinsicWidth();
if (width <= 0) width = d.getMinimumWidth();
if (width <= 0) width = d.getBounds().width();
return width;
}