Utilize StateListDrawable for key preview background

Change-Id: I268550a185701caecefc27ebaec40fb2c940d883
main
Tadashi G. Takaoka 2012-09-05 15:34:51 +09:00
parent 2a2b5edc21
commit e8c6d5a480
11 changed files with 127 additions and 127 deletions

View File

@ -14,9 +14,11 @@
limitations under the License.
-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_long_pressable="true"
android:drawable="@drawable/keyboard_key_feedback_more_background" />
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:latin="http://schemas.android.com/apk/res/com.android.inputmethod.latin"
>
<item latin:state_has_morekeys="true"
android:drawable="@drawable/keyboard_key_feedback_more_background" />
<item android:drawable="@drawable/keyboard_key_feedback_background" />
</selector>

View File

@ -14,8 +14,23 @@
limitations under the License.
-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_long_pressable="true"
android:drawable="@drawable/keyboard_key_feedback_more_background_holo" />
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:latin="http://schemas.android.com/apk/res/com.android.inputmethod.latin"
>
<!-- Left edge -->
<item latin:state_left_edge="true" latin:state_has_morekeys="true"
android:drawable="@drawable/keyboard_key_feedback_left_more_background_holo" />
<item latin:state_left_edge="true"
android:drawable="@drawable/keyboard_key_feedback_left_background_holo" />
<!-- Right edge -->
<item latin:state_right_edge="true" latin:state_has_morekeys="true"
android:drawable="@drawable/keyboard_key_feedback_right_more_background_holo" />
<item latin:state_right_edge="true"
android:drawable="@drawable/keyboard_key_feedback_right_background_holo" />
<item latin:state_has_morekeys="true"
android:drawable="@drawable/keyboard_key_feedback_more_background_holo" />
<item android:drawable="@drawable/keyboard_key_feedback_background_holo" />
</selector>

View File

@ -1,21 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2011 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_long_pressable="true"
android:drawable="@drawable/keyboard_key_feedback_left_more_background_holo" />
<item android:drawable="@drawable/keyboard_key_feedback_left_background_holo" />
</selector>

View File

@ -1,21 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2011 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_long_pressable="true"
android:drawable="@drawable/keyboard_key_feedback_right_more_background_holo" />
<item android:drawable="@drawable/keyboard_key_feedback_right_background_holo" />
</selector>

View File

