Skip to content

Commit

Permalink
fix vertical alignment, allow force marquee option
Browse files Browse the repository at this point in the history
  • Loading branch information
Dean Wild committed Mar 16, 2016
1 parent 03b324e commit 56e0f77
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import android.util.AttributeSet;
import android.view.View;

import uk.co.deanwild.marqueetextview.R;

/**
* Created by deanwild on 14/03/16.
*/
Expand All @@ -26,6 +24,7 @@ public class MarqueeTextView extends View {
static final int DEFAULT_EDGE_EFFECT_COLOR = Color.WHITE;

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

CharSequence text;

int xOffset;
double wrapAroundPoint;
boolean animationRunning = false;
boolean paused = false;
Expand All @@ -46,10 +44,13 @@ public class MarqueeTextView extends View {
Paint leftPaint;
Paint rightPaint;


Rect textBounds;
RectF leftRect;
RectF rightRect;

int topOffset;
int xOffset;

public static void setGlobalDefaultPauseDuration(int pauseDuration) {
DEFAULT_PAUSE_DURATION = pauseDuration;
Expand All @@ -76,13 +77,9 @@ void init(AttributeSet attrs) {
readAttrs(attrs);
}

renewPaint();

textBounds = new Rect();

if (text != null) {
setText(text);
}
setText(text);

}

Expand All @@ -96,7 +93,8 @@ void readAttrs(AttributeSet attrs) {
R.attr.showEdgeEffect,
R.attr.edgeEffectWidth,
R.attr.edgeEffectColor,
R.attr.pauseDuration
R.attr.pauseDuration,
R.attr.forceMarqueed
};

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

ta.recycle();
}


@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Expand All @@ -124,9 +122,7 @@ 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
if (textWidth < viewWidth && !forceMarquee) { // text can fit in view, no marquee needed

animationRunning = false;

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

if(pauseDuration > 0) {
if (pauseDuration > 0) {
xOffset = 0;
pause();
}
Expand Down Expand Up @@ -233,11 +229,15 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Parent has told us how big to be. So be it.
height = heightSize;
} else {
height = (int) textSize;

TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
paint.density = getResources().getDisplayMetrics().density;
paint.setTextSize(textSize);

height = (int) (Math.abs(paint.ascent()) + Math.abs(paint.descent()));
}

setMeasuredDimension(width, height);

renewPaint();
}

Expand Down Expand Up @@ -278,15 +278,23 @@ void renewPaint() {

leftRect = new RectF(0, 0, absEdgeEffectWidth, getMeasuredHeight());
rightRect = new RectF(rightOffset, 0, getMeasuredWidth(), getMeasuredHeight());

textPaint.getTextBounds(text.toString(), 0, text.length(), textBounds);

int viewheight = getMeasuredHeight();
topOffset = (int) (viewheight / 2 - ((textPaint.descent() + textPaint.ascent()) / 2));
}

public void setSpeed(int speed) {
this.speed = speed;
}

public void setText(CharSequence text) {

if (text == null)
text = "";

this.text = text;
textPaint.getTextBounds(text.toString(), 0, text.length(), textBounds);
animationRunning = false;
requestLayout();
}
Expand All @@ -300,7 +308,6 @@ public void setTextColor(int color) {
public void setTextSize(float textSize) {
this.textSize = textSize;
renewPaint();
textPaint.getTextBounds(text.toString(), 0, text.length(), textBounds);
animationRunning = false;
requestLayout();
}
Expand Down
1 change: 1 addition & 0 deletions marqueetextview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
<attr name="edgeEffectWidth" format="integer" />
<attr name="edgeEffectColor" format="color" />
<attr name="pauseDuration" format="integer" />
<attr name="forceMarqueed" format="boolean" />
</declare-styleable>
</resources>
58 changes: 46 additions & 12 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,36 @@

<uk.co.deanwild.marqueetextview.MarqueeTextView
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:layout_height="100dp"
android:background="#800"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit."
android:textColor="#fff"
android:textSize="42sp"
app:edgeEffectColor="#fff"
app:edgeEffectWidth="20"
app:pauseDuration="3000"
app:showEdgeEffect="true" />
app:showEdgeEffect="false" />


<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#080"
android:gravity="center"
android:text="Lorem ipsum dolor"
android:textColor="#fff"
android:textSize="42sp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#999"
android:orientation="horizontal">


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

android:text="Karting"
android:textColor="#fff"

Expand All @@ -44,9 +52,9 @@

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

android:text="Football"
android:textColor="#fff"

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


</LinearLayout>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#333"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Karting"
android:textColor="#fff"
android:textSize="24sp" />


<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Football"
android:textColor="#fff"
android:textSize="24sp" />


</LinearLayout>
Expand Down

0 comments on commit 56e0f77

Please sign in to comment.