Skip to content

Commit 56e0f77

Browse files
author
Dean Wild
committed
fix vertical alignment, allow force marquee option
1 parent 03b324e commit 56e0f77

File tree

3 files changed

+72
-30
lines changed

3 files changed

+72
-30
lines changed

marqueetextview/src/main/java/uk/co/deanwild/marqueetextview/MarqueeTextView.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
import android.util.AttributeSet;
1414
import android.view.View;
1515

16-
import uk.co.deanwild.marqueetextview.R;
17-
1816
/**
1917
* Created by deanwild on 14/03/16.
2018
*/
@@ -26,6 +24,7 @@ public class MarqueeTextView extends View {
2624
static final int DEFAULT_EDGE_EFFECT_COLOR = Color.WHITE;
2725

2826
boolean marqueeEnabled = true;
27+
boolean forceMarquee = false;
2928
int textColor = Color.BLACK;
3029
float textSize = getResources().getDisplayMetrics().scaledDensity * 20.0f;
3130
int pauseDuration = DEFAULT_PAUSE_DURATION;
@@ -36,7 +35,6 @@ public class MarqueeTextView extends View {
3635

3736
CharSequence text;
3837

39-
int xOffset;
4038
double wrapAroundPoint;
4139
boolean animationRunning = false;
4240
boolean paused = false;
@@ -46,10 +44,13 @@ public class MarqueeTextView extends View {
4644
Paint leftPaint;
4745
Paint rightPaint;
4846

47+
4948
Rect textBounds;
5049
RectF leftRect;
5150
RectF rightRect;
5251

52+
int topOffset;
53+
int xOffset;
5354

5455
public static void setGlobalDefaultPauseDuration(int pauseDuration) {
5556
DEFAULT_PAUSE_DURATION = pauseDuration;
@@ -76,13 +77,9 @@ void init(AttributeSet attrs) {
7677
readAttrs(attrs);
7778
}
7879

79-
renewPaint();
80-
8180
textBounds = new Rect();
8281

83-
if (text != null) {
84-
setText(text);
85-
}
82+
setText(text);
8683

8784
}
8885

@@ -96,7 +93,8 @@ void readAttrs(AttributeSet attrs) {
9693
R.attr.showEdgeEffect,
9794
R.attr.edgeEffectWidth,
9895
R.attr.edgeEffectColor,
99-
R.attr.pauseDuration
96+
R.attr.pauseDuration,
97+
R.attr.forceMarqueed
10098
};
10199

102100
TypedArray ta = getContext().obtainStyledAttributes(attrs, attrsArray);
@@ -109,11 +107,11 @@ void readAttrs(AttributeSet attrs) {
109107
edgeEffectWidth = ta.getInt(5, edgeEffectWidth);
110108
edgeEffectColor = ta.getColor(6, edgeEffectColor);
111109
pauseDuration = ta.getInt(7, pauseDuration);
110+
forceMarquee = ta.getBoolean(8, forceMarquee);
112111

113112
ta.recycle();
114113
}
115114

116-
117115
@Override
118116
protected void onDraw(Canvas canvas) {
119117
super.onDraw(canvas);
@@ -124,9 +122,7 @@ protected void onDraw(Canvas canvas) {
124122

125123
int textWidth = textBounds.width();
126124

127-
float topOffset = textBounds.height() - textBounds.bottom;
128-
129-
if (textWidth < viewWidth) { // text can fit in view, no marquee needed
125+
if (textWidth < viewWidth && !forceMarquee) { // text can fit in view, no marquee needed
130126

131127
animationRunning = false;
132128

@@ -169,7 +165,7 @@ protected void onDraw(Canvas canvas) {
169165
if (wrapped && xOffset <= 0) {
170166
wrapped = false;
171167

172-
if(pauseDuration > 0) {
168+
if (pauseDuration > 0) {
173169
xOffset = 0;
174170
pause();
175171
}
@@ -233,11 +229,15 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
233229
// Parent has told us how big to be. So be it.
234230
height = heightSize;
235231
} else {
236-
height = (int) textSize;
232+
233+
TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
234+
paint.density = getResources().getDisplayMetrics().density;
235+
paint.setTextSize(textSize);
236+
237+
height = (int) (Math.abs(paint.ascent()) + Math.abs(paint.descent()));
237238
}
238239

239240
setMeasuredDimension(width, height);
240-
241241
renewPaint();
242242
}
243243

@@ -278,15 +278,23 @@ void renewPaint() {
278278

279279
leftRect = new RectF(0, 0, absEdgeEffectWidth, getMeasuredHeight());
280280
rightRect = new RectF(rightOffset, 0, getMeasuredWidth(), getMeasuredHeight());
281+
282+
textPaint.getTextBounds(text.toString(), 0, text.length(), textBounds);
283+
284+
int viewheight = getMeasuredHeight();
285+
topOffset = (int) (viewheight / 2 - ((textPaint.descent() + textPaint.ascent()) / 2));
281286
}
282287

283288
public void setSpeed(int speed) {
284289
this.speed = speed;
285290
}
286291

287292
public void setText(CharSequence text) {
293+
294+
if (text == null)
295+
text = "";
296+
288297
this.text = text;
289-
textPaint.getTextBounds(text.toString(), 0, text.length(), textBounds);
290298
animationRunning = false;
291299
requestLayout();
292300
}
@@ -300,7 +308,6 @@ public void setTextColor(int color) {
300308
public void setTextSize(float textSize) {
301309
this.textSize = textSize;
302310
renewPaint();
303-
textPaint.getTextBounds(text.toString(), 0, text.length(), textBounds);
304311
animationRunning = false;
305312
requestLayout();
306313
}

marqueetextview/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
<attr name="edgeEffectWidth" format="integer" />
66
<attr name="edgeEffectColor" format="color" />
77
<attr name="pauseDuration" format="integer" />
8+
<attr name="forceMarqueed" format="boolean" />
89
</declare-styleable>
910
</resources>

sample/src/main/res/layout/activity_main.xml

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,36 @@
1010

1111
<uk.co.deanwild.marqueetextview.MarqueeTextView
1212
android:layout_width="match_parent"
13-
android:layout_height="wrap_content"
14-
android:layout_margin="10dp"
15-
android:text="Lorem ipsum dolor sit amet."
16-
android:textColor="@color/colorAccent"
17-
13+
android:layout_height="100dp"
14+
android:background="#800"
15+
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit."
16+
android:textColor="#fff"
1817
android:textSize="42sp"
19-
app:edgeEffectColor="#fff"
20-
app:edgeEffectWidth="20"
2118
app:pauseDuration="3000"
22-
app:showEdgeEffect="true" />
19+
app:showEdgeEffect="false" />
20+
21+
22+
<TextView
23+
android:layout_width="match_parent"
24+
android:layout_height="100dp"
25+
android:background="#080"
26+
android:gravity="center"
27+
android:text="Lorem ipsum dolor"
28+
android:textColor="#fff"
29+
android:textSize="42sp" />
2330

2431
<LinearLayout
2532
android:layout_width="match_parent"
2633
android:layout_height="wrap_content"
34+
android:background="#999"
2735
android:orientation="horizontal">
2836

2937

3038
<uk.co.deanwild.marqueetextview.MarqueeTextView
31-
android:background="#999"
3239
android:layout_width="0dp"
33-
android:layout_weight="1"
3440
android:layout_height="wrap_content"
41+
android:layout_weight="1"
42+
3543
android:text="Karting"
3644
android:textColor="#fff"
3745

@@ -44,9 +52,9 @@
4452

4553
<uk.co.deanwild.marqueetextview.MarqueeTextView
4654
android:layout_width="0dp"
47-
android:background="#999"
48-
android:layout_weight="1"
4955
android:layout_height="wrap_content"
56+
android:layout_weight="1"
57+
5058
android:text="Football"
5159
android:textColor="#fff"
5260

@@ -57,7 +65,33 @@
5765
app:showEdgeEffect="true" />
5866

5967

68+
</LinearLayout>
69+
70+
71+
<LinearLayout
72+
android:layout_width="match_parent"
73+
android:layout_height="wrap_content"
74+
android:background="#333"
75+
android:orientation="horizontal">
76+
77+
<TextView
78+
android:layout_width="0dp"
79+
android:layout_height="wrap_content"
80+
android:layout_weight="1"
81+
android:gravity="center"
82+
android:text="Karting"
83+
android:textColor="#fff"
84+
android:textSize="24sp" />
85+
6086

87+
<TextView
88+
android:layout_width="0dp"
89+
android:layout_height="wrap_content"
90+
android:layout_weight="1"
91+
android:gravity="center"
92+
android:text="Football"
93+
android:textColor="#fff"
94+
android:textSize="24sp" />
6195

6296

6397
</LinearLayout>

0 commit comments

Comments
 (0)