@ -20,8 +20,8 @@
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="40dp"
android:layout_height="wrap_content"
android:background="@drawable/keyboard_key_feedback"
android:minWidth="32dp"
android:gravity="center"
/>
/>

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
/*
**
** Copyright 2012, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/keyboard_key_feedback_ics"
android:minWidth="32dp"
android:gravity="center"
/>

View File

@ -54,12 +54,10 @@
<!-- Layout resource for key press feedback.-->
<attr name="keyPreviewLayout" format="reference" />
<!-- The background for key press feedback. -->
<attr name="keyPreviewBackground" format="reference" />
<!-- The background for the left edge key press feedback. -->
<attr name="keyPreviewLeftBackground" format="reference" />
<!-- The background for the right edge key press feedback. -->
<attr name="keyPreviewRightBackground" format="reference" />
<!-- Key preview background states -->
<attr name="state_left_edge" format="boolean" />
<attr name="state_right_edge" format="boolean" />
<attr name="state_has_morekeys" format="boolean" />
<!-- Vertical offset of the key press feedback from the key. -->
<attr name="keyPreviewOffset" format="dimension" />
<!-- Height of the key press feedback popup. -->

View File

@ -54,9 +54,6 @@
<item name="keyPopupHintLetterPadding">@dimen/key_popup_hint_letter_padding</item>
<item name="keyShiftedLetterHintPadding">@dimen/key_uppercase_letter_padding</item>
<item name="keyPreviewLayout">@layout/key_preview</item>
<item name="keyPreviewBackground">@drawable/keyboard_key_feedback</item>
<item name="keyPreviewLeftBackground">@null</item>
<item name="keyPreviewRightBackground">@null</item>
<item name="keyPreviewTextColor">#FFFFFFFF</item>
<item name="keyPreviewOffset">@dimen/key_preview_offset</item>
<item name="keyPreviewHeight">@dimen/key_preview_height</item>
@ -323,9 +320,7 @@
<item name="keyHintLabelColor">#A0FFFFFF</item>
<item name="keyShiftedLetterHintInactivatedColor">#66E0E4E5</item>
<item name="keyShiftedLetterHintActivatedColor">#FFFFFFFF</item>
<item name="keyPreviewBackground">@drawable/keyboard_key_feedback_ics</item>
<item name="keyPreviewLeftBackground">@drawable/keyboard_key_feedback_left_ics</item>
<item name="keyPreviewRightBackground">@drawable/keyboard_key_feedback_right_ics</item>
<item name="keyPreviewLayout">@layout/key_preview_ics</item>
<item name="keyPreviewTextColor">#FFFFFFFF</item>
<item name="keyPreviewOffset">@dimen/key_preview_offset_ics</item>
<item name="keyTextShadowColor">#00000000</item>

View File

@ -59,9 +59,6 @@ import java.util.HashSet;
* @attr ref R.styleable#KeyboardView_keyBackground
* @attr ref R.styleable#KeyboardView_moreKeysLayout
* @attr ref R.styleable#KeyboardView_keyPreviewLayout
* @attr ref R.styleable#KeyboardView_keyPreviewBackground
* @attr ref R.styleable#KeyboardView_keyPreviewLeftBackground
* @attr ref R.styleable#KeyboardView_keyPreviewRightBackground
* @attr ref R.styleable#KeyboardView_keyPreviewOffset
* @attr ref R.styleable#KeyboardView_keyPreviewHeight
* @attr ref R.styleable#KeyboardView_keyPreviewLingerTimeout
@ -108,9 +105,6 @@ import java.util.HashSet;
public class KeyboardView extends View implements PointerTracker.DrawingProxy {
private static final String TAG = KeyboardView.class.getSimpleName();
// Miscellaneous constants
private static final int[] LONG_PRESSABLE_STATE_SET = { android.R.attr.state_long_pressable };
// XML attributes
private final KeyVisualAttributes mKeyVisualAttributes;
private final int mKeyLabelHorizontalPadding;
@ -139,12 +133,13 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
private Keyboard mKeyboard;
protected final KeyDrawParams mKeyDrawParams = new KeyDrawParams();
// Preview placer view
private final PreviewPlacerView mPreviewPlacerView;
private final int[] mCoordinates = new int[2];
// Key preview
private static final int PREVIEW_ALPHA = 240;
private final int mKeyPreviewLayoutId;
private final Drawable mPreviewBackground;
private final Drawable mPreviewLeftBackground;
private final Drawable mPreviewRightBackground;
private final int mPreviewOffset;
private final int mPreviewHeight;
private final int mPreviewLingerTimeout;
@ -152,7 +147,28 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
protected final KeyPreviewDrawParams mKeyPreviewDrawParams = new KeyPreviewDrawParams();
private boolean mShowKeyPreviewPopup = true;
private int mDelayAfterPreview;
private final PreviewPlacerView mPreviewPlacerView;
// Background state set
private static final int[][][] KEY_PREVIEW_BACKGROUND_STATE_TABLE = {
{ // STATE_MIDDLE
EMPTY_STATE_SET,
{ R.attr.state_has_morekeys }
},
{ // STATE_LEFT
{ R.attr.state_left_edge },
{ R.attr.state_left_edge, R.attr.state_has_morekeys }
},
{ // STATE_RIGHT
{ R.attr.state_right_edge },
{ R.attr.state_right_edge, R.attr.state_has_morekeys }
}
};
private static final int STATE_MIDDLE = 0;
private static final int STATE_LEFT = 1;
private static final int STATE_RIGHT = 2;
private static final int STATE_NORMAL = 0;
private static final int STATE_HAS_MOREKEYS = 1;
private static final int[] KEY_PREVIEW_BACKGROUND_DEFAULT_STATE =
KEY_PREVIEW_BACKGROUND_STATE_TABLE[STATE_MIDDLE][STATE_NORMAL];
// Drawing
/** True if the entire keyboard needs to be dimmed. */
@ -230,15 +246,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
R.styleable.KeyboardView, defStyle, R.style.KeyboardView);
mKeyBackground = keyboardViewAttr.getDrawable(R.styleable.KeyboardView_keyBackground);
mKeyBackground.getPadding(mKeyBackgroundPadding);
mPreviewBackground = keyboardViewAttr.getDrawable(
R.styleable.KeyboardView_keyPreviewBackground);
mPreviewLeftBackground = keyboardViewAttr.getDrawable(
R.styleable.KeyboardView_keyPreviewLeftBackground);
mPreviewRightBackground = keyboardViewAttr.getDrawable(
R.styleable.KeyboardView_keyPreviewRightBackground);
setAlpha(mPreviewBackground, PREVIEW_ALPHA);
setAlpha(mPreviewLeftBackground, PREVIEW_ALPHA);
setAlpha(mPreviewRightBackground, PREVIEW_ALPHA);
mPreviewOffset = keyboardViewAttr.getDimensionPixelOffset(
R.styleable.KeyboardView_keyPreviewOffset, 0);
mPreviewHeight = keyboardViewAttr.getDimensionPixelSize(
@ -278,11 +285,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
mPaint.setAntiAlias(true);
}
private static void setAlpha(final Drawable drawable, final int alpha) {
if (drawable == null) return;
drawable.setAlpha(alpha);
}
private static void blendAlpha(final Paint paint, final int alpha) {
final int color = paint.getColor();
paint.setARGB((paint.getAlpha() * alpha) / Constants.Color.ALPHA_OPAQUE,
@ -498,7 +500,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
}
// Draw key background.
protected void onDrawKeyBackground(Key key, Canvas canvas) {
protected void onDrawKeyBackground(final Key key, final Canvas canvas) {
final Rect padding = mKeyBackgroundPadding;
final int bgWidth = key.getDrawWidth() + padding.left + padding.right;
final int bgHeight = key.mHeight + padding.top + padding.bottom;
@ -520,7 +522,8 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
}
// Draw key top visuals.
protected void onDrawKeyTopVisuals(Key key, Canvas canvas, Paint paint, KeyDrawParams params) {
protected void onDrawKeyTopVisuals(final Key key, final Canvas canvas, final Paint paint,
final KeyDrawParams params) {
final int keyWidth = key.getDrawWidth();
final int keyHeight = key.mHeight;
final float centerX = keyWidth * 0.5f;
@ -676,7 +679,8 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
}
// Draw popup hint "..." at the bottom right corner of the key.
protected void drawKeyPopupHint(Key key, Canvas canvas, Paint paint, KeyDrawParams params) {
protected void drawKeyPopupHint(final Key key, final Canvas canvas, final Paint paint,
final KeyDrawParams params) {
final int keyWidth = key.getDrawWidth();
final int keyHeight = key.mHeight;
@ -696,7 +700,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
}
}
private static int getCharGeometryCacheKey(char referenceChar, Paint paint) {
private static int getCharGeometryCacheKey(final char referenceChar, final Paint paint) {
final int labelSize = (int)paint.getTextSize();
final Typeface face = paint.getTypeface();
final int codePointOffset = referenceChar << 15;
@ -714,7 +718,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
// Working variable for the following methods.
private final Rect mTextBounds = new Rect();
private float getCharHeight(char[] referenceChar, Paint paint) {
private float getCharHeight(final char[] referenceChar, final Paint paint) {
final int key = getCharGeometryCacheKey(referenceChar[0], paint);
final Float cachedValue = sTextHeightCache.get(key);
if (cachedValue != null)
@ -726,7 +730,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
return height;
}
private float getCharWidth(char[] referenceChar, Paint paint) {
private float getCharWidth(final char[] referenceChar, final Paint paint) {
final int key = getCharGeometryCacheKey(referenceChar[0], paint);
final Float cachedValue = sTextWidthCache.get(key);
if (cachedValue != null)
@ -738,36 +742,37 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
return width;
}
public float getLabelWidth(String label, Paint paint) {
paint.getTextBounds(label.toString(), 0, label.length(), mTextBounds);
public float getLabelWidth(final String label, final Paint paint) {
paint.getTextBounds(label, 0, label.length(), mTextBounds);
return mTextBounds.width();
}
protected static void drawIcon(Canvas canvas, Drawable icon, int x, int y, int width,
int height) {
protected static void drawIcon(final Canvas canvas, final Drawable icon, final int x,
final int y, final int width, final int height) {
canvas.translate(x, y);
icon.setBounds(0, 0, width, height);
icon.draw(canvas);
canvas.translate(-x, -y);
}
private static void drawHorizontalLine(Canvas canvas, float y, float w, int color,
Paint paint) {
private static void drawHorizontalLine(final Canvas canvas, final float y, final float w,
final int color, final Paint paint) {
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1.0f);
paint.setColor(color);
canvas.drawLine(0, y, w, y, paint);
}
private static void drawVerticalLine(Canvas canvas, float x, float h, int color, Paint paint) {
private static void drawVerticalLine(final Canvas canvas, final float x, final float h,
final int color, final Paint paint) {
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1.0f);
paint.setColor(color);
canvas.drawLine(x, 0, x, h, paint);
}
private static void drawRectangle(Canvas canvas, float x, float y, float w, float h, int color,
Paint paint) {
private static void drawRectangle(final Canvas canvas, final float x, final float y,
final float w, final float h, final int color, final Paint paint) {
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1.0f);
paint.setColor(color);
@ -849,7 +854,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
}
}
public void showGestureFloatingPreviewText(String gestureFloatingPreviewText) {
public void showGestureFloatingPreviewText(final String gestureFloatingPreviewText) {
locatePreviewPlacerView();
mPreviewPlacerView.setGestureFloatingPreviewText(gestureFloatingPreviewText);
}
@ -865,7 +870,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
mPreviewPlacerView.invalidatePointer(tracker);
}
@SuppressWarnings("deprecation") // setBackgroundDrawable is replaced by setBackground in API16
@Override
public void showKeyPreview(final PointerTracker tracker) {
final KeyPreviewDrawParams previewParams = mKeyPreviewDrawParams;
@ -886,10 +890,17 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
// If key is invalid or IME is already closed, we must not show key preview.
// Trying to show key preview while root window is closed causes
// WindowManager.BadTokenException.
if (key == null)
if (key == null) {
return;
}
final KeyDrawParams drawParams = mKeyDrawParams;
previewText.setTextColor(drawParams.mPreviewTextColor);
final Drawable background = previewText.getBackground();
if (background != null) {
background.setState(KEY_PREVIEW_BACKGROUND_DEFAULT_STATE);
background.setAlpha(PREVIEW_ALPHA);
}
final String label = key.isShiftedLetterActivated() ? key.mHintLabel : key.mLabel;
// What we show as preview should match what we show on a key top in onDraw().
if (label != null) {
@ -908,7 +919,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
key.getPreviewIcon(mKeyboard.mIconsSet));
previewText.setText(null);
}
previewText.setBackgroundDrawable(mPreviewBackground);
previewText.measure(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
@ -924,32 +934,29 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
// The distance between the top edge of the parent key and the bottom of the visible part
// of the key preview background.
previewParams.mPreviewVisibleOffset = mPreviewOffset - previewText.getPaddingBottom();
getLocationInWindow(previewParams.mCoordinates);
getLocationInWindow(mCoordinates);
// The key preview is horizontally aligned with the center of the visible part of the
// parent key. If it doesn't fit in this {@link KeyboardView}, it is moved inward to fit and
// the left/right background is used if such background is specified.
int previewX = key.getDrawX() - (previewWidth - keyDrawWidth) / 2
+ previewParams.mCoordinates[0];
final int statePosition;
int previewX = key.getDrawX() - (previewWidth - keyDrawWidth) / 2 + mCoordinates[0];
if (previewX < 0) {
previewX = 0;
if (mPreviewLeftBackground != null) {
previewText.setBackgroundDrawable(mPreviewLeftBackground);
}
statePosition = STATE_LEFT;
} else if (previewX > getWidth() - previewWidth) {
previewX = getWidth() - previewWidth;
if (mPreviewRightBackground != null) {
previewText.setBackgroundDrawable(mPreviewRightBackground);
}
statePosition = STATE_RIGHT;
} else {
statePosition = STATE_MIDDLE;
}
// The key preview is placed vertically above the top edge of the parent key with an
// arbitrary offset.
final int previewY = key.mY - previewHeight + mPreviewOffset
+ previewParams.mCoordinates[1];
final int previewY = key.mY - previewHeight + mPreviewOffset + mCoordinates[1];
// Set the preview background state
previewText.getBackground().setState(
key.mMoreKeys != null ? LONG_PRESSABLE_STATE_SET : EMPTY_STATE_SET);
previewText.setTextColor(drawParams.mPreviewTextColor);
if (background != null) {
final int hasMoreKeys = (key.mMoreKeys != null) ? STATE_HAS_MOREKEYS : STATE_NORMAL;
background.setState(KEY_PREVIEW_BACKGROUND_STATE_TABLE[statePosition][hasMoreKeys]);
}
ViewLayoutUtils.placeViewAt(
previewText, previewX, previewY, previewWidth, previewHeight);
previewText.setVisibility(VISIBLE);
@ -975,7 +982,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
* @see #invalidateAllKeys
*/
@Override
public void invalidateKey(Key key) {
public void invalidateKey(final Key key) {
if (mInvalidateAllKeys) return;
if (key == null) return;
mInvalidatedKeys.add(key);

View File

@ -20,7 +20,7 @@ import android.graphics.Typeface;
import com.android.inputmethod.latin.ResourceUtils;
public class KeyDrawParams {
public final class KeyDrawParams {
public Typeface mTypeface;
public int mLetterSize;

View File

@ -16,7 +16,7 @@
package com.android.inputmethod.keyboard.internal;
public class KeyPreviewDrawParams {
public final class KeyPreviewDrawParams {
// The graphical geometry of the key preview.
// <-width->
// +-------+ ^
@ -41,6 +41,4 @@ public class KeyPreviewDrawParams {
// offset between the top edge of parent key and the bottom of the visible part of key
// preview background.
public int mPreviewVisibleOffset;
public final int[] mCoordinates = new int[2];
}