Auto scale-X text of key popup preview

Bug: 9978106
Change-Id: I335fa3117ac2af2cb8220c4827dd54c1a1da4c5c
main
Tadashi G. Takaoka 2014-08-11 19:13:07 +09:00
parent c9add10c38
commit 016d6c424c
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.KeyPreviewChoreographer;
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.MoreKeySpec;
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,
final int languageOnSpacebarFormatType,
final boolean hasMultipleEnabledIMEsOrSubtypes) {
if (subtypeChanged) {
KeyPreviewView.clearTextCache();
}
mLanguageOnSpacebarFormatType = languageOnSpacebarFormatType;
mHasMultipleEnabledIMEsOrSubtypes = hasMultipleEnabledIMEsOrSubtypes;
final ObjectAnimator animator = mLanguageOnSpacebarFadeoutAnimator;

View File

@ -17,7 +17,10 @@
package com.android.inputmethod.keyboard.internal;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
@ -26,6 +29,8 @@ import android.widget.TextView;
import com.android.inputmethod.keyboard.Key;
import com.android.inputmethod.latin.R;
import java.util.HashSet;
/**
* 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_RIGHT = 2;
private final Rect mBackgroundPadding = new Rect();
private static final HashSet<String> sNoScaleXTextSet = new HashSet<>();
public KeyPreviewView(final Context context, final AttributeSet attrs) {
this(context, attrs, 0);
}
@ -58,7 +66,48 @@ public class KeyPreviewView extends TextView {
setTextSize(TypedValue.COMPLEX_UNIT_PX, key.selectPreviewTextSize(drawParams));
setTypeface(key.selectPreviewTypeface(drawParams));
// 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