Example usage for android.graphics.drawable Drawable getState

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

Introduction

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

Prototype

public @NonNull int[] getState() 

Source Link

Document

Describes the current state, as a union of primitve states, such as android.R.attr#state_focused , android.R.attr#state_selected , etc.

Usage

From source file:com.anysoftkeyboard.keyboards.views.AnyKeyboardBaseView.java

private void showKey(final int keyIndex, PointerTracker tracker) {
    Key key = tracker.getKey(keyIndex);
    if (key == null || !mShowPreview)
        return;//from  w  w w. j  a v a 2s  . co  m

    int popupWidth = 0;
    int popupHeight = 0;
    // Should not draw hint icon in key preview
    Drawable iconToDraw = getIconToDrawForKey(key, true);
    if (iconToDraw != null) {
        // Here's an annoying bug for you (explanation at the end of the
        // hack)
        mPreviewIcon.setImageState(iconToDraw.getState(), false);
        // end of hack. You see, the drawable comes with a state, this state
        // is overridden by the ImageView. No more.
        mPreviewIcon.setImageDrawable(iconToDraw);
        mPreviewIcon.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        popupWidth = Math.max(mPreviewIcon.getMeasuredWidth(), key.width);
        popupHeight = Math.max(mPreviewIcon.getMeasuredHeight(), key.height);
        mPreviewText.setText(null);
    } else {
        CharSequence label = tracker.getPreviewText(key, mKeyboard.isShifted());
        if (TextUtils.isEmpty(label)) {
            label = guessLabelForKey(key.codes[0]);
        }
        mPreviewIcon.setImageDrawable(null);
        mPreviewText.setTextColor(mPreviewKeyTextColor);
        setKeyPreviewText(key, label);
        popupWidth = Math.max(mPreviewText.getMeasuredWidth(), key.width);
        popupHeight = Math.max(mPreviewText.getMeasuredHeight(), key.height);
    }

    if (mPreviewPaddingHeight < 0) {
        mPreviewPaddingWidth = mPreviewLayut.getPaddingLeft() + mPreviewLayut.getPaddingRight();
        mPreviewPaddingHeight = mPreviewLayut.getPaddingTop() + mPreviewLayut.getPaddingBottom();

        if (mPreviewKeyBackground != null) {
            Rect padding = new Rect();
            mPreviewKeyBackground.getPadding(padding);
            mPreviewPaddingWidth += (padding.left + padding.right);
            mPreviewPaddingHeight += (padding.top + padding.bottom);
        }
    }
    popupWidth += mPreviewPaddingWidth;
    popupHeight += mPreviewPaddingHeight;

    // and checking that the width and height are big enough for the
    // background.
    if (mPreviewKeyBackground != null) {
        popupWidth = Math.max(mPreviewKeyBackground.getMinimumWidth(), popupWidth);
        popupHeight = Math.max(mPreviewKeyBackground.getMinimumHeight(), popupHeight);
    }

    final boolean showPopupAboveKey = AnyApplication.getConfig().showKeyPreviewAboveKey();
    int popupPreviewX = showPopupAboveKey ? key.x - ((popupWidth - key.width) / 2)
            : (getWidth() - popupWidth) / 2;
    int popupPreviewY = (showPopupAboveKey ? key.y : 0) - popupHeight - mPreviewOffset;

    mHandler.cancelDismissPreview();
    if (mOffsetInWindow == null) {
        mOffsetInWindow = new int[] { 0, 0 };
        getLocationInWindow(mOffsetInWindow);
        Log.d(TAG, "mOffsetInWindow " + mOffsetInWindow[0] + ", " + mOffsetInWindow[1]);
        mOffsetInWindow[0] += mPopupPreviewOffsetX; // Offset may be zero
        mOffsetInWindow[1] += mPopupPreviewOffsetY; // Offset may be zero
        int[] windowLocation = new int[2];
        getLocationOnScreen(windowLocation);
        mWindowY = windowLocation[1];
    }
    popupPreviewX += mOffsetInWindow[0];
    popupPreviewY += mOffsetInWindow[1];

    // If the popup cannot be shown above the key, put it on the side
    if (popupPreviewY + mWindowY < 0) {
        // If the key you're pressing is on the left side of the keyboard,
        // show the popup on
        // the right, offset by enough to see at least one key to the
        // left/right.
        if (key.x + key.width <= getWidth() / 2) {
            popupPreviewX += (int) (key.width * 2.5);
        } else {
            popupPreviewX -= (int) (key.width * 2.5);
        }
        popupPreviewY += popupHeight;
    }

    if (mPreviewPopup.isShowing()) {
        mPreviewPopup.update(popupPreviewX, popupPreviewY, popupWidth, popupHeight);
    } else {
        mPreviewPopup.setWidth(popupWidth);
        mPreviewPopup.setHeight(popupHeight);
        try {
            // https://github.com/AnySoftKeyboard/AnySoftKeyboard/issues/6
            // I don't understand why this should happen, and only with MIUI
            // ROMs.
            // anyhow, it easy to hide :)
            mPreviewPopup.showAtLocation(mMiniKeyboardParent, Gravity.NO_GRAVITY, popupPreviewX, popupPreviewY);
        } catch (RuntimeException e) {
            // nothing to do here. I think.
        }

    }
    // Record pop-up preview position to display mini-keyboard later at the
    // same position
    mPopupPreviewDisplayedY = popupPreviewY + (showPopupAboveKey ? 0 : key.y);// the popup keyboard should
    // be
    // placed at the right
    // position.
    // So I'm fixing
    mPreviewLayut.setVisibility(VISIBLE);

    // Set the preview background state
    if (mPreviewKeyBackground != null) {
        mPreviewKeyBackground.setState(key.popupResId != 0 ? LONG_PRESSABLE_STATE_SET : EMPTY_STATE_SET);
    }

    // LayoutParams lp = mPreviewLayut.getLayoutParams();
    // lp.width = popupWidth;
    mPreviewLayut.requestLayout();
    mPreviewLayut.invalidate();
}