下面列出了android.graphics.drawable.Drawable#setCallback() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Bitmap recolorBitmap(Drawable drawable, int color) {
if (drawable == null) {
return null;
}
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
if (width <= 0 || height <= 0) {
return Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
}
Bitmap outBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outBitmap);
drawable.setBounds(0, 0, outBitmap.getWidth(), outBitmap.getHeight());
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
drawable.draw(canvas);
drawable.setColorFilter(null);
drawable.setCallback(null); // free up any references
return outBitmap;
}
/** Note: This should not change the size of the drawable. */
private void applyChildDrawable(@Nullable Drawable drawable) {
if (drawable == null) {
return;
}
drawable.setCallback(this);
DrawableCompat.setLayoutDirection(drawable, DrawableCompat.getLayoutDirection(this));
drawable.setLevel(getLevel());
drawable.setVisible(isVisible(), false);
if (drawable == closeIcon) {
if (drawable.isStateful()) {
drawable.setState(getCloseIconState());
}
DrawableCompat.setTintList(drawable, closeIconTint);
return;
}
if (drawable.isStateful()) {
drawable.setState(getState());
}
if (drawable == chipIcon && hasChipIconTint) {
DrawableCompat.setTintList(chipIcon, chipIconTint);
}
}
/**
* Set the drawable to use for the content scrim from resources. Providing null will disable
* the scrim functionality.
*
* @param drawable the drawable to display
* @attr ref R.styleable#FlexibleToolbarLayout_contentScrimColor
* @see #getContentScrim()
*/
public void setContentScrim(@Nullable Drawable drawable) {
if (mContentScrim != drawable) {
if (mContentScrim != null) {
mContentScrim.setCallback(null);
}
if (drawable != null) {
mContentScrim = drawable.mutate();
drawable.setBounds(0, 0, getWidth(), getHeight());
drawable.setCallback(this);
drawable.setAlpha(mScrimAlpha);
} else {
mContentScrim = null;
}
ViewCompat.postInvalidateOnAnimation(this);
}
}
/**
* Assign a new tick mark drawable
* @param tickMark
*/
public void setTickMark(Drawable tickMark) {
if (mTickMark != null) {
mTickMark.setCallback(null);
}
mTickMark = tickMark;
if (tickMark != null) {
setProgressOffset(0);
tickMark.setCallback(this);
if (tickMark.isStateful()) {
tickMark.setState(getDrawableState());
}
final int w = tickMark.getIntrinsicWidth();
final int h = tickMark.getIntrinsicHeight();
final int halfW = w >= 0 ? w / 2 : 1;
final int halfH = h >= 0 ? h / 2 : 1;
tickMark.setBounds(-halfW, -halfH, halfW, halfH);
applyTickMarkTint();
}
invalidate();
}
@Override
public void setDrawable(Drawable drawable) {
// Remove old callback
Drawable oldDrawable = getDrawable();
if (oldDrawable != null) {
oldDrawable.setCallback(null);
}
super.setDrawable(drawable);
if (drawable != null) {
drawable.setCallback(mTextView);
}
updateBounds();
if (drawable != null) {
invalidateSelf();
}
}
public static void setEdgeEffectColor(final EdgeEffect edgeEffect, @ColorRes final int color) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
edgeEffect.setColor(color);
return;
}
final Field edgeField = EdgeEffect.class.getDeclaredField("mEdge");
final Field glowField = EdgeEffect.class.getDeclaredField("mGlow");
edgeField.setAccessible(true);
glowField.setAccessible(true);
final Drawable edge = (Drawable) edgeField.get(edgeEffect);
final Drawable glow = (Drawable) glowField.get(edgeEffect);
edge.setColorFilter(color, PorterDuff.Mode.SRC_IN);
glow.setColorFilter(color, PorterDuff.Mode.SRC_IN);
edge.setCallback(null); // free up any references
glow.setCallback(null); // free up any references
} catch (final Exception ignored) {
ignored.printStackTrace();
}
}
@Override
public void setDrawable(Drawable drawable) {
// Remove old callback
Drawable oldDrawable = getDrawable();
if (oldDrawable != null) {
oldDrawable.setCallback(null);
}
super.setDrawable(drawable);
if (drawable != null) {
drawable.setCallback(mTextView);
}
updateBounds();
if (drawable != null) {
invalidateSelf();
}
}
/**
* Set the button graphic to a given Drawable
*
* @param d The Drawable to use as the button graphic
*/
public void setButtonDrawable(Drawable d) {
if (drawable != d) {
if (drawable != null) {
drawable.setCallback(null);
unscheduleDrawable(drawable);
}
drawable = d;
if (d != null) {
drawable = DrawableCompat.wrap(d);
d.setCallback(this);
//d.setLayoutDirection(getLayoutDirection());
if (d.isStateful()) {
d.setState(getDrawableState());
}
d.setVisible(getVisibility() == VISIBLE, false);
setMinHeight(d.getIntrinsicHeight());
updateButtonTint();
}
}
}
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());
}
public void setLeftDrawable(Drawable drawable) {
if (leftDrawable == drawable) {
return;
}
if (leftDrawable != null) {
leftDrawable.setCallback(null);
}
leftDrawable = drawable;
if (drawable != null) {
drawable.setCallback(this);
}
if (!recreateLayoutMaybe()) {
invalidate();
}
}
/**
* <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);
}
}
public CombinedDrawable(Drawable backgroundDrawable, Drawable iconDrawable) {
background = backgroundDrawable;
icon = iconDrawable;
if (iconDrawable != null) {
iconDrawable.setCallback(this);
}
}
@SuppressWarnings("unused")
private void destoryBitmaps()
{
for (int i = 0; i < imageViewsList.size(); i++)
{
ImageView imageView = imageViewsList.get(i);
Drawable drawable = imageView.getDrawable();
if (drawable != null)
{
drawable.setCallback(null);
}
}
}
private void unmountDrawable(Drawable drawable) {
assertMainThread();
drawable.setCallback(null);
invalidate(drawable.getBounds());
releaseScrapDataStructuresIfNeeded();
}
/**
* Sets callback to the drawable.
* @param drawable drawable to set callbacks to
* @param callback standard Android Drawable.Callback
* @param transformCallback TransformCallback used by TransformAwareDrawables
*/
public static void setCallbacks(
Drawable drawable,
@Nullable Drawable.Callback callback,
@Nullable TransformCallback transformCallback) {
if (drawable != null) {
drawable.setCallback(callback);
if (drawable instanceof TransformAwareDrawable) {
((TransformAwareDrawable) drawable).setTransformCallback(transformCallback);
}
}
}
private void setCheckMarkDrawableInternal(@Nullable Drawable d, @DrawableRes int resId) {
if (mCheckMarkDrawable != null) {
mCheckMarkDrawable.setCallback(null);
unscheduleDrawable(mCheckMarkDrawable);
}
mNeedRequestlayout = (d != mCheckMarkDrawable);
if (d != null) {
d.setCallback(this);
d.setVisible(getVisibility() == VISIBLE, false);
d.setState(CHECKED_STATE_SET);
// Record the intrinsic dimensions when in "checked" state.
setMinHeight(d.getIntrinsicHeight());
mCheckMarkWidth = d.getIntrinsicWidth();
d.setState(getDrawableState());
} else {
mCheckMarkWidth = 0;
}
mCheckMarkDrawable = d;
mCheckMarkResource = resId;
applyCheckMarkTint();
// Do padding resolution. This will call internalSetPadding() and do a
// requestLayout() if needed.
resolvePadding();
}
/**
* 添加drawable, 如果id已经存在, drawable将会被替换
*
* @param id the drawable id.
* @param drawable the drawable.
* @return <code>true</code> - 如果添加成功, <code>false</code> - 其他
*/
public boolean addDrawable(int id, @NonNull Drawable drawable) {
DrawableInfo old = findAvatarDrawableById(id);
if (old != null) {
Drawable d = old.mDrawable;
old.mDrawable = drawable;
if (!hasSameDrawable(d)) {
cleanDrawable(d);
}
updateDrawableBounds(old);
} else {
if (getNumberOfDrawables() >= MAX_DRAWABLE_COUNT) {
return false;
}
mDrawables.add(crateAvatarDrawable(id, drawable));
layoutDrawables();
}
drawable.setCallback(this);
drawable.setVisible(getWindowVisibility() == VISIBLE && isShown(), true);
if (drawable.isStateful()) {
drawable.setState(getDrawableState());
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
drawable.setLayoutDirection(getLayoutDirection());
}
invalidate();
return true;
}
/**
* 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(@Nullable Drawable drawable) {
if (mForeground != drawable) {
if (mForeground != null) {
mForeground.setCallback(null);
mView.unscheduleDrawable(mForeground);
}
mForeground = drawable;
if (drawable != null) {
mView.setWillNotDraw(false);
drawable.setCallback(mView);
if (drawable.isStateful()) {
drawable.setState(mView.getDrawableState());
}
if (mForegroundGravity == Gravity.FILL) {
Rect padding = new Rect();
drawable.getPadding(padding);
}
} else {
mView.setWillNotDraw(true);
}
mView.requestLayout();
mView.invalidate();
}
}
public EmojiSpan(Drawable drawable, View view) {
super(drawable);
drawable.setCallback(view);
}
private void init(AttributeSet attrs, int defStyle) {
// Load attributes
final TypedArray a = getContext().obtainStyledAttributes(
attrs, R.styleable.CircularView, defStyle, 0);
final int centerBackgroundColor = a.getColor(
R.styleable.CircularView_centerBackgroundColor,
CircularViewObject.NO_COLOR);
// Set up a default TextPaint object
mTextPaint = new TextPaint();
mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setTextAlign(Paint.Align.LEFT);
mText = a.getString(R.styleable.CircularView_text);
mTextPaint.setTextSize(a.getDimension(
R.styleable.CircularView_textSize,
24f));
mTextPaint.setColor(a.getColor(
R.styleable.CircularView_textColor,
mTextPaint.getColor()));
Drawable circleDrawable = null;
if (a.hasValue(R.styleable.CircularView_centerDrawable)) {
circleDrawable = a.getDrawable(
R.styleable.CircularView_centerDrawable);
circleDrawable.setCallback(this);
}
mHighlightedDegreeObjectAnimator = new ObjectAnimator();
mHighlightedDegreeObjectAnimator.setTarget(CircularView.this);
mHighlightedDegreeObjectAnimator.setPropertyName("highlightedDegree");
mHighlightedDegreeObjectAnimator.addListener(mAnimatorListener);
// Update TextPaint and text measurements from attributes
invalidateTextPaintAndMeasurements();
mCirclePaint = new Paint();
mCirclePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
mCirclePaint.setStyle(Paint.Style.FILL);
mCirclePaint.setColor(Color.RED);
mDrawHighlightedMarkerOnTop = a.getBoolean(R.styleable.CircularView_drawHighlightedMarkerOnTop, false);
mHighlightedMarker = null;
mHighlightedMarkerPosition = -1;
mHighlightedDegree = a.getFloat(R.styleable.CircularView_highlightedDegree, HIGHLIGHT_NONE);
mMarkerStartingPoint = a.getFloat(R.styleable.CircularView_markerStartingPoint, 0f);
mAnimateMarkersOnStillHighlight = a.getBoolean(R.styleable.CircularView_animateMarkersOnStillHighlight, false);
mAnimateMarkersOnHighlightAnimation = false;
mIsAnimating = false;
mCircle = new CircularViewObject(getContext(), CIRCLE_TO_MARKER_PADDING, centerBackgroundColor);
mCircle.setSrc(circleDrawable);
mCircle.setFitToCircle(a.getBoolean(R.styleable.CircularView_fitToCircle, false));
mDefaultMarkerRadius = getResources().getInteger(R.integer.cv_default_marker_radius);
mEditModeMarkerCount = a.getInt(R.styleable.CircularView_editMode_markerCount, 0);
mEditModeMarkerRadius = a.getInt(R.styleable.CircularView_editMode_markerRadius, mDefaultMarkerRadius);
a.recycle();
setOnLongClickListener(mOnLongClickListener);
if (isInEditMode()) {
mAdapter = new SimpleCircularViewAdapter() {
@Override
public int getCount() {
return mEditModeMarkerCount;
}
@Override
public void setupMarker(int position, Marker marker) {
marker.setRadius(mEditModeMarkerRadius);
marker.setCenterBackgroundColor(getResources().getColor(android.R.color.black));
}
};
}
}