-
Notifications
You must be signed in to change notification settings - Fork 48
/
JustifiedTextView.java
122 lines (106 loc) · 3.9 KB
/
JustifiedTextView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (C) 2013 UNCOPT LLC.
*
* 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.
*/
package com.uncopt.android.widget.text.justify;
import android.content.Context;
import android.graphics.Typeface;
import android.text.Layout;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* A TextView with justified text.<br>
* The TextView has a ScrollingMovementMethod by default. You can change the MovementMethod,
* but you should not set it to null.
*/
public class JustifiedTextView extends TextView implements Justify.Justified {
@SuppressWarnings("unused")
public JustifiedTextView(final @NotNull Context context) {
super(context);
super.setMovementMethod(new LinkMovementMethod());
}
@SuppressWarnings("unused")
public JustifiedTextView(final @NotNull Context context, final AttributeSet attrs) {
super(context, attrs);
if (getMovementMethod() == null) super.setMovementMethod(new LinkMovementMethod());
}
@SuppressWarnings("unused")
public JustifiedTextView(final @NotNull Context context,
final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
if (getMovementMethod() == null) super.setMovementMethod(new LinkMovementMethod());
}
@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// Make sure we don't call setupScaleSpans again if the measure was triggered
// by setupScaleSpans itself.
if (!mMeasuring) {
final Typeface typeface = getTypeface();
final float textSize = getTextSize();
final float textScaleX = getTextScaleX();
final boolean fakeBold = getPaint().isFakeBoldText();
if (mTypeface != typeface ||
mTextSize != textSize ||
mTextScaleX != textScaleX ||
mFakeBold != fakeBold) {
final int width = MeasureSpec.getSize(widthMeasureSpec);
if (width > 0 && width != mWidth) {
mTypeface = typeface;
mTextSize = textSize;
mTextScaleX = textScaleX;
mFakeBold = fakeBold;
mWidth = width;
mMeasuring = true;
try {
// Setup ScaleXSpans on whitespaces to justify the text.
Justify.setupScaleSpans(this, mSpanStarts, mSpanEnds, mSpans);
}
finally {
mMeasuring = false;
}
}
}
}
}
@Override
protected void onTextChanged(final CharSequence text,
final int start, final int lengthBefore, final int lengthAfter) {
super.onTextChanged(text, start, lengthBefore, lengthAfter);
final Layout layout = getLayout();
if (layout != null) {
Justify.setupScaleSpans(this, mSpanStarts, mSpanEnds, mSpans);
}
}
@Override
@NotNull
public TextView getTextView() {
return this;
}
@Override
public float getMaxProportion() {
return Justify.DEFAULT_MAX_PROPORTION;
}
private static final int MAX_SPANS = 512;
private boolean mMeasuring = false;
private Typeface mTypeface = null;
private float mTextSize = 0f;
private float mTextScaleX = 0f;
private boolean mFakeBold = false;
private int mWidth = 0;
private int[] mSpanStarts = new int[MAX_SPANS];
private int[] mSpanEnds = new int[MAX_SPANS];
private Justify.ScaleSpan[] mSpans = new Justify.ScaleSpan[MAX_SPANS];
}