Example usage for android.graphics.drawable Drawable getConstantState

List of usage examples for android.graphics.drawable Drawable getConstantState

Introduction

In this page you can find the example usage for android.graphics.drawable Drawable getConstantState.

Prototype

public @Nullable ConstantState getConstantState() 

Source Link

Document

Return a ConstantState instance that holds the shared state of this Drawable.

Usage

From source file:org.chromium.chrome.browser.omnibox.SuggestionView.java

/**
 * Constructs a new omnibox suggestion view.
 *
 * @param context The context used to construct the suggestion view.
 * @param locationBar The location bar showing these suggestions.
 *//*from w w  w .  jav  a  2 s  . c  o  m*/
public SuggestionView(Context context, LocationBar locationBar) {
    super(context);
    mLocationBar = locationBar;

    mSuggestionHeight = context.getResources().getDimensionPixelOffset(R.dimen.omnibox_suggestion_height);
    mSuggestionAnswerHeight = context.getResources()
            .getDimensionPixelOffset(R.dimen.omnibox_suggestion_answer_height);

    TypedArray a = getContext().obtainStyledAttributes(new int[] { R.attr.selectableItemBackground });
    Drawable itemBackground = a.getDrawable(0);
    a.recycle();

    mContentsView = new SuggestionContentsContainer(context, itemBackground);
    addView(mContentsView);

    mRefineView = new View(context) {
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            if (mRefineIcon == null)
                return;
            canvas.save();
            canvas.translate((getMeasuredWidth() - mRefineIcon.getIntrinsicWidth()) / 2f,
                    (getMeasuredHeight() - mRefineIcon.getIntrinsicHeight()) / 2f);
            mRefineIcon.draw(canvas);
            canvas.restore();
        }

        @Override
        public void setVisibility(int visibility) {
            super.setVisibility(visibility);

            if (visibility == VISIBLE) {
                setClickable(true);
                setFocusable(true);
            } else {
                setClickable(false);
                setFocusable(false);
            }
        }

        @Override
        protected void drawableStateChanged() {
            super.drawableStateChanged();

            if (mRefineIcon != null && mRefineIcon.isStateful()) {
                mRefineIcon.setState(getDrawableState());
            }
        }
    };
    mRefineView.setContentDescription(getContext().getString(R.string.accessibility_omnibox_btn_refine));

    // Although this has the same background as the suggestion view, it can not be shared as
    // it will result in the state of the drawable being shared and always showing up in the
    // refine view.
    mRefineView.setBackground(itemBackground.getConstantState().newDrawable());
    mRefineView.setId(R.id.refine_view_id);
    mRefineView.setClickable(true);
    mRefineView.setFocusable(true);
    mRefineView.setLayoutParams(new LayoutParams(0, 0));
    addView(mRefineView);

    mRefineWidth = getResources().getDimensionPixelSize(R.dimen.omnibox_suggestion_refine_width);

    mUrlBar = (UrlBar) locationBar.getContainerView().findViewById(R.id.url_bar);

    mPhoneUrlBarLeftOffsetPx = getResources()
            .getDimensionPixelOffset(R.dimen.omnibox_suggestion_phone_url_bar_left_offset);
    mPhoneUrlBarLeftOffsetRtlPx = getResources()
            .getDimensionPixelOffset(R.dimen.omnibox_suggestion_phone_url_bar_left_offset_rtl);
}

From source file:com.android.launcher2.Launcher.java

private Drawable.ConstantState updateButtonWithIconFromExternalActivity(int buttonId,
        ComponentName activityName, int fallbackDrawableId, String toolbarResourceName) {
    ImageView button = (ImageView) findViewById(buttonId);
    Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);

    if (button != null) {
        // If we were unable to find the icon via the meta-data, use a
        // generic one
        if (toolbarIcon == null) {
            button.setImageResource(fallbackDrawableId);
        } else {/* ww  w .j a  v a 2 s  .  c om*/
            button.setImageDrawable(toolbarIcon);
        }
    }

    return toolbarIcon != null ? toolbarIcon.getConstantState() : null;

}

From source file:com.android.launcher2.Launcher.java

private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(int buttonId,
        ComponentName activityName, int fallbackDrawableId, String toolbarResourceName) {
    Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
    Resources r = getResources();
    int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
    int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);

    TextView button = (TextView) findViewById(buttonId);
    // If we were unable to find the icon via the meta-data, use a generic one
    if (toolbarIcon == null) {
        toolbarIcon = r.getDrawable(fallbackDrawableId);
        toolbarIcon.setBounds(0, 0, w, h);
        if (button != null) {
            button.setCompoundDrawables(toolbarIcon, null, null, null);
        }//from   w ww  .jav a 2  s  .c o  m
        return null;
    } else {
        toolbarIcon.setBounds(0, 0, w, h);
        if (button != null) {
            button.setCompoundDrawables(toolbarIcon, null, null, null);
        }
        return toolbarIcon.getConstantState();
    }
}