Skip to content

Commit

Permalink
fix positioning of text
Browse files Browse the repository at this point in the history
  • Loading branch information
Dean Wild committed Mar 15, 2016
1 parent 15e4e27 commit a2245ea
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public class MarqueeTextView extends View {

static final int DEFAULT_SPEED = 10;
static int DEFAULT_PAUSE_DURATION = 3000;
static int DEFAULT_PAUSE_DURATION = 10000;
static final int DEFAULT_EDGE_EFFECT_WIDTH = 20;
static final int DEFAULT_EDGE_EFFECT_COLOR = Color.WHITE;

Expand Down Expand Up @@ -124,12 +124,14 @@ protected void onDraw(Canvas canvas) {

int textWidth = textBounds.width();

float topOffset = textBounds.height() - textBounds.bottom;

if (textWidth < viewWidth) { // text can fit in view, no marquee needed

animationRunning = false;

float leftMargin = (viewWidth - textWidth) / 2;
canvas.drawText(text.toString(), leftMargin, textBounds.height(), textPaint);
canvas.drawText(text.toString(), leftMargin, topOffset, textPaint);

} else { // not enough room, we must animate it

Expand All @@ -143,7 +145,7 @@ protected void onDraw(Canvas canvas) {

}

canvas.drawText(text.toString(), xOffset, textBounds.height(), textPaint);
canvas.drawText(text.toString(), xOffset, topOffset, textPaint);

if (showEdgeEffect) {

Expand Down Expand Up @@ -228,7 +230,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Parent has told us how big to be. So be it.
height = heightSize;
} else {
height = textBounds.height() + textBounds.bottom;
height = (int) textSize;
}

setMeasuredDimension(width, height);
Expand Down
74 changes: 37 additions & 37 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,66 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#fff"
android:orientation="vertical"
tools:context="uk.co.deanwild.marqueetextviewsample.MainActivity">

<uk.co.deanwild.marqueetextview.MarqueeTextView
app:pauseDuration="3000"
android:text="Lorem ipsum dolor sit amet."
app:showEdgeEffect="true"
app:edgeEffectColor="#fff"
app:edgeEffectWidth="20"

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Lorem ipsum dolor sit amet."
android:textColor="@color/colorAccent"
android:textSize="42sp" />

<uk.co.deanwild.marqueetextview.MarqueeTextView
app:pauseDuration="3000"
android:text="Lorem ipsum dolor sit amet."
app:showEdgeEffect="true"
android:textSize="42sp"
app:edgeEffectColor="#fff"
app:edgeEffectWidth="20"
app:pauseDuration="3000"
app:showEdgeEffect="true" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textColor="@color/colorAccent"
android:textSize="42sp" />
android:orientation="horizontal">

<uk.co.deanwild.marqueetextview.MarqueeTextView
app:pauseDuration="3000"
android:text="Lorem ipsum dolor sit amet."
app:showEdgeEffect="true"
app:edgeEffectColor="#fff"
app:edgeEffectWidth="20"

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textColor="@color/colorAccent"
android:textSize="42sp" />
<uk.co.deanwild.marqueetextview.MarqueeTextView
android:background="#999"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Karting"
android:textColor="#fff"

android:textSize="24sp"
app:edgeEffectColor="#fff"
app:edgeEffectWidth="20"
app:pauseDuration="3000"
app:showEdgeEffect="true" />


<uk.co.deanwild.marqueetextview.MarqueeTextView
android:layout_width="0dp"
android:background="#999"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Football"
android:textColor="#fff"

android:textSize="24sp"
app:edgeEffectColor="#fff"
app:edgeEffectWidth="20"
app:pauseDuration="3000"
app:showEdgeEffect="true" />


<uk.co.deanwild.marqueetextview.MarqueeTextView
app:pauseDuration="3000"
android:text="Lorem ipsum dolor sit amet."
app:showEdgeEffect="true"
app:edgeEffectColor="#000"
app:edgeEffectWidth="20"

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textColor="@color/colorAccent"
android:textSize="42sp" />


</LinearLayout>


</LinearLayout>

0 comments on commit a2245ea

Please sign in to comment.