2010-08-20 05:35:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 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.
|
|
|
|
*/
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
package com.android.inputmethod.keyboard;
|
2010-08-20 05:35:02 +00:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.TypedArray;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.Canvas;
|
2011-02-20 05:44:59 +00:00
|
|
|
import android.graphics.Color;
|
2010-08-20 05:35:02 +00:00
|
|
|
import android.graphics.Paint;
|
2010-08-24 13:46:36 +00:00
|
|
|
import android.graphics.Paint.Align;
|
2010-08-20 05:35:02 +00:00
|
|
|
import android.graphics.PorterDuff;
|
|
|
|
import android.graphics.Rect;
|
2012-08-02 06:10:59 +00:00
|
|
|
import android.graphics.Region;
|
2013-09-20 07:27:30 +00:00
|
|
|
import android.graphics.Typeface;
|
2010-08-20 05:35:02 +00:00
|
|
|
import android.graphics.drawable.Drawable;
|
2014-05-15 08:28:54 +00:00
|
|
|
import android.graphics.drawable.NinePatchDrawable;
|
2014-08-05 08:32:53 +00:00
|
|
|
import android.text.TextUtils;
|
2010-08-20 05:35:02 +00:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.View;
|
|
|
|
|
2012-08-30 07:18:53 +00:00
|
|
|
import com.android.inputmethod.keyboard.internal.KeyDrawParams;
|
2012-08-30 08:42:49 +00:00
|
|
|
import com.android.inputmethod.keyboard.internal.KeyVisualAttributes;
|
2012-07-19 08:20:54 +00:00
|
|
|
import com.android.inputmethod.latin.Constants;
|
2011-06-15 04:38:58 +00:00
|
|
|
import com.android.inputmethod.latin.R;
|
2013-07-05 09:57:07 +00:00
|
|
|
import com.android.inputmethod.latin.utils.TypefaceUtils;
|
2011-06-15 04:38:58 +00:00
|
|
|
|
2012-03-13 08:28:52 +00:00
|
|
|
import java.util.HashSet;
|
2010-08-20 05:35:02 +00:00
|
|
|
|
|
|
|
/**
|
2011-07-01 04:07:59 +00:00
|
|
|
* A view that renders a virtual {@link Keyboard}.
|
2010-08-20 05:35:02 +00:00
|
|
|
*
|
2010-12-02 09:46:21 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyBackground
|
2014-05-14 06:30:37 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_functionalKeyBackground
|
|
|
|
* @attr ref R.styleable#KeyboardView_spacebarBackground
|
2014-05-15 08:28:54 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_spacebarIconWidthRatio
|
2014-08-07 14:44:34 +00:00
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyLabelFlags
|
2011-06-28 07:32:39 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyHintLetterPadding
|
2014-08-05 08:32:53 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyPopupHintLetter
|
2011-09-07 12:56:42 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyPopupHintLetterPadding
|
2012-01-26 09:03:30 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyShiftedLetterHintPadding
|
2012-08-30 08:42:49 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyTextShadowRadius
|
2012-08-31 01:09:43 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_verticalCorrection
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyTypeface
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyLetterSize
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyLabelSize
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyLargeLetterRatio
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyLargeLabelRatio
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyHintLetterRatio
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyShiftedLetterHintRatio
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyHintLabelRatio
|
2014-08-07 14:44:34 +00:00
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyLabelOffCenterRatio
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyHintLabelOffCenterRatio
|
2012-08-31 01:09:43 +00:00
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyPreviewTextRatio
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyTextColor
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyTextColorDisabled
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyTextShadowColor
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyHintLetterColor
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyHintLabelColor
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyShiftedLetterHintInactivatedColor
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyShiftedLetterHintActivatedColor
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyPreviewTextColor
|
2010-08-20 05:35:02 +00:00
|
|
|
*/
|
2013-01-21 06:23:32 +00:00
|
|
|
public class KeyboardView extends View {
|
2011-08-31 05:22:53 +00:00
|
|
|
// XML attributes
|
2013-04-11 03:08:36 +00:00
|
|
|
private final KeyVisualAttributes mKeyVisualAttributes;
|
2014-08-07 14:44:34 +00:00
|
|
|
// Default keyLabelFlags from {@link KeyboardTheme}.
|
|
|
|
// Currently only "alignHintLabelToBottom" is supported.
|
|
|
|
private final int mDefaultKeyLabelFlags;
|
2012-08-31 01:09:43 +00:00
|
|
|
private final float mKeyHintLetterPadding;
|
2014-08-05 08:32:53 +00:00
|
|
|
private final String mKeyPopupHintLetter;
|
2012-08-31 01:09:43 +00:00
|
|
|
private final float mKeyPopupHintLetterPadding;
|
|
|
|
private final float mKeyShiftedLetterHintPadding;
|
2012-08-30 08:42:49 +00:00
|
|
|
private final float mKeyTextShadowRadius;
|
2013-04-11 03:08:36 +00:00
|
|
|
private final float mVerticalCorrection;
|
|
|
|
private final Drawable mKeyBackground;
|
2014-05-14 06:30:37 +00:00
|
|
|
private final Drawable mFunctionalKeyBackground;
|
|
|
|
private final Drawable mSpacebarBackground;
|
2014-05-15 08:28:54 +00:00
|
|
|
private final float mSpacebarIconWidthRatio;
|
2013-04-11 03:08:36 +00:00
|
|
|
private final Rect mKeyBackgroundPadding = new Rect();
|
2014-05-13 02:19:43 +00:00
|
|
|
private static final float KET_TEXT_SHADOW_RADIUS_DISABLED = -1.0f;
|
2010-08-27 12:23:51 +00:00
|
|
|
|
2011-09-12 07:29:24 +00:00
|
|
|
// The maximum key label width in the proportion to the key width.
|
|
|
|
private static final float MAX_LABEL_RATIO = 0.90f;
|
|
|
|
|
2010-08-27 12:23:51 +00:00
|
|
|
// Main keyboard
|
2010-12-02 09:46:21 +00:00
|
|
|
private Keyboard mKeyboard;
|
2012-08-30 08:42:49 +00:00
|
|
|
protected final KeyDrawParams mKeyDrawParams = new KeyDrawParams();
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2010-08-27 12:23:51 +00:00
|
|
|
// Drawing
|
2012-03-13 08:28:52 +00:00
|
|
|
/** True if all keys should be drawn */
|
|
|
|
private boolean mInvalidateAllKeys;
|
|
|
|
/** The keys that should be drawn */
|
2014-05-23 11:18:17 +00:00
|
|
|
private final HashSet<Key> mInvalidatedKeys = new HashSet<>();
|
2012-08-02 06:10:59 +00:00
|
|
|
/** The working rectangle variable */
|
|
|
|
private final Rect mWorkingRect = new Rect();
|
2011-07-21 06:57:00 +00:00
|
|
|
/** The keyboard bitmap buffer for faster updates */
|
2012-08-02 07:39:27 +00:00
|
|
|
/** The clip region to draw keys */
|
|
|
|
private final Region mClipRegion = new Region();
|
2012-08-02 06:10:59 +00:00
|
|
|
private Bitmap mOffscreenBuffer;
|
2010-08-20 05:35:02 +00:00
|
|
|
/** The canvas for the above mutable keyboard bitmap */
|
2012-09-12 07:28:54 +00:00
|
|
|
private final Canvas mOffscreenCanvas = new Canvas();
|
2011-06-15 03:36:53 +00:00
|
|
|
private final Paint mPaint = new Paint();
|
2012-03-27 08:47:33 +00:00
|
|
|
private final Paint.FontMetrics mFontMetrics = new Paint.FontMetrics();
|
2012-08-30 08:42:49 +00:00
|
|
|
public KeyboardView(final Context context, final AttributeSet attrs) {
|
2010-08-20 05:35:02 +00:00
|
|
|
this(context, attrs, R.attr.keyboardViewStyle);
|
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public KeyboardView(final Context context, final AttributeSet attrs, final int defStyle) {
|
2010-08-20 05:35:02 +00:00
|
|
|
super(context, attrs, defStyle);
|
|
|
|
|
2012-08-30 07:05:40 +00:00
|
|
|
final TypedArray keyboardViewAttr = context.obtainStyledAttributes(attrs,
|
|
|
|
R.styleable.KeyboardView, defStyle, R.style.KeyboardView);
|
2012-08-30 08:42:49 +00:00
|
|
|
mKeyBackground = keyboardViewAttr.getDrawable(R.styleable.KeyboardView_keyBackground);
|
|
|
|
mKeyBackground.getPadding(mKeyBackgroundPadding);
|
2014-05-14 06:30:37 +00:00
|
|
|
final Drawable functionalKeyBackground = keyboardViewAttr.getDrawable(
|
|
|
|
R.styleable.KeyboardView_functionalKeyBackground);
|
|
|
|
mFunctionalKeyBackground = (functionalKeyBackground != null) ? functionalKeyBackground
|
|
|
|
: mKeyBackground;
|
|
|
|
final Drawable spacebarBackground = keyboardViewAttr.getDrawable(
|
|
|
|
R.styleable.KeyboardView_spacebarBackground);
|
2014-05-15 08:28:54 +00:00
|
|
|
mSpacebarBackground = (spacebarBackground != null) ? spacebarBackground : mKeyBackground;
|
|
|
|
mSpacebarIconWidthRatio = keyboardViewAttr.getFloat(
|
|
|
|
R.styleable.KeyboardView_spacebarIconWidthRatio, 1.0f);
|
2012-08-31 10:57:25 +00:00
|
|
|
mKeyHintLetterPadding = keyboardViewAttr.getDimension(
|
2013-04-11 03:08:36 +00:00
|
|
|
R.styleable.KeyboardView_keyHintLetterPadding, 0.0f);
|
2014-08-05 08:32:53 +00:00
|
|
|
mKeyPopupHintLetter = keyboardViewAttr.getString(
|
|
|
|
R.styleable.KeyboardView_keyPopupHintLetter);
|
2012-08-31 10:57:25 +00:00
|
|
|
mKeyPopupHintLetterPadding = keyboardViewAttr.getDimension(
|
2013-04-11 03:08:36 +00:00
|
|
|
R.styleable.KeyboardView_keyPopupHintLetterPadding, 0.0f);
|
2012-08-31 10:57:25 +00:00
|
|
|
mKeyShiftedLetterHintPadding = keyboardViewAttr.getDimension(
|
2013-04-11 03:08:36 +00:00
|
|
|
R.styleable.KeyboardView_keyShiftedLetterHintPadding, 0.0f);
|
2012-08-30 08:42:49 +00:00
|
|
|
mKeyTextShadowRadius = keyboardViewAttr.getFloat(
|
2014-05-13 02:19:43 +00:00
|
|
|
R.styleable.KeyboardView_keyTextShadowRadius, KET_TEXT_SHADOW_RADIUS_DISABLED);
|
2012-08-30 08:42:49 +00:00
|
|
|
mVerticalCorrection = keyboardViewAttr.getDimension(
|
2013-04-11 03:08:36 +00:00
|
|
|
R.styleable.KeyboardView_verticalCorrection, 0.0f);
|
2012-08-30 07:05:40 +00:00
|
|
|
keyboardViewAttr.recycle();
|
2012-08-30 08:42:49 +00:00
|
|
|
|
|
|
|
final TypedArray keyAttr = context.obtainStyledAttributes(attrs,
|
|
|
|
R.styleable.Keyboard_Key, defStyle, R.style.KeyboardView);
|
2014-08-07 14:44:34 +00:00
|
|
|
mDefaultKeyLabelFlags = keyAttr.getInt(R.styleable.Keyboard_Key_keyLabelFlags, 0);
|
2012-08-30 08:42:49 +00:00
|
|
|
mKeyVisualAttributes = KeyVisualAttributes.newInstance(keyAttr);
|
2012-08-30 07:05:40 +00:00
|
|
|
keyAttr.recycle();
|
2010-08-20 05:35:02 +00:00
|
|
|
|
|
|
|
mPaint.setAntiAlias(true);
|
|
|
|
}
|
|
|
|
|
2014-02-24 08:24:49 +00:00
|
|
|
public KeyVisualAttributes getKeyVisualAttribute() {
|
|
|
|
return mKeyVisualAttributes;
|
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
private static void blendAlpha(final Paint paint, final int alpha) {
|
|
|
|
final int color = paint.getColor();
|
|
|
|
paint.setARGB((paint.getAlpha() * alpha) / Constants.Color.ALPHA_OPAQUE,
|
|
|
|
Color.red(color), Color.green(color), Color.blue(color));
|
|
|
|
}
|
|
|
|
|
2013-08-01 15:25:05 +00:00
|
|
|
public void setHardwareAcceleratedDrawingEnabled(final boolean enabled) {
|
|
|
|
if (!enabled) return;
|
|
|
|
// TODO: Should use LAYER_TYPE_SOFTWARE when hardware acceleration is off?
|
2013-08-02 11:22:51 +00:00
|
|
|
setLayerType(LAYER_TYPE_HARDWARE, null);
|
2013-08-01 15:25:05 +00:00
|
|
|
}
|
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
/**
|
|
|
|
* Attaches a keyboard to this view. The keyboard can be switched at any time and the
|
|
|
|
* view will re-layout itself to accommodate the keyboard.
|
2010-12-02 09:46:21 +00:00
|
|
|
* @see Keyboard
|
2010-08-20 05:35:02 +00:00
|
|
|
* @see #getKeyboard()
|
|
|
|
* @param keyboard the keyboard to display in this view
|
|
|
|
*/
|
2012-08-30 08:42:49 +00:00
|
|
|
public void setKeyboard(final Keyboard keyboard) {
|
2010-08-20 05:35:02 +00:00
|
|
|
mKeyboard = keyboard;
|
2012-08-30 08:42:49 +00:00
|
|
|
final int keyHeight = keyboard.mMostCommonKeyHeight - keyboard.mVerticalGap;
|
|
|
|
mKeyDrawParams.updateParams(keyHeight, mKeyVisualAttributes);
|
|
|
|
mKeyDrawParams.updateParams(keyHeight, keyboard.mKeyVisualAttributes);
|
2013-01-22 05:32:47 +00:00
|
|
|
invalidateAllKeys();
|
|
|
|
requestLayout();
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current keyboard being displayed by this view.
|
|
|
|
* @return the currently attached keyboard
|
2010-12-02 09:46:21 +00:00
|
|
|
* @see #setKeyboard(Keyboard)
|
2010-08-20 05:35:02 +00:00
|
|
|
*/
|
2010-12-02 09:46:21 +00:00
|
|
|
public Keyboard getKeyboard() {
|
2010-08-20 05:35:02 +00:00
|
|
|
return mKeyboard;
|
|
|
|
}
|
|
|
|
|
2013-04-11 03:08:36 +00:00
|
|
|
protected float getVerticalCorrection() {
|
|
|
|
return mVerticalCorrection;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void updateKeyDrawParams(final int keyHeight) {
|
|
|
|
mKeyDrawParams.updateParams(keyHeight, mKeyVisualAttributes);
|
|
|
|
}
|
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
@Override
|
2012-08-30 08:42:49 +00:00
|
|
|
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
|
2013-08-08 10:14:55 +00:00
|
|
|
if (mKeyboard == null) {
|
2011-07-21 06:57:00 +00:00
|
|
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
2013-08-08 10:14:55 +00:00
|
|
|
return;
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
2013-08-08 10:14:55 +00:00
|
|
|
// The main keyboard expands to the entire this {@link KeyboardView}.
|
|
|
|
final int width = mKeyboard.mOccupiedWidth + getPaddingLeft() + getPaddingRight();
|
|
|
|
final int height = mKeyboard.mOccupiedHeight + getPaddingTop() + getPaddingBottom();
|
|
|
|
setMeasuredDimension(width, height);
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-01-21 07:36:25 +00:00
|
|
|
protected void onDraw(final Canvas canvas) {
|
2010-08-20 05:35:02 +00:00
|
|
|
super.onDraw(canvas);
|
2012-08-02 07:39:27 +00:00
|
|
|
if (canvas.isHardwareAccelerated()) {
|
|
|
|
onDrawKeyboard(canvas);
|
|
|
|
return;
|
|
|
|
}
|
2012-08-09 06:25:01 +00:00
|
|
|
|
|
|
|
final boolean bufferNeedsUpdates = mInvalidateAllKeys || !mInvalidatedKeys.isEmpty();
|
|
|
|
if (bufferNeedsUpdates || mOffscreenBuffer == null) {
|
2012-08-02 06:10:59 +00:00
|
|
|
if (maybeAllocateOffscreenBuffer()) {
|
|
|
|
mInvalidateAllKeys = true;
|
2012-09-12 07:28:54 +00:00
|
|
|
// TODO: Stop using the offscreen canvas even when in software rendering
|
|
|
|
mOffscreenCanvas.setBitmap(mOffscreenBuffer);
|
2012-08-02 06:10:59 +00:00
|
|
|
}
|
|
|
|
onDrawKeyboard(mOffscreenCanvas);
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
2013-04-11 03:08:36 +00:00
|
|
|
canvas.drawBitmap(mOffscreenBuffer, 0.0f, 0.0f, null);
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
2012-08-02 06:10:59 +00:00
|
|
|
private boolean maybeAllocateOffscreenBuffer() {
|
2011-02-01 05:02:22 +00:00
|
|
|
final int width = getWidth();
|
|
|
|
final int height = getHeight();
|
2012-08-02 06:10:59 +00:00
|
|
|
if (width == 0 || height == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (mOffscreenBuffer != null && mOffscreenBuffer.getWidth() == width
|
|
|
|
&& mOffscreenBuffer.getHeight() == height) {
|
|
|
|
return false;
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
2012-08-02 06:10:59 +00:00
|
|
|
freeOffscreenBuffer();
|
|
|
|
mOffscreenBuffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
|
|
|
return true;
|
|
|
|
}
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2012-08-02 06:10:59 +00:00
|
|
|
private void freeOffscreenBuffer() {
|
2013-10-08 01:56:32 +00:00
|
|
|
mOffscreenCanvas.setBitmap(null);
|
|
|
|
mOffscreenCanvas.setMatrix(null);
|
2012-08-02 06:10:59 +00:00
|
|
|
if (mOffscreenBuffer != null) {
|
|
|
|
mOffscreenBuffer.recycle();
|
|
|
|
mOffscreenBuffer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onDrawKeyboard(final Canvas canvas) {
|
2010-08-20 05:35:02 +00:00
|
|
|
if (mKeyboard == null) return;
|
|
|
|
|
2012-08-02 06:10:59 +00:00
|
|
|
final int width = getWidth();
|
|
|
|
final int height = getHeight();
|
2012-03-13 08:28:52 +00:00
|
|
|
final Paint paint = mPaint;
|
|
|
|
|
2012-08-02 07:39:27 +00:00
|
|
|
// Calculate clip region and set.
|
|
|
|
final boolean drawAllKeys = mInvalidateAllKeys || mInvalidatedKeys.isEmpty();
|
|
|
|
final boolean isHardwareAccelerated = canvas.isHardwareAccelerated();
|
|
|
|
// TODO: Confirm if it's really required to draw all keys when hardware acceleration is on.
|
|
|
|
if (drawAllKeys || isHardwareAccelerated) {
|
|
|
|
mClipRegion.set(0, 0, width, height);
|
|
|
|
} else {
|
|
|
|
mClipRegion.setEmpty();
|
|
|
|
for (final Key key : mInvalidatedKeys) {
|
|
|
|
if (mKeyboard.hasKey(key)) {
|
2013-08-12 09:05:11 +00:00
|
|
|
final int x = key.getX() + getPaddingLeft();
|
|
|
|
final int y = key.getY() + getPaddingTop();
|
|
|
|
mWorkingRect.set(x, y, x + key.getWidth(), y + key.getHeight());
|
2012-08-02 07:39:27 +00:00
|
|
|
mClipRegion.union(mWorkingRect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isHardwareAccelerated) {
|
|
|
|
canvas.clipRegion(mClipRegion, Region.Op.REPLACE);
|
2012-08-09 06:25:01 +00:00
|
|
|
// Draw keyboard background.
|
|
|
|
canvas.drawColor(Color.BLACK, PorterDuff.Mode.CLEAR);
|
|
|
|
final Drawable background = getBackground();
|
|
|
|
if (background != null) {
|
|
|
|
background.draw(canvas);
|
|
|
|
}
|
2012-08-02 07:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Confirm if it's really required to draw all keys when hardware acceleration is on.
|
|
|
|
if (drawAllKeys || isHardwareAccelerated) {
|
2011-04-15 04:05:58 +00:00
|
|
|
// Draw all keys.
|
2014-04-21 21:41:57 +00:00
|
|
|
for (final Key key : mKeyboard.getSortedKeys()) {
|
2012-08-30 08:42:49 +00:00
|
|
|
onDrawKey(key, canvas, paint);
|
2012-03-13 08:28:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Draw invalidated keys.
|
|
|
|
for (final Key key : mInvalidatedKeys) {
|
2012-08-02 07:39:27 +00:00
|
|
|
if (mKeyboard.hasKey(key)) {
|
2012-08-30 08:42:49 +00:00
|
|
|
onDrawKey(key, canvas, paint);
|
2012-04-18 05:54:40 +00:00
|
|
|
}
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
}
|
2010-11-05 09:41:12 +00:00
|
|
|
|
2012-03-13 08:28:52 +00:00
|
|
|
mInvalidatedKeys.clear();
|
|
|
|
mInvalidateAllKeys = false;
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
private void onDrawKey(final Key key, final Canvas canvas, final Paint paint) {
|
2012-08-28 08:19:49 +00:00
|
|
|
final int keyDrawX = key.getDrawX() + getPaddingLeft();
|
2013-08-12 09:05:11 +00:00
|
|
|
final int keyDrawY = key.getY() + getPaddingTop();
|
2012-03-13 08:28:52 +00:00
|
|
|
canvas.translate(keyDrawX, keyDrawY);
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
final int keyHeight = mKeyboard.mMostCommonKeyHeight - mKeyboard.mVerticalGap;
|
2013-08-12 09:05:11 +00:00
|
|
|
final KeyVisualAttributes attr = key.getVisualAttributes();
|
2012-08-30 08:42:49 +00:00
|
|
|
final KeyDrawParams params = mKeyDrawParams.mayCloneAndUpdateParams(keyHeight, attr);
|
2012-07-19 08:20:54 +00:00
|
|
|
params.mAnimAlpha = Constants.Color.ALPHA_OPAQUE;
|
2012-08-30 08:42:49 +00:00
|
|
|
|
2012-02-02 15:18:47 +00:00
|
|
|
if (!key.isSpacer()) {
|
2014-05-14 06:30:37 +00:00
|
|
|
final Drawable background = key.selectBackgroundDrawable(
|
|
|
|
mKeyBackground, mFunctionalKeyBackground, mSpacebarBackground);
|
|
|
|
onDrawKeyBackground(key, canvas, background);
|
2012-02-02 15:18:47 +00:00
|
|
|
}
|
2011-12-17 17:58:13 +00:00
|
|
|
onDrawKeyTopVisuals(key, canvas, paint, params);
|
2012-03-13 08:28:52 +00:00
|
|
|
|
|
|
|
canvas.translate(-keyDrawX, -keyDrawY);
|
2011-12-17 17:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw key background.
|
2013-12-13 08:09:16 +00:00
|
|
|
protected void onDrawKeyBackground(final Key key, final Canvas canvas,
|
|
|
|
final Drawable background) {
|
2012-08-30 08:42:49 +00:00
|
|
|
final Rect padding = mKeyBackgroundPadding;
|
|
|
|
final int bgWidth = key.getDrawWidth() + padding.left + padding.right;
|
2013-08-12 09:05:11 +00:00
|
|
|
final int bgHeight = key.getHeight() + padding.top + padding.bottom;
|
2012-08-30 08:42:49 +00:00
|
|
|
final int bgX = -padding.left;
|
|
|
|
final int bgY = -padding.top;
|
2011-12-17 17:58:13 +00:00
|
|
|
final Rect bounds = background.getBounds();
|
|
|
|
if (bgWidth != bounds.right || bgHeight != bounds.bottom) {
|
|
|
|
background.setBounds(0, 0, bgWidth, bgHeight);
|
2011-06-28 07:32:39 +00:00
|
|
|
}
|
2011-12-17 17:58:13 +00:00
|
|
|
canvas.translate(bgX, bgY);
|
|
|
|
background.draw(canvas);
|
|
|
|
canvas.translate(-bgX, -bgY);
|
|
|
|
}
|
2011-06-28 07:32:39 +00:00
|
|
|
|
2011-12-17 17:58:13 +00:00
|
|
|
// Draw key top visuals.
|
2012-09-05 06:34:51 +00:00
|
|
|
protected void onDrawKeyTopVisuals(final Key key, final Canvas canvas, final Paint paint,
|
|
|
|
final KeyDrawParams params) {
|
2012-08-28 08:19:49 +00:00
|
|
|
final int keyWidth = key.getDrawWidth();
|
2013-08-12 09:05:11 +00:00
|
|
|
final int keyHeight = key.getHeight();
|
2011-06-28 07:32:39 +00:00
|
|
|
final float centerX = keyWidth * 0.5f;
|
|
|
|
final float centerY = keyHeight * 0.5f;
|
|
|
|
|
2011-04-15 04:05:58 +00:00
|
|
|
// Draw key label.
|
2012-05-16 08:41:56 +00:00
|
|
|
final Drawable icon = key.getIcon(mKeyboard.mIconsSet, params.mAnimAlpha);
|
2014-08-07 14:44:34 +00:00
|
|
|
float labelX = centerX;
|
|
|
|
float labelBaseline = centerY;
|
2013-08-12 09:05:11 +00:00
|
|
|
final String label = key.getLabel();
|
|
|
|
if (label != null) {
|
2012-08-30 08:42:49 +00:00
|
|
|
paint.setTypeface(key.selectTypeface(params));
|
|
|
|
paint.setTextSize(key.selectTextSize(params));
|
2013-12-13 08:09:16 +00:00
|
|
|
final float labelCharHeight = TypefaceUtils.getReferenceCharHeight(paint);
|
|
|
|
final float labelCharWidth = TypefaceUtils.getReferenceCharWidth(paint);
|
2011-04-15 04:05:58 +00:00
|
|
|
|
|
|
|
// Vertical label text alignment.
|
2014-08-07 14:44:34 +00:00
|
|
|
labelBaseline = centerY + labelCharHeight / 2.0f;
|
2011-06-25 10:38:55 +00:00
|
|
|
|
2011-04-15 04:05:58 +00:00
|
|
|
// Horizontal label text alignment
|
2014-08-07 14:31:47 +00:00
|
|
|
if (key.isAlignLabelOffCenter()) {
|
|
|
|
// The label is placed off center of the key. Used mainly on "phone number" layout.
|
2014-08-07 14:44:34 +00:00
|
|
|
labelX = centerX + params.mLabelOffCenterRatio * labelCharWidth;
|
2011-06-25 10:38:55 +00:00
|
|
|
paint.setTextAlign(Align.LEFT);
|
2011-04-15 04:05:58 +00:00
|
|
|
} else {
|
2014-08-07 14:44:34 +00:00
|
|
|
labelX = centerX;
|
2011-04-15 04:05:58 +00:00
|
|
|
paint.setTextAlign(Align.CENTER);
|
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
if (key.needsAutoXScale()) {
|
|
|
|
final float ratio = Math.min(1.0f, (keyWidth * MAX_LABEL_RATIO) /
|
|
|
|
TypefaceUtils.getStringWidth(label, paint));
|
|
|
|
if (key.needsAutoScale()) {
|
|
|
|
final float autoSize = paint.getTextSize() * ratio;
|
|
|
|
paint.setTextSize(autoSize);
|
|
|
|
} else {
|
|
|
|
paint.setTextScaleX(ratio);
|
|
|
|
}
|
2011-09-12 07:29:24 +00:00
|
|
|
}
|
2011-06-25 10:38:55 +00:00
|
|
|
|
2011-06-23 12:23:44 +00:00
|
|
|
if (key.isEnabled()) {
|
2014-05-13 02:19:43 +00:00
|
|
|
paint.setColor(key.selectTextColor(params));
|
|
|
|
// Set a drop shadow for the text if the shadow radius is positive value.
|
|
|
|
if (mKeyTextShadowRadius > 0.0f) {
|
|
|
|
paint.setShadowLayer(mKeyTextShadowRadius, 0.0f, 0.0f, params.mTextShadowColor);
|
|
|
|
} else {
|
|
|
|
paint.clearShadowLayer();
|
|
|
|
}
|
2011-04-15 04:05:58 +00:00
|
|
|
} else {
|
|
|
|
// Make label invisible
|
|
|
|
paint.setColor(Color.TRANSPARENT);
|
2014-05-13 02:19:43 +00:00
|
|
|
paint.clearShadowLayer();
|
2011-04-15 04:05:58 +00:00
|
|
|
}
|
2012-08-30 08:42:49 +00:00
|
|
|
blendAlpha(paint, params.mAnimAlpha);
|
2014-08-07 14:44:34 +00:00
|
|
|
canvas.drawText(label, 0, label.length(), labelX, labelBaseline, paint);
|
2011-09-12 07:29:24 +00:00
|
|
|
// Turn off drop shadow and reset x-scale.
|
2014-05-13 02:19:43 +00:00
|
|
|
paint.clearShadowLayer();
|
2011-09-12 07:29:24 +00:00
|
|
|
paint.setTextScaleX(1.0f);
|
2011-04-15 04:05:58 +00:00
|
|
|
}
|
|
|
|
|
2011-06-25 10:38:55 +00:00
|
|
|
// Draw hint label.
|
2013-08-12 09:05:11 +00:00
|
|
|
final String hintLabel = key.getHintLabel();
|
|
|
|
if (hintLabel != null) {
|
2012-08-30 08:42:49 +00:00
|
|
|
paint.setTextSize(key.selectHintTextSize(params));
|
|
|
|
paint.setColor(key.selectHintTextColor(params));
|
2013-09-20 07:27:30 +00:00
|
|
|
// TODO: Should add a way to specify type face for hint letters
|
|
|
|
paint.setTypeface(Typeface.DEFAULT_BOLD);
|
2012-08-30 08:42:49 +00:00
|
|
|
blendAlpha(paint, params.mAnimAlpha);
|
2013-12-13 08:09:16 +00:00
|
|
|
final float labelCharHeight = TypefaceUtils.getReferenceCharHeight(paint);
|
|
|
|
final float labelCharWidth = TypefaceUtils.getReferenceCharWidth(paint);
|
2014-08-07 14:44:34 +00:00
|
|
|
final float hintX, hintBaseline;
|
2011-06-25 10:38:55 +00:00
|
|
|
if (key.hasHintLabel()) {
|
2011-09-07 03:07:56 +00:00
|
|
|
// The hint label is placed just right of the key label. Used mainly on
|
|
|
|
// "phone number" layout.
|
2014-08-07 14:44:34 +00:00
|
|
|
hintX = labelX + params.mHintLabelOffCenterRatio * labelCharWidth;
|
|
|
|
if (key.isAlignHintLabelToBottom(mDefaultKeyLabelFlags)) {
|
|
|
|
hintBaseline = labelBaseline;
|
|
|
|
} else {
|
|
|
|
hintBaseline = centerY + labelCharHeight / 2.0f;
|
|
|
|
}
|
2011-06-28 07:32:39 +00:00
|
|
|
paint.setTextAlign(Align.LEFT);
|
2012-01-26 09:03:30 +00:00
|
|
|
} else if (key.hasShiftedLetterHint()) {
|
2011-09-07 03:07:56 +00:00
|
|
|
// The hint label is placed at top-right corner of the key. Used mainly on tablet.
|
2013-12-13 08:09:16 +00:00
|
|
|
hintX = keyWidth - mKeyShiftedLetterHintPadding - labelCharWidth / 2.0f;
|
2012-03-27 08:47:33 +00:00
|
|
|
paint.getFontMetrics(mFontMetrics);
|
2014-08-07 14:44:34 +00:00
|
|
|
hintBaseline = -mFontMetrics.top;
|
2011-06-28 07:32:39 +00:00
|
|
|
paint.setTextAlign(Align.CENTER);
|
|
|
|
} else { // key.hasHintLetter()
|
2012-04-24 03:59:33 +00:00
|
|
|
// The hint letter is placed at top-right corner of the key. Used mainly on phone.
|
2013-12-13 08:09:16 +00:00
|
|
|
final float hintDigitWidth = TypefaceUtils.getReferenceDigitWidth(paint);
|
|
|
|
final float hintLabelWidth = TypefaceUtils.getStringWidth(hintLabel, paint);
|
2012-08-31 01:09:43 +00:00
|
|
|
hintX = keyWidth - mKeyHintLetterPadding
|
2013-12-13 08:09:16 +00:00
|
|
|
- Math.max(hintDigitWidth, hintLabelWidth) / 2.0f;
|
2014-08-07 14:44:34 +00:00
|
|
|
hintBaseline = -paint.ascent();
|
2011-06-28 07:32:39 +00:00
|
|
|
paint.setTextAlign(Align.CENTER);
|
2011-06-25 10:38:55 +00:00
|
|
|
}
|
2014-08-07 14:44:34 +00:00
|
|
|
final float adjustmentY = params.mHintLabelVerticalAdjustment * labelCharHeight;
|
|
|
|
canvas.drawText(
|
|
|
|
hintLabel, 0, hintLabel.length(), hintX, hintBaseline + adjustmentY, paint);
|
2011-06-15 03:36:53 +00:00
|
|
|
}
|
|
|
|
|
2011-04-15 04:05:58 +00:00
|
|
|
// Draw key icon.
|
2013-08-12 09:05:11 +00:00
|
|
|
if (label == null && icon != null) {
|
2014-05-15 08:28:54 +00:00
|
|
|
final int iconWidth;
|
|
|
|
if (key.getCode() == Constants.CODE_SPACE && icon instanceof NinePatchDrawable) {
|
|
|
|
iconWidth = (int)(keyWidth * mSpacebarIconWidthRatio);
|
|
|
|
} else {
|
|
|
|
iconWidth = Math.min(icon.getIntrinsicWidth(), keyWidth);
|
|
|
|
}
|
2011-06-25 10:38:55 +00:00
|
|
|
final int iconHeight = icon.getIntrinsicHeight();
|
2014-08-07 03:32:59 +00:00
|
|
|
final int iconY;
|
|
|
|
if (key.isAlignIconToBottom()) {
|
|
|
|
iconY = keyHeight - iconHeight;
|
|
|
|
} else {
|
|
|
|
iconY = (keyHeight - iconHeight) / 2; // Align vertically center.
|
|
|
|
}
|
|
|
|
final int iconX = (keyWidth - iconWidth) / 2; // Align horizontally center.
|
2011-06-25 10:38:55 +00:00
|
|
|
drawIcon(canvas, icon, iconX, iconY, iconWidth, iconHeight);
|
2011-04-15 04:05:58 +00:00
|
|
|
}
|
|
|
|
|
2013-08-12 09:05:11 +00:00
|
|
|
if (key.hasPopupHint() && key.getMoreKeys() != null) {
|
2011-12-17 17:58:13 +00:00
|
|
|
drawKeyPopupHint(key, canvas, paint, params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw popup hint "..." at the bottom right corner of the key.
|
2012-09-05 06:34:51 +00:00
|
|
|
protected void drawKeyPopupHint(final Key key, final Canvas canvas, final Paint paint,
|
|
|
|
final KeyDrawParams params) {
|
2014-08-05 08:32:53 +00:00
|
|
|
if (TextUtils.isEmpty(mKeyPopupHintLetter)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-28 08:19:49 +00:00
|
|
|
final int keyWidth = key.getDrawWidth();
|
2013-08-12 09:05:11 +00:00
|
|
|
final int keyHeight = key.getHeight();
|
2011-12-17 17:58:13 +00:00
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
paint.setTypeface(params.mTypeface);
|
|
|
|
paint.setTextSize(params.mHintLetterSize);
|
|
|
|
paint.setColor(params.mHintLabelColor);
|
2011-12-17 17:58:13 +00:00
|
|
|
paint.setTextAlign(Align.CENTER);
|
2012-08-31 01:09:43 +00:00
|
|
|
final float hintX = keyWidth - mKeyHintLetterPadding
|
2013-12-13 08:09:16 +00:00
|
|
|
- TypefaceUtils.getReferenceCharWidth(paint) / 2.0f;
|
2012-08-31 01:09:43 +00:00
|
|
|
final float hintY = keyHeight - mKeyPopupHintLetterPadding;
|
2014-08-05 08:32:53 +00:00
|
|
|
canvas.drawText(mKeyPopupHintLetter, hintX, hintY, paint);
|
2011-04-15 04:05:58 +00:00
|
|
|
}
|
|
|
|
|
2012-09-05 06:34:51 +00:00
|
|
|
protected static void drawIcon(final Canvas canvas, final Drawable icon, final int x,
|
|
|
|
final int y, final int width, final int height) {
|
2010-10-19 15:18:50 +00:00
|
|
|
canvas.translate(x, y);
|
|
|
|
icon.setBounds(0, 0, width, height);
|
|
|
|
icon.draw(canvas);
|
|
|
|
canvas.translate(-x, -y);
|
|
|
|
}
|
|
|
|
|
2013-04-11 03:08:36 +00:00
|
|
|
public Paint newLabelPaint(final Key key) {
|
2012-03-16 07:41:18 +00:00
|
|
|
final Paint paint = new Paint();
|
|
|
|
paint.setAntiAlias(true);
|
2013-04-11 03:08:36 +00:00
|
|
|
if (key == null) {
|
|
|
|
paint.setTypeface(mKeyDrawParams.mTypeface);
|
|
|
|
paint.setTextSize(mKeyDrawParams.mLabelSize);
|
|
|
|
} else {
|
2014-02-06 09:14:26 +00:00
|
|
|
paint.setColor(key.selectTextColor(mKeyDrawParams));
|
2013-04-11 03:08:36 +00:00
|
|
|
paint.setTypeface(key.selectTypeface(mKeyDrawParams));
|
|
|
|
paint.setTextSize(key.selectTextSize(mKeyDrawParams));
|
|
|
|
}
|
2012-03-16 07:41:18 +00:00
|
|
|
return paint;
|
|
|
|
}
|
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
/**
|
|
|
|
* Requests a redraw of the entire keyboard. Calling {@link #invalidate} is not sufficient
|
|
|
|
* because the keyboard renders the keys to an off-screen buffer and an invalidate() only
|
|
|
|
* draws the cached buffer.
|
2010-08-31 11:21:45 +00:00
|
|
|
* @see #invalidateKey(Key)
|
2010-08-20 05:35:02 +00:00
|
|
|
*/
|
|
|
|
public void invalidateAllKeys() {
|
2012-03-13 08:28:52 +00:00
|
|
|
mInvalidatedKeys.clear();
|
|
|
|
mInvalidateAllKeys = true;
|
2010-08-20 05:35:02 +00:00
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invalidates a key so that it will be redrawn on the next repaint. Use this method if only
|
|
|
|
* one key is changing it's content. Any changes that affect the position or size of the key
|
|
|
|
* may not be honored.
|
2010-12-02 09:46:21 +00:00
|
|
|
* @param key key in the attached {@link Keyboard}.
|
2010-08-20 05:35:02 +00:00
|
|
|
* @see #invalidateAllKeys
|
|
|
|
*/
|
2012-09-05 06:34:51 +00:00
|
|
|
public void invalidateKey(final Key key) {
|
2012-03-13 08:28:52 +00:00
|
|
|
if (mInvalidateAllKeys) return;
|
|
|
|
if (key == null) return;
|
|
|
|
mInvalidatedKeys.add(key);
|
2013-08-12 09:05:11 +00:00
|
|
|
final int x = key.getX() + getPaddingLeft();
|
|
|
|
final int y = key.getY() + getPaddingTop();
|
|
|
|
invalidate(x, y, x + key.getWidth(), y + key.getHeight());
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-09-27 06:18:02 +00:00
|
|
|
protected void onDetachedFromWindow() {
|
2010-08-20 05:35:02 +00:00
|
|
|
super.onDetachedFromWindow();
|
2012-08-02 06:10:59 +00:00
|
|
|
freeOffscreenBuffer();
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
2013-08-01 09:01:48 +00:00
|
|
|
|
|
|
|
public void deallocateMemory() {
|
|
|
|
freeOffscreenBuffer();
|
|
|
|
}
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|