@@ -20,6 +20,8 @@ public class OverlayTextView extends ViewGroup {
2020 private static final int BORDER_SIZE_PX = 1 ;
2121 private static final float BOX_CONTROL_SIZE_DP = 10 ;
2222 private static final float CONTROL_TOUCH_AREA_SIZE_DP = 48 ;
23+ private static final int MIN_TEXT_LINES_FOR_LINE_SPACING = 3 ;
24+ private static final float LINE_SPACING_ADJUSTMENT_DP = 5 ;
2325
2426 boolean hasFocus = true ;
2527
@@ -38,6 +40,7 @@ public class OverlayTextView extends ViewGroup {
3840 private float mShadowDxMultiplier = 0 ;
3941 private float mShadowDyMultiplier = 0 ;
4042 private float mBgCornerRadiusMultiplier = 0 ;
43+ private int lineSpacingPx ;
4144
4245 public OverlayTextView (Context context ) {
4346 super (context );
@@ -58,6 +61,7 @@ private void setDpValues() {
5861 textPadding = convertDpToPx (TEXT_PADDING_DP );
5962 controlTouchAreaSize = convertDpToPx (CONTROL_TOUCH_AREA_SIZE_DP );
6063 boxControlSize = convertDpToPx (BOX_CONTROL_SIZE_DP );
64+ lineSpacingPx = convertDpToPx (LINE_SPACING_ADJUSTMENT_DP );
6165 }
6266
6367 private int convertDpToPx (float dp ) {
@@ -108,6 +112,7 @@ private void setupControls(Context context) {
108112
109113 float dx ;
110114 int [] textViewLocation = new int [2 ];
115+ float oldDesiredWidth ;
111116
112117 @ SuppressLint ("ClickableViewAccessibility" )
113118 @ Override
@@ -118,15 +123,40 @@ public boolean onTouch(View v, MotionEvent event) {
118123 mTextView .getLocationOnScreen (textViewLocation );
119124 int rightEdgeStart = mTextView .getRight () + textViewLocation [0 ];
120125 dx = rightEdgeStart - event .getRawX ();
126+ oldDesiredWidth = getDesiredWidth (event );
121127 return true ;
122128 case MotionEvent .ACTION_MOVE :
123- float desiredWidth = event .getRawX () + dx - textViewLocation [0 ];
124- float scale = desiredWidth / mTextView .getUnscaledWidth ();
125- mTextView .setScaleX (scale );
129+ float desiredWidth = getDesiredWidth (event );
130+ if (mTextView .getLineCount () >= MIN_TEXT_LINES_FOR_LINE_SPACING ) {
131+ setLineSpacing (desiredWidth );
132+ } else {
133+ scaleText (desiredWidth );
134+ }
135+ oldDesiredWidth = desiredWidth ;
126136 return true ;
127137 }
128138 return true ;
129139 }
140+
141+ private float getDesiredWidth (MotionEvent event ) {
142+ return event .getRawX () + dx - textViewLocation [0 ];
143+ }
144+
145+ private void setLineSpacing (float desiredWidth ) {
146+ float currentLineSpacing = mTextView .getLineSpacingExtra ();
147+ int numLines = mTextView .getLineCount ();
148+ float adjustment = lineSpacingPx / (numLines - 1 );
149+ if (oldDesiredWidth < desiredWidth ) {
150+ mTextView .setLineSpacing (currentLineSpacing + adjustment , 1 );
151+ } else {
152+ mTextView .setLineSpacing (currentLineSpacing - adjustment , 1 );
153+ }
154+ }
155+
156+ private void scaleText (float desiredWidth ) {
157+ float scale = desiredWidth / mTextView .getUnscaledWidth ();
158+ mTextView .setScaleX (scale );
159+ }
130160 };
131161
132162 OnTouchListener fontSizeTouchListener = new OnTouchListener () {
@@ -241,11 +271,6 @@ private int getTextViewLeft() {
241271 return getPaddingLeft ();
242272 }
243273
244- // in local coordinates
245- private int getTextViewRight () {
246- return getPaddingLeft () + mTextView .getWidth ();
247- }
248-
249274 // in local coordinates
250275 private int getTextViewBottom () {
251276 return getTextViewTop () + mTextView .getHeight ();
@@ -451,13 +476,6 @@ public PointF getTextViewTopLeft() {
451476 return new PointF (x , y );
452477 }
453478
454- // in parent coordinates
455- public PointF getTextViewTopRight () {
456- float x = getX () + getTextViewRight ();// + mTextView.getPaddingLeft();
457- float y = getY () + getTextViewTop ();// + mTextView.getPaddingTop();
458- return new PointF (x , y );
459- }
460-
461479 // in parent coordinates
462480 public PointF getTextViewBottomLeft () {
463481 float x = getX () + getTextViewLeft ();// + mTextView.getPaddingLeft();
0 commit comments