Merge "Add explicit sizing logic for single words" into tm-dev am: 7316c95946
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/18463431 Change-Id: I7bba85a9499085efc6d21b4cdedb998163e3171d Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
@@ -111,8 +111,8 @@
|
||||
android:gravity="center|start"
|
||||
android:ellipsize="end"
|
||||
android:autoSizeTextType="uniform"
|
||||
android:autoSizeMinTextSize="10sp"
|
||||
android:autoSizeMaxTextSize="200sp"
|
||||
android:autoSizeMinTextSize="@dimen/clipboard_overlay_min_font"
|
||||
android:autoSizeMaxTextSize="@dimen/clipboard_overlay_max_font"
|
||||
android:textColor="?attr/overlayButtonTextColor"
|
||||
android:background="?androidprv:attr/colorAccentSecondary"
|
||||
android:layout_width="@dimen/clipboard_preview_size"
|
||||
|
||||
@@ -295,7 +295,8 @@
|
||||
<dimen name="overlay_border_width_neg">-4dp</dimen>
|
||||
|
||||
<dimen name="clipboard_preview_size">@dimen/overlay_x_scale</dimen>
|
||||
|
||||
<dimen name="clipboard_overlay_min_font">10sp</dimen>
|
||||
<dimen name="clipboard_overlay_max_font">50sp</dimen>
|
||||
|
||||
<!-- The width of the view containing navigation buttons -->
|
||||
<dimen name="navigation_key_width">70dp</dimen>
|
||||
|
||||
@@ -51,8 +51,10 @@ import android.content.IntentFilter;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.res.Configuration;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Insets;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.Region;
|
||||
import android.graphics.drawable.Icon;
|
||||
@@ -67,8 +69,10 @@ import android.util.DisplayMetrics;
|
||||
import android.util.Log;
|
||||
import android.util.MathUtils;
|
||||
import android.util.Size;
|
||||
import android.util.TypedValue;
|
||||
import android.view.Display;
|
||||
import android.view.DisplayCutout;
|
||||
import android.view.Gravity;
|
||||
import android.view.InputEvent;
|
||||
import android.view.InputEventReceiver;
|
||||
import android.view.InputMonitor;
|
||||
@@ -121,6 +125,7 @@ public class ClipboardOverlayController {
|
||||
|
||||
private static final int CLIPBOARD_DEFAULT_TIMEOUT_MILLIS = 6000;
|
||||
private static final int SWIPE_PADDING_DP = 12; // extra padding around views to allow swipe
|
||||
private static final int FONT_SEARCH_STEP_PX = 4;
|
||||
|
||||
private final Context mContext;
|
||||
private final UiEventLogger mUiEventLogger;
|
||||
@@ -506,10 +511,59 @@ public class ClipboardOverlayController {
|
||||
|
||||
private void showTextPreview(CharSequence text, TextView textView) {
|
||||
showSinglePreview(textView);
|
||||
textView.setText(text.subSequence(0, Math.min(500, text.length())));
|
||||
final CharSequence truncatedText = text.subSequence(0, Math.min(500, text.length()));
|
||||
textView.setText(truncatedText);
|
||||
updateTextSize(truncatedText, textView);
|
||||
|
||||
textView.addOnLayoutChangeListener(
|
||||
(v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
|
||||
if (right - left != oldRight - oldLeft) {
|
||||
updateTextSize(truncatedText, textView);
|
||||
}
|
||||
});
|
||||
mEditChip.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
private void updateTextSize(CharSequence text, TextView textView) {
|
||||
Paint paint = new Paint(textView.getPaint());
|
||||
Resources res = textView.getResources();
|
||||
float minFontSize = res.getDimensionPixelSize(R.dimen.clipboard_overlay_min_font);
|
||||
float maxFontSize = res.getDimensionPixelSize(R.dimen.clipboard_overlay_max_font);
|
||||
if (isOneWord(text) && fitsInView(text, textView, paint, minFontSize)) {
|
||||
// If the text is a single word and would fit within the TextView at the min font size,
|
||||
// find the biggest font size that will fit.
|
||||
float fontSizePx = minFontSize;
|
||||
while (fontSizePx + FONT_SEARCH_STEP_PX < maxFontSize
|
||||
&& fitsInView(text, textView, paint, fontSizePx + FONT_SEARCH_STEP_PX)) {
|
||||
fontSizePx += FONT_SEARCH_STEP_PX;
|
||||
}
|
||||
// Need to turn off autosizing, otherwise setTextSize is a no-op.
|
||||
textView.setAutoSizeTextTypeWithDefaults(TextView.AUTO_SIZE_TEXT_TYPE_NONE);
|
||||
// It's possible to hit the max font size and not fill the width, so centering
|
||||
// horizontally looks better in this case.
|
||||
textView.setGravity(Gravity.CENTER);
|
||||
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, (int) fontSizePx);
|
||||
} else {
|
||||
// Otherwise just stick with autosize.
|
||||
textView.setAutoSizeTextTypeUniformWithConfiguration((int) minFontSize,
|
||||
(int) maxFontSize, FONT_SEARCH_STEP_PX, TypedValue.COMPLEX_UNIT_PX);
|
||||
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean fitsInView(CharSequence text, TextView textView, Paint paint,
|
||||
float fontSizePx) {
|
||||
paint.setTextSize(fontSizePx);
|
||||
float size = paint.measureText(text.toString());
|
||||
float availableWidth = textView.getWidth() - textView.getPaddingLeft()
|
||||
- textView.getPaddingRight();
|
||||
return size < availableWidth;
|
||||
}
|
||||
|
||||
private static boolean isOneWord(CharSequence text) {
|
||||
return text.toString().split("\\s+", 2).length == 1;
|
||||
}
|
||||
|
||||
private void showEditableText(CharSequence text, boolean hidden) {
|
||||
TextView textView = hidden ? mHiddenPreview : mTextPreview;
|
||||
showTextPreview(text, textView);
|
||||
|
||||
Reference in New Issue
Block a user