下面列出了android.graphics.drawable.Drawable#getCurrent() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static boolean sameBitmap(Context context, Drawable drawable, int resourceId) {
Drawable otherDrawable = ContextCompat.getDrawable(context, resourceId);
if (drawable == null || otherDrawable == null) {
return false;
}
if (drawable instanceof StateListDrawable && otherDrawable instanceof StateListDrawable) {
drawable = drawable.getCurrent();
otherDrawable = otherDrawable.getCurrent();
}
if (drawable instanceof BitmapDrawable) {
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
Bitmap otherBitmap = ((BitmapDrawable) otherDrawable).getBitmap();
return bitmap.sameAs(otherBitmap);
}
return false;
}
private static boolean sameBitmap(Context context, Drawable drawable, int resourceId) {
if (resourceId == 0) {
return (drawable == null);
}
Drawable otherDrawable = ContextCompat.getDrawable(context, resourceId);
if (drawable == null || otherDrawable == null) {
return false;
}
if (drawable instanceof StateListDrawable) {
drawable = drawable.getCurrent();
}
if (otherDrawable instanceof StateListDrawable) {
otherDrawable = otherDrawable.getCurrent();
}
if (drawable instanceof BitmapDrawable) {
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
Bitmap otherBitmap = ((BitmapDrawable) otherDrawable).getBitmap();
return bitmap.sameAs(otherBitmap);
}
return false;
}
protected void keyPressed() {
if (isEnabled() && isClickable()) {
Drawable selector = this.mSelector;
Rect selectorRect = this.mSelectorRect;
if (selector == null) {
return;
}
if ((isFocused() || touchModeDrawsInPressedState()) && !selectorRect.isEmpty()) {
View v = getChildAt(this.mSelectedPosition - this.mFirstPosition);
if (v != null) {
if (!v.hasFocusable()) {
v.setPressed(true);
} else {
return;
}
}
setPressed(true);
boolean longClickable = isLongClickable();
Drawable d = selector.getCurrent();
if (d != null && (d instanceof TransitionDrawable)) {
if (longClickable) {
((TransitionDrawable) d).startTransition(ViewConfiguration.getLongPressTimeout());
} else {
((TransitionDrawable) d).resetTransition();
}
}
if (longClickable && !this.mDataChanged) {
if (this.mPendingCheckForKeyLongPress == null) {
this.mPendingCheckForKeyLongPress = new CheckForKeyLongPress(this, null);
}
this.mPendingCheckForKeyLongPress.rememberWindowAttachCount();
postDelayed(this.mPendingCheckForKeyLongPress, (long) ViewConfiguration.getLongPressTimeout());
}
}
}
}
/**
* Returns the parent drawable at the specified index.
* <p>
* If MatrixDrawable or ScaleTypeDrawable is found at that index, it will be returned as a parent.
* Otherwise, the FadeDrawable will be returned.
*/
private Drawable findLayerParent(int index) {
Drawable parent = mFadeDrawable;
Drawable child = mFadeDrawable.getDrawable(index);
if (child instanceof MatrixDrawable) {
parent = child;
child = parent.getCurrent();
}
if (child instanceof ScaleTypeDrawable) {
parent = child;
child = parent.getCurrent();
}
return parent;
}
/**
* Returns the ScaleTypeDrawable at the specified index, or null if not found.
*/
private @Nullable ScaleTypeDrawable findLayerScaleTypeDrawable(int index) {
Drawable drawable = mFadeDrawable.getDrawable(index);
if (drawable instanceof MatrixDrawable) {
drawable = drawable.getCurrent();
}
if (drawable instanceof ScaleTypeDrawable) {
return (ScaleTypeDrawable) drawable;
} else {
return null;
}
}
/**
* Sets the selector state to "pressed" and posts a CheckForKeyLongPress to see if
* this is a long press.
*/
void keyPressed() {
if (!isEnabled() || !isClickable()) {
return;
}
Drawable selector = mSelector;
Rect selectorRect = mSelectorRect;
if (selector != null && (isFocused() || touchModeDrawsInPressedState())
&& selectorRect != null && !selectorRect.isEmpty()) {
final View v = getChildAt(mSelectedPosition - mFirstPosition);
if (v != null) {
if (v.hasFocusable()) return;
v.setPressed(true);
}
setPressed(true);
final boolean longClickable = isLongClickable();
Drawable d = selector.getCurrent();
if (d != null && d instanceof TransitionDrawable) {
if (longClickable) {
((TransitionDrawable) d).startTransition(
ViewConfiguration.getLongPressTimeout());
} else {
((TransitionDrawable) d).resetTransition();
}
}
if (longClickable && !mDataChanged) {
if (mPendingCheckForKeyLongPress == null) {
mPendingCheckForKeyLongPress = new CheckForKeyLongPress();
}
mPendingCheckForKeyLongPress.rememberWindowAttachCount();
postDelayed(mPendingCheckForKeyLongPress, ViewConfiguration.getLongPressTimeout());
}
}
}
public static int getDrawableAlpha(Drawable background) {
if (background == null)
return 255;
background = background.getCurrent();
if (background instanceof ColorDrawable)
return ((ColorDrawable) background).getAlpha();
if (background instanceof AlphaDrawable)
return ((AlphaDrawable) background).getAlpha();
return 255;
}
/**
* Sets the selector state to "pressed" and posts a CheckForKeyLongPress to see if this is a long press.
*/
protected void keyPressed() {
if ( !isEnabled() || !isClickable() ) {
return;
}
Drawable selector = mSelector;
Rect selectorRect = mSelectorRect;
if ( selector != null && ( isFocused() || touchModeDrawsInPressedState() )
&& !selectorRect.isEmpty() ) {
final View v = getChildAt( mSelectedPosition - mFirstPosition );
if ( v != null ) {
if ( v.hasFocusable() ) return;
v.setPressed( true );
}
setPressed( true );
final boolean longClickable = isLongClickable();
Drawable d = selector.getCurrent();
if ( d != null && d instanceof TransitionDrawable ) {
if ( longClickable ) {
( (TransitionDrawable) d ).startTransition(
ViewConfiguration.getLongPressTimeout() );
} else {
( (TransitionDrawable) d ).resetTransition();
}
}
if ( longClickable && !mDataChanged ) {
if ( mPendingCheckForKeyLongPress == null ) {
mPendingCheckForKeyLongPress = new CheckForKeyLongPress();
}
mPendingCheckForKeyLongPress.rememberWindowAttachCount();
postDelayed( mPendingCheckForKeyLongPress, ViewConfiguration.getLongPressTimeout() );
}
}
}
public static int getLayer(View view) {
// 不可见的即为没有背景
if (view == null || view.getVisibility() != View.VISIBLE) {
return 0;
}
int layer = 0;
// 没有文字也为没有背景
if (view instanceof TextView) {
if (!TextUtils.isEmpty(((TextView) view).getText())) {
layer ++;
}
}
if (view instanceof ImageView) {
if (((ImageView) view).getDrawable() != null) {
layer ++;
}
}
Drawable background = view.getBackground();
if (background != null) {
Drawable current = background.getCurrent();
if (current == null) {
// may be null e.g. StateListDrawable
return layer;
}
if (current instanceof ColorDrawable) {
int color = ((ColorDrawable) current).getColor();
// 有背景且背景为透明
if (color != 0) {
layer ++;
return layer;
}
}
Bitmap bitmap = drawableToBitmap(current);
if (bitmap == null) {
return layer;
}
if (!isBitmapTransparent(bitmap)) {
layer ++;
return layer;
}
}
return layer;
}
@Override
protected boolean verifyDrawable(Drawable who) {
return super.verifyDrawable(who) || mHasDraweeInText
// only schedule animation on AnimatableDrawable
&& (who instanceof ForwardingDrawable && who.getCurrent() instanceof Animatable);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
mBinding = ViewNowPlayingControlPanelBinding.inflate(inflater, container, false);
NowPlayingControllerViewModel viewModel = new NowPlayingControllerViewModel(getContext(),
getFragmentManager(), mPlayerController, mMusicStore, mPlaylistStore, mThemeStore);
mPlayerController.getCurrentPosition()
.compose(bindToLifecycle())
.subscribe(viewModel::setCurrentPosition,
throwable -> {
Timber.e(throwable, "failed to update position");
});
mPlayerController.getNowPlaying()
.compose(bindToLifecycle())
.subscribe(viewModel::setSong,
throwable -> Timber.e(throwable, "Failed to set song"));
mPlayerController.isPlaying()
.compose(bindToLifecycle())
.subscribe(viewModel::setPlaying,
throwable -> Timber.e(throwable, "Failed to set playing"));
mPlayerController.getDuration()
.compose(bindToLifecycle())
.subscribe(viewModel::setDuration,
throwable -> Timber.e(throwable, "Failed to set duration"));
mBinding.setViewModel(viewModel);
Drawable progress = mBinding.nowPlayingControllerScrubber.nowPlayingSeekBar.getProgressDrawable();
if (progress instanceof StateListDrawable) {
progress = progress.getCurrent();
}
if (progress instanceof LayerDrawable) {
((LayerDrawable) progress)
.findDrawableByLayerId(android.R.id.background)
.setColorFilter(Color.TRANSPARENT, PorterDuff.Mode.SRC_IN);
}
return mBinding.getRoot();
}
/**
* Similar to {@link #createBackgroundDrawable(int, int, int, boolean)} but with additional {@code original} drawable parameter.
*
* @param context Which context to use
* @param normal Color normal state of the drawable to this color
* @param clickedBackground Background color of the View that will show when view is clicked
* @param shouldFade Set to true if the state list should have a fading effect
* @param original This drawable will be contrasted to the {@code clickedBackground} color on press
* @return The state list drawable that is in contrast with the on-click background color
*/
@SuppressLint({
"InlinedApi", "NewApi"
})
public Drawable createContrastStateDrawable(Context context, int normal, int clickedBackground, boolean shouldFade, Drawable original) {
if (original == null || original instanceof StateListDrawable) {
if (original != null) {
Log.i(LOG_TAG, "Original drawable is already a StateListDrawable");
original = original.getCurrent();
}
// overridden in previous if clause, so check again
if (original == null) {
return null;
}
}
// init state arrays
int[] selectedState = new int[] {
android.R.attr.state_selected
};
int[] pressedState = new int[] {
android.R.attr.state_pressed
};
int[] checkedState = new int[] {
android.R.attr.state_checked
};
int[] activatedState = new int[] {};
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
activatedState = new int[] {
android.R.attr.state_activated
};
}
Drawable normalStateDrawable = colorDrawable(context, original, normal);
Drawable clickedStateDrawable = colorDrawable(context, original, getContrastColor(clickedBackground));
Drawable checkedStateDrawable = colorDrawable(context, original, getContrastColor(clickedBackground));
// prepare state list (order of adding states is important!)
StateListDrawable states = new StateListDrawable();
states.addState(pressedState, clickedStateDrawable);
if (!shouldFade) {
states.addState(selectedState, clickedStateDrawable);
states.addState(checkedState, checkedStateDrawable);
}
// add fade effect if applicable
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
if (shouldFade) {
states.addState(new int[] {}, normalStateDrawable);
states.setEnterFadeDuration(0);
states.setExitFadeDuration(FADE_DURATION);
} else {
states.addState(activatedState, clickedStateDrawable);
states.addState(new int[] {}, normalStateDrawable);
}
} else {
states.addState(new int[] {}, normalStateDrawable);
}
return states;
}
@Override
public boolean updateStyle(List<PXRuleSet> ruleSets, List<PXStylerContext> contexts) {
if (!super.updateStyle(ruleSets, contexts)) {
return false;
}
// Extract the existing compound icons
TextView styleable = (TextView) contexts.get(0).getStyleable();
Drawable[] compoundDrawables = styleable.getCompoundDrawables();
Drawable iconDrawable = compoundDrawables[position.ordinal()];
Map<int[], Drawable> existingStates = PXDrawableUtil.getExistingStates(iconDrawable);
Drawable newDrawable = null;
if (existingStates == null || existingStates.isEmpty()) {
// create a new StateListDrawable for that icon with the original
// style adapter
if (contexts.size() == 1) {
newDrawable = contexts.get(0).getBackgroundImage();
} else {
newDrawable = PXDrawableUtil.createNewStateListDrawable(
PXStyleAdapter.getStyleAdapter(styleable), ruleSets, contexts);
}
} else {
// create a drawable that will hold a merge of the existing states
// and the new states.
newDrawable = PXDrawableUtil.createDrawable(
PXStyleAdapter.getStyleAdapter(styleable), existingStates, ruleSets, contexts);
}
// Set the drawable.
if (newDrawable != null
&& !PXDrawableUtil.isEquals(newDrawable, compoundDrawables[position.ordinal()])) {
compoundDrawables[position.ordinal()] = newDrawable;
if (newDrawable instanceof DrawableContainer && newDrawable.getCurrent() == null) {
// We have to select a Drawable in the StateListDrawables.
// Otherwise, the bounds will not be set correctly.
DrawableContainer container = (DrawableContainer) newDrawable;
container.selectDrawable(0);
}
newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(),
newDrawable.getIntrinsicHeight());
styleable.setCompoundDrawables(compoundDrawables[0], compoundDrawables[1],
compoundDrawables[2], compoundDrawables[3]);
}
return true;
}