下面列出了android.graphics.drawable.Drawable#setLevel() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Convert a AnimationDrawable for use as a barberpole animation.
* Each frame of the animation is wrapped in a ClipDrawable and
* given a tiling BitmapShader.
*/
private Drawable tileifyIndeterminate(Drawable drawable) {
if (drawable instanceof AnimationDrawable) {
AnimationDrawable background = (AnimationDrawable) drawable;
final int N = background.getNumberOfFrames();
AnimationDrawable newBg = new AnimationDrawable();
newBg.setOneShot(background.isOneShot());
for (int i = 0; i < N; i++) {
Drawable frame = tileify(background.getFrame(i), true);
frame.setLevel(10000);
newBg.addFrame(frame, background.getDuration(i));
}
newBg.setLevel(10000);
drawable = newBg;
}
return drawable;
}
/**
* Convert a AnimationDrawable for use as a barberpole animation.
* Each frame of the animation is wrapped in a ClipDrawable and
* given a tiling BitmapShader.
*/
private Drawable tileifyIndeterminate(Drawable drawable) {
if (drawable instanceof AnimationDrawable) {
AnimationDrawable background = (AnimationDrawable) drawable;
final int N = background.getNumberOfFrames();
AnimationDrawable newBg = new AnimationDrawable();
newBg.setOneShot(background.isOneShot());
for (int i = 0; i < N; i++) {
Drawable frame = tileify(background.getFrame(i), true);
frame.setLevel(10000);
newBg.addFrame(frame, background.getDuration(i));
}
newBg.setLevel(10000);
drawable = newBg;
}
return drawable;
}
private LayerDrawable applyCircleDrawableToShutterBackground(LayerDrawable shutterBackground)
{
// the background for video has a circle_item drawable placeholder
// that gets replaced by an AnimatedCircleDrawable for the cool
// shrink-down-to-a-circle effect
// all other modes need not do this replace
Drawable d = shutterBackground.findDrawableByLayerId(R.id.circle_item);
if (d != null)
{
Drawable animatedCircleDrawable =
new AnimatedCircleDrawable((int) mCircleRadius);
shutterBackground
.setDrawableByLayerId(R.id.circle_item, animatedCircleDrawable);
animatedCircleDrawable.setLevel(DRAWABLE_MAX_LEVEL);
}
return shutterBackground;
}
/**
* Convert a AnimationDrawable for use as a barberpole animation.
* Each frame of the animation is wrapped in a ClipDrawable and
* given a tiling BitmapShader.
*/
private Drawable tileifyIndeterminate(Drawable drawable) {
if (drawable instanceof AnimationDrawable) {
AnimationDrawable background = (AnimationDrawable) drawable;
final int N = background.getNumberOfFrames();
AnimationDrawable newBg = new AnimationDrawable();
newBg.setOneShot(background.isOneShot());
for (int i = 0; i < N; i++) {
Drawable frame = tileify(background.getFrame(i), true);
frame.setLevel(10000);
newBg.addFrame(frame, background.getDuration(i));
}
newBg.setLevel(10000);
drawable = newBg;
}
return drawable;
}
/**
* Convert a AnimationDrawable for use as a barberpole animation.
* Each frame of the animation is wrapped in a ClipDrawable and
* given a tiling BitmapShader.
*/
private Drawable tileifyIndeterminate(Drawable drawable) {
if (drawable instanceof AnimationDrawable) {
AnimationDrawable background = (AnimationDrawable) drawable;
final int N = background.getNumberOfFrames();
AnimationDrawable newBg = new AnimationDrawable();
newBg.setOneShot(background.isOneShot());
for (int i = 0; i < N; i++) {
Drawable frame = tileify(background.getFrame(i), true);
frame.setLevel(10000);
newBg.addFrame(frame, background.getDuration(i));
}
newBg.setLevel(10000);
drawable = newBg;
}
return drawable;
}
private void updateColor() {
Drawable leftDrawable = name.getCompoundDrawablesRelative()[0];
String domain = entity.getDomain();
if (leftDrawable != null && (domain.equals(LIGHT) || domain.equals(SWITCH))) {
if (!(leftDrawable instanceof LevelListDrawable)) {
LevelListDrawable levelListDrawable = new LevelListDrawable();
// Add states
levelListDrawable.addLevel(1, 1, leftDrawable);
BitmapDrawable enabledDrawable = (BitmapDrawable) leftDrawable.getConstantState().newDrawable().mutate();
enabledDrawable.setTintList(ColorStateList.valueOf(ContextCompat.getColor(name.getContext(), R.color.color_activated)));
levelListDrawable.addLevel(2, 2, enabledDrawable);
// Restore bounds
levelListDrawable.setBounds(0, 0, name.getResources().getDimensionPixelSize(R.dimen.icon_size), name.getResources().getDimensionPixelSize(R.dimen.icon_size));
// Set drawable
name.setCompoundDrawablesRelative(levelListDrawable, null, null, null);
leftDrawable = levelListDrawable;
}
leftDrawable.setLevel(entity.state.equals(HassUtils.getOnState(entity, false)) ? 1 : 2);
}
}
private void updateBackground() {
if (isBackgroundSet) {
Drawable background = itemView.getBackground();
if (background != null) {
background.setLevel(isExpanded() ? expandedResLevel : collapsedResLevel);
}
} else {
if (isExpanded()) {
changeBackground(itemView, defaultGroupDrawable, recyclerView.mItemsBackgroundRes, expandedResLevel);
} else {
changeBackground(itemView, defaultGroupDrawable, recyclerView.mItemsBackgroundRes, collapsedResLevel);
}
}
isBackgroundSet = true;
}
/**
* Convert a AnimationDrawable for use as a barberpole animation.
* Each frame of the animation is wrapped in a ClipDrawable and
* given a tiling BitmapShader.
*/
private Drawable tileifyIndeterminate(Drawable drawable) {
if (drawable instanceof AnimationDrawable) {
AnimationDrawable background = (AnimationDrawable) drawable;
final int N = background.getNumberOfFrames();
AnimationDrawable newBg = new AnimationDrawable();
newBg.setOneShot(background.isOneShot());
for (int i = 0; i < N; i++) {
Drawable frame = tileify(background.getFrame(i), true);
frame.setLevel(10000);
newBg.addFrame(frame, background.getDuration(i));
}
newBg.setLevel(10000);
drawable = newBg;
}
return drawable;
}
/** 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);
}
}
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
Drawable d = mCurrentDrawable;
if (d != null) {
// Translate canvas so a indeterminate circular progress bar with padding
// rotates properly in its animation
canvas.save();
canvas.translate(getPaddingLeft() + mIndeterminateRealLeft, getPaddingTop() + mIndeterminateRealTop);
long time = getDrawingTime();
if (mAnimation != null) {
mAnimation.getTransformation(time, mTransformation);
float scale = mTransformation.getAlpha();
try {
mInDrawing = true;
d.setLevel((int) (scale * MAX_LEVEL));
} finally {
mInDrawing = false;
}
if (SystemClock.uptimeMillis() - mLastDrawTime >= mAnimationResolution) {
mLastDrawTime = SystemClock.uptimeMillis();
postInvalidateDelayed(mAnimationResolution);
}
}
d.draw(canvas);
canvas.restore();
if (mShouldStartAnimationDrawable && d instanceof Animatable) {
((Animatable) d).start();
mShouldStartAnimationDrawable = false;
}
}
}
/**
* Copies various properties from one drawable to the other.
*
* @param to drawable to copy properties to
* @param from drawable to copy properties from
*/
public static void copyProperties(@Nullable Drawable to, @Nullable Drawable from) {
if (from == null || to == null || to == from) {
return;
}
to.setBounds(from.getBounds());
to.setChangingConfigurations(from.getChangingConfigurations());
to.setLevel(from.getLevel());
to.setVisible(from.isVisible(), /* restart */ false);
to.setState(from.getState());
}
@Override
protected void onDraw( Canvas canvas )
{
super.onDraw( canvas );
if ( loading )
{
long time = getDrawingTime();
int i;
Drawable d;
for (i=0; i < loadingDrawables.length; i++)
{
d = loadingDrawables[i];
if (d != null)
{
if ( !(d instanceof Animatable) )
{
animation.getTransformation( time, transformation );
float scale = transformation.getAlpha();
try
{
d.setLevel( (int) (scale * MAX_LEVEL) );
} finally
{
}
ViewCompat.postInvalidateOnAnimation( this );
}
if ( mShouldStartAnimationDrawable && d instanceof Animatable )
{
((Animatable) d).start();
mShouldStartAnimationDrawable = false;
}
}
}
}
}
/**
* Copies various properties from one drawable to the other.
* @param to drawable to copy properties to
* @param from drawable to copy properties from
*/
public static void copyProperties(Drawable to, Drawable from) {
if (from == null || to == null || to == from) {
return;
}
to.setBounds(from.getBounds());
to.setChangingConfigurations(from.getChangingConfigurations());
to.setLevel(from.getLevel());
to.setVisible(from.isVisible(), /* restart */ false);
to.setState(from.getState());
}
ChildDrawable(ChildDrawable orig, LayerDrawable owner, Resources res) {
final Drawable dr = orig.mDrawable;
final Drawable clone;
if (dr != null) {
final ConstantState cs = dr.getConstantState();
if (res != null) {
clone = cs.newDrawable(res);
} else {
clone = cs.newDrawable();
}
clone.setCallback(owner);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
clone.setLayoutDirection(dr.getLayoutDirection());
clone.setBounds(dr.getBounds());
clone.setLevel(dr.getLevel());
} else {
clone = null;
}
mDrawable = clone;
mThemeAttrs = orig.mThemeAttrs;
mInsetL = orig.mInsetL;
mInsetT = orig.mInsetT;
mInsetR = orig.mInsetR;
mInsetB = orig.mInsetB;
mInsetS = orig.mInsetS;
mInsetE = orig.mInsetE;
mWidth = orig.mWidth;
mHeight = orig.mHeight;
mGravity = orig.mGravity;
mId = orig.mId;
}
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
Drawable d = mCurrentDrawable;
if (d != null) {
// Translate canvas so a indeterminate circular progress bar with padding
// rotates properly in its animation
canvas.save();
canvas.translate(getPaddingLeft() + mIndeterminateRealLeft, getPaddingTop() + mIndeterminateRealTop);
long time = getDrawingTime();
if (mAnimation != null) {
mAnimation.getTransformation(time, mTransformation);
float scale = mTransformation.getAlpha();
try {
mInDrawing = true;
d.setLevel((int) (scale * MAX_LEVEL));
} finally {
mInDrawing = false;
}
if (SystemClock.uptimeMillis() - mLastDrawTime >= mAnimationResolution) {
mLastDrawTime = SystemClock.uptimeMillis();
postInvalidateDelayed(mAnimationResolution);
}
}
d.draw(canvas);
canvas.restore();
if (mShouldStartAnimationDrawable && d instanceof Animatable) {
((Animatable) d).start();
mShouldStartAnimationDrawable = false;
}
}
}
private void switchTab(int pos) {
TextView tv = getChildAtCurPos(pos);
Drawable drawable = tv.getCompoundDrawables()[2];
int level = drawable.getLevel();
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(tv, pos, level == 1);
}
if (mLastIndicatorPosition == pos) {
// 点击同一个条目时
tv.setTextColor(level == 0 ? mTabSelectedColor : mTabDefaultColor);
drawable.setLevel(1 - level);
return;
}
mCurrentIndicatorPosition = pos;
resetPos(mLastIndicatorPosition);
//highLightPos(pos);
tv.setTextColor(mTabSelectedColor);
tv.getCompoundDrawables()[2].setLevel(1);
mLastIndicatorPosition = pos;
}
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
Drawable d = mCurrentDrawable;
if (d != null) {
// Translate canvas so a indeterminate circular progress bar with padding
// rotates properly in its animation
canvas.save();
canvas.translate(getPaddingLeft() + mIndeterminateRealLeft, getPaddingTop() + mIndeterminateRealTop);
long time = getDrawingTime();
if (mAnimation != null) {
mAnimation.getTransformation(time, mTransformation);
float scale = mTransformation.getAlpha();
try {
mInDrawing = true;
d.setLevel((int) (scale * MAX_LEVEL));
} finally {
mInDrawing = false;
}
if (SystemClock.uptimeMillis() - mLastDrawTime >= mAnimationResolution) {
mLastDrawTime = SystemClock.uptimeMillis();
postInvalidateDelayed(mAnimationResolution);
}
}
d.draw(canvas);
canvas.restore();
if (mShouldStartAnimationDrawable && d instanceof Animatable) {
((Animatable) d).start();
mShouldStartAnimationDrawable = false;
}
}
}
/**
* Initializes a drawable for display in this container.
*
* @param d The drawable to initialize.
*/
private void initializeDrawableForDisplay(Drawable d) {
if (mBlockInvalidateCallback == null) {
mBlockInvalidateCallback = new BlockInvalidateCallback();
}
// Temporary fix for suspending callbacks during initialization. We
// don't want any of these setters causing an invalidate() since that
// may call back into DrawableContainer.
d.setCallback(mBlockInvalidateCallback.wrap(d.getCallback()));
try {
if (mDrawableContainerState.mEnterFadeDuration <= 0 && mHasAlpha) {
d.setAlpha(mAlpha);
}
if (mDrawableContainerState.mHasColorFilter) {
// Color filter always overrides tint.
d.setColorFilter(mDrawableContainerState.mColorFilter);
} else {
if (mDrawableContainerState.mHasTintList) {
DrawableCompat.setTintList(d, mDrawableContainerState.mTintList);
}
if (mDrawableContainerState.mHasTintMode) {
DrawableCompat.setTintMode(d, mDrawableContainerState.mTintMode);
}
}
d.setVisible(isVisible(), true);
d.setDither(mDrawableContainerState.mDither);
d.setState(getState());
d.setLevel(getLevel());
d.setBounds(getBounds());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
d.setLayoutDirection(getLayoutDirection());
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
d.setAutoMirrored(mDrawableContainerState.mAutoMirrored);
}
final Rect hotspotBounds = mHotspotBounds;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && hotspotBounds != null) {
d.setHotspotBounds(hotspotBounds.left, hotspotBounds.top,
hotspotBounds.right, hotspotBounds.bottom);
}
} finally {
d.setCallback(mBlockInvalidateCallback.unwrap());
}
}
void fixIconImage(Resources resources, RemoteViews remoteViews, boolean hasIconBitmap, Notification notification) {
if (remoteViews == null) return;
if (!mNotificationCompat.isSystemLayout(remoteViews)) {
VLog.w(TAG, "ignore not system contentView");
return;
}
// if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
try {
//noinspection deprecation
int id = R_Hide.id.icon.get();
if (!hasIconBitmap) {
Drawable drawable = resources.getDrawable(android.R.drawable.sym_def_app_icon);//notification.icon);
drawable.setLevel(notification.iconLevel);
Bitmap bitmap = drawableToBitMap(drawable);
// Log.i(NotificationHandler.TAG, "den" + resources.getConfiguration().densityDpi);
remoteViews.setImageViewBitmap(id, bitmap);
}
if (Build.VERSION.SDK_INT >= 21) {
remoteViews.setInt(id, "setBackgroundColor", Color.TRANSPARENT);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
remoteViews.setViewPadding(id, 0, 0, 0, 0);
}
} catch (Exception e) {
e.printStackTrace();
VLog.w(TAG, "fix icon", e);
}
// } else {
// try {
// int id = R_Hide.id.icon.get();
// Icon icon = notification.getLargeIcon();
// if (icon == null) {
// icon = notification.getSmallIcon();
// }
// remoteViews.setImageViewIcon(id, icon);
// if (Build.VERSION.SDK_INT >= 21) {
// remoteViews.setInt(id, "setBackgroundColor", Color.TRANSPARENT);
// }
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
// remoteViews.setViewPadding(id, 0, 0, 0, 0);
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
}
@Override
public void set(Drawable object, Integer value) {
object.setLevel(value);
}