下面列出了android.graphics.drawable.Drawable#getMinimumHeight() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
void drawOverscrollHeader(Canvas canvas, Drawable drawable, Rect bounds) {
final int height = drawable.getMinimumHeight();
canvas.save();
canvas.clipRect(bounds);
final int span = bounds.bottom - bounds.top;
if (span < height) {
bounds.top = bounds.bottom - height;
}
drawable.setBounds(bounds);
drawable.draw(canvas);
canvas.restore();
}
void drawOverscrollFooter(Canvas canvas, Drawable drawable, Rect bounds) {
final int height = drawable.getMinimumHeight();
canvas.save();
canvas.clipRect(bounds);
final int span = bounds.bottom - bounds.top;
if (span < height) {
bounds.bottom = bounds.top + height;
}
drawable.setBounds(bounds);
drawable.draw(canvas);
canvas.restore();
}
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 height = drawable.getMinimumHeight();
canvas.save();
canvas.clipRect(bounds);
final int span = bounds.bottom - bounds.top;
if (span < height) {
bounds.bottom = bounds.top + height;
}
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);
}
}
void drawOverscrollHeader(Canvas canvas, Drawable drawable, Rect bounds) {
final int height = drawable.getMinimumHeight();
canvas.save();
canvas.clipRect(bounds);
final int span = bounds.bottom - bounds.top;
if (span < height) {
bounds.top = bounds.bottom - height;
}
drawable.setBounds(bounds);
drawable.draw(canvas);
canvas.restore();
}
/**
* 设置富文本 图片
*
* @param text 文字
* @param drawable 图片
* @return SpannableString
*/
private SpannableString getSpannableImg(CharSequence text, Drawable drawable) {
int len = text.length();
if (len == 0 || len < 5) {
text = "请输入要展示的信息!";
return new SpannableString(text);
}
SpannableString spanableInfo = new SpannableString(text);
int drawHeight = drawable.getMinimumHeight();
drawable.setBounds(0, 0, drawHeight, drawHeight);
CenterAlignImageSpan imageSpan = new CenterAlignImageSpan(drawable);
spanableInfo.setSpan(imageSpan, len - 2, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spanableInfo.setSpan(new spanImgClickable(), text.length() - 2, text.length(),
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
return spanableInfo;
}
@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();
}
void drawOverscrollHeader(Canvas canvas, Drawable drawable, Rect bounds) {
final int height = drawable.getMinimumHeight();
canvas.save();
canvas.clipRect(bounds);
final int span = bounds.bottom - bounds.top;
if (span < height) {
bounds.top = bounds.bottom - height;
}
drawable.setBounds(bounds);
drawable.draw(canvas);
canvas.restore();
}
/**
* Manually set the track drawable
*
* @param d
*/
public void setTrackDrawable(Drawable d) {
boolean needUpdate;
if (mTrack != null && d != mTrack) {
mTrack.setCallback(null);
needUpdate = true;
} else {
needUpdate = false;
}
if (d != null) {
d.setCallback(this);
// if (canResolveLayoutDirection()) {
// d.setLayoutDirection(getLayoutDirection());
// }
// Make sure the ProgressBar is always tall enough
int drawableHeight = d.getMinimumHeight();
if (mMaxHeight < drawableHeight) {
mMaxHeight = drawableHeight;
requestLayout();
}
}
mTrack = d;
if (needUpdate) {
updateTrackBounds(getWidth(), getHeight());
updateTrackState();
//TODO update all thumbs with their range tracks also
}
}
/**
* <p>Define the drawable used to draw the progress bar in
* progress mode.</p>
*
* @param d the new drawable
*
* @see #getProgressDrawable()
* @see #setIndeterminate(boolean)
*/
public void setProgressDrawable(Drawable d) {
boolean needUpdate;
if (mProgressDrawable != null && d != mProgressDrawable) {
mProgressDrawable.setCallback(null);
needUpdate = true;
} else {
needUpdate = false;
}
if (d != null) {
d.setCallback(this);
// Make sure the ProgressBar is always tall enough
int drawableHeight = d.getMinimumHeight();
if (mMaxHeight < drawableHeight) {
mMaxHeight = drawableHeight;
requestLayout();
}
}
mProgressDrawable = d;
if (!mIndeterminate) {
mCurrentDrawable = d;
postInvalidate();
}
if (needUpdate) {
updateDrawableBounds(getWidth(), getHeight());
updateDrawableState();
doRefreshProgress(android.R.id.progress, mProgress, false, false);
doRefreshProgress(android.R.id.secondaryProgress, mSecondaryProgress, false, false);
}
}
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;
}
/**
* <p>Define the drawable used to draw the progress bar in
* progress mode.</p>
*
* @param d the new drawable
*
* @see #getProgressDrawable()
* @see #setIndeterminate(boolean)
*/
public void setProgressDrawable(Drawable d) {
boolean needUpdate;
if (mProgressDrawable != null && d != mProgressDrawable) {
mProgressDrawable.setCallback(null);
needUpdate = true;
} else {
needUpdate = false;
}
if (d != null) {
d.setCallback(this);
// Make sure the ProgressBar is always tall enough
int drawableHeight = d.getMinimumHeight();
if (mMaxHeight < drawableHeight) {
mMaxHeight = drawableHeight;
requestLayout();
}
}
mProgressDrawable = d;
if (!mIndeterminate) {
mCurrentDrawable = d;
postInvalidate();
}
if (needUpdate) {
updateDrawableBounds(getWidth(), getHeight());
updateDrawableState();
doRefreshProgress(android.R.id.progress, mProgress, false, false);
doRefreshProgress(android.R.id.secondaryProgress, mSecondaryProgress, false, false);
}
}
@Override
public int getMinimumHeight() {
if (mRowCount <= 0)
return -1;
final Drawable drawable = getWrappedDrawable();
if (drawable == null)
return -1;
final int itemHeight = drawable.getMinimumHeight();
return Math.round(mRowCount * itemHeight + mVerticalSpacing * (mRowCount - 1));
}
private boolean hasSize(Drawable d) {
if ((d.getIntrinsicHeight() <= 0 || d.getIntrinsicWidth() <= 0)
&& (d.getMinimumWidth() <= 0 || d.getMinimumHeight() <= 0)
&& (d.getBounds().width() <= 0 || d.getBounds().height() <= 0)) {
return false;
}
return true;
}
/**
* <p>Define the drawable used to draw the progress bar in
* progress mode.</p>
*
* @param d the new drawable
*
* @see #getProgressDrawable()
* @see #setIndeterminate(boolean)
*/
public void setProgressDrawable(Drawable d) {
boolean needUpdate;
if (mProgressDrawable != null && d != mProgressDrawable) {
mProgressDrawable.setCallback(null);
needUpdate = true;
} else {
needUpdate = false;
}
if (d != null) {
d.setCallback(this);
// Make sure the ProgressBar is always tall enough
int drawableHeight = d.getMinimumHeight();
if (mMaxHeight < drawableHeight) {
mMaxHeight = drawableHeight;
requestLayout();
}
}
mProgressDrawable = d;
if (!mIndeterminate) {
mCurrentDrawable = d;
postInvalidate();
}
if (needUpdate) {
updateDrawableBounds(getWidth(), getHeight());
updateDrawableState();
doRefreshProgress(android.R.id.progress, mProgress, false, false);
doRefreshProgress(android.R.id.secondaryProgress, mSecondaryProgress, false, false);
}
}
/**
* <p>Define the drawable used to draw the progress bar in
* progress mode.</p>
*
* @param d the new drawable
*
* @see #getProgressDrawable()
* @see #setIndeterminate(boolean)
*/
public void setProgressDrawable(Drawable d) {
boolean needUpdate;
if (mProgressDrawable != null && d != mProgressDrawable) {
mProgressDrawable.setCallback(null);
needUpdate = true;
} else {
needUpdate = false;
}
if (d != null) {
d.setCallback(this);
// Make sure the ProgressBar is always tall enough
int drawableHeight = d.getMinimumHeight();
if (mMaxHeight < drawableHeight) {
mMaxHeight = drawableHeight;
requestLayout();
}
}
mProgressDrawable = d;
if (!mIndeterminate) {
mCurrentDrawable = d;
postInvalidate();
}
if (needUpdate) {
updateDrawableBounds(getWidth(), getHeight());
updateDrawableState();
doRefreshProgress(android.R.id.progress, mProgress, false, false);
doRefreshProgress(android.R.id.secondaryProgress, mSecondaryProgress, false, false);
}
}
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 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 getDrawableHeight(Drawable d) {
int height = d.getIntrinsicHeight();
if (height <= 0) height = d.getMinimumHeight();
if (height <= 0) height = d.getBounds().height();
return height;
}