Java Code Examples for android.graphics.drawable.Drawable#getCurrent()

The following examples show how to use android.graphics.drawable.Drawable#getCurrent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From ProgressButton with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: CustomMatchers.java    From friendspell with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: AbsHListView.java    From letv with Apache License 2.0 5 votes vote down vote up
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());
            }
        }
    }
}
 
Example 4
Source File: GenericDraweeHierarchy.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source File: GenericDraweeHierarchy.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
  }
}
 
Example 6
Source File: TwoWayAbsListView.java    From recent-images with MIT License 5 votes vote down vote up
/**
 * 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());
		}
	}
}
 
Example 7
Source File: Carbon.java    From Carbon with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: AbsHListView.java    From Klyph with MIT License 5 votes vote down vote up
/**
 * 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() );
		}
	}
}
 
Example 9
Source File: ViewBgUtil.java    From AndroidGodEye with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 10
Source File: DraweeTextView.java    From drawee-text-view with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean verifyDrawable(Drawable who) {
    return super.verifyDrawable(who) || mHasDraweeInText
            // only schedule animation on AnimatableDrawable
            && (who instanceof ForwardingDrawable && who.getCurrent() instanceof Animatable);
}
 
Example 11
Source File: PlayerControllerFragment.java    From Jockey with Apache License 2.0 4 votes vote down vote up
@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();
}
 
Example 12
Source File: Coloring.java    From actual-number-picker with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 13
Source File: PXVirtualIconAdapter.java    From pixate-freestyle-android with Apache License 2.0 4 votes vote down vote up
@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;
}