am 016d6c42: Auto scale-X text of key popup preview

* commit '016d6c424cf004c282db5a57e64f3d1eee672a22':
  Auto scale-X text of key popup preview
This commit is contained in:
Tadashi G. Takaoka 2014-08-12 04:29:16 +00:00 committed by Android Git Automerger
commit 3d477a73af
2 changed files with 54 additions and 1 deletions

View file

@ -46,6 +46,7 @@ import com.android.inputmethod.keyboard.internal.GestureTrailsDrawingPreview;
import com.android.inputmethod.keyboard.internal.KeyDrawParams; import com.android.inputmethod.keyboard.internal.KeyDrawParams;
import com.android.inputmethod.keyboard.internal.KeyPreviewChoreographer; import com.android.inputmethod.keyboard.internal.KeyPreviewChoreographer;
import com.android.inputmethod.keyboard.internal.KeyPreviewDrawParams; import com.android.inputmethod.keyboard.internal.KeyPreviewDrawParams;
import com.android.inputmethod.keyboard.internal.KeyPreviewView;
import com.android.inputmethod.keyboard.internal.LanguageOnSpacebarHelper; import com.android.inputmethod.keyboard.internal.LanguageOnSpacebarHelper;
import com.android.inputmethod.keyboard.internal.MoreKeySpec; import com.android.inputmethod.keyboard.internal.MoreKeySpec;
import com.android.inputmethod.keyboard.internal.NonDistinctMultitouchHelper; import com.android.inputmethod.keyboard.internal.NonDistinctMultitouchHelper;
@ -764,6 +765,9 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
public void startDisplayLanguageOnSpacebar(final boolean subtypeChanged, public void startDisplayLanguageOnSpacebar(final boolean subtypeChanged,
final int languageOnSpacebarFormatType, final int languageOnSpacebarFormatType,
final boolean hasMultipleEnabledIMEsOrSubtypes) { final boolean hasMultipleEnabledIMEsOrSubtypes) {
if (subtypeChanged) {
KeyPreviewView.clearTextCache();
}
mLanguageOnSpacebarFormatType = languageOnSpacebarFormatType; mLanguageOnSpacebarFormatType = languageOnSpacebarFormatType;
mHasMultipleEnabledIMEsOrSubtypes = hasMultipleEnabledIMEsOrSubtypes; mHasMultipleEnabledIMEsOrSubtypes = hasMultipleEnabledIMEsOrSubtypes;
final ObjectAnimator animator = mLanguageOnSpacebarFadeoutAnimator; final ObjectAnimator animator = mLanguageOnSpacebarFadeoutAnimator;

View file

@ -17,7 +17,10 @@
package com.android.inputmethod.keyboard.internal; package com.android.inputmethod.keyboard.internal;
import android.content.Context; import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.Gravity; import android.view.Gravity;
@ -26,6 +29,8 @@ import android.widget.TextView;
import com.android.inputmethod.keyboard.Key; import com.android.inputmethod.keyboard.Key;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
import java.util.HashSet;
/** /**
* The pop up key preview view. * The pop up key preview view.
*/ */
@ -34,6 +39,9 @@ public class KeyPreviewView extends TextView {
public static final int POSITION_LEFT = 1; public static final int POSITION_LEFT = 1;
public static final int POSITION_RIGHT = 2; public static final int POSITION_RIGHT = 2;
private final Rect mBackgroundPadding = new Rect();
private static final HashSet<String> sNoScaleXTextSet = new HashSet<>();
public KeyPreviewView(final Context context, final AttributeSet attrs) { public KeyPreviewView(final Context context, final AttributeSet attrs) {
this(context, attrs, 0); this(context, attrs, 0);
} }
@ -58,7 +66,48 @@ public class KeyPreviewView extends TextView {
setTextSize(TypedValue.COMPLEX_UNIT_PX, key.selectPreviewTextSize(drawParams)); setTextSize(TypedValue.COMPLEX_UNIT_PX, key.selectPreviewTextSize(drawParams));
setTypeface(key.selectPreviewTypeface(drawParams)); setTypeface(key.selectPreviewTypeface(drawParams));
// TODO Should take care of temporaryShiftLabel here. // TODO Should take care of temporaryShiftLabel here.
setText(key.getPreviewLabel()); setTextAndScaleX(key.getPreviewLabel());
}
private void setTextAndScaleX(final String text) {
setTextScaleX(1.0f);
setText(text);
if (sNoScaleXTextSet.contains(text)) {
return;
}
// TODO: Override {@link #setBackground(Drawable)} that is supported from API 16 and
// calculate maximum text width.
final Drawable background = getBackground();
if (background == null) {
return;
}
background.getPadding(mBackgroundPadding);
final int maxWidth = background.getIntrinsicWidth() - mBackgroundPadding.left
- mBackgroundPadding.right;
final float width = getTextWidth(text, getPaint());
if (width <= maxWidth) {
sNoScaleXTextSet.add(text);
return;
}
setTextScaleX(maxWidth / width);
}
public static void clearTextCache() {
sNoScaleXTextSet.clear();
}
private static float getTextWidth(final String text, final TextPaint paint) {
if (TextUtils.isEmpty(text)) {
return 0.0f;
}
final int len = text.length();
final float[] widths = new float[len];
final int count = paint.getTextWidths(text, 0, len, widths);
float width = 0;
for (int i = 0; i < count; i++) {
width += widths[i];
}
return width;
} }
// Background state set // Background state set