Skip to content

Instantly share code, notes, and snippets.

@bestpika
Last active March 16, 2017 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bestpika/6c528e72f1eaf978076dd0e930883a88 to your computer and use it in GitHub Desktop.
Save bestpika/6c528e72f1eaf978076dd0e930883a88 to your computer and use it in GitHub Desktop.
Android MarqueeTextView
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
public class MarqueeTextView extends android.support.v7.widget.AppCompatTextView {
private Paint paint = null;
private float x = 0.0f;
private float y = 0.0f;
private float speed = 2.0f;
public MarqueeTextView(Context context) {
super(context);
init();
}
public MarqueeTextView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
init();
}
public MarqueeTextView(Context context, AttributeSet attributeSet, int defStyle) {
super(context, attributeSet, defStyle);
init();
}
public void setSpeed(float speed) {
this.speed = speed;
}
private void init() {
paint = getPaint();
}
@Override
public void onDraw(Canvas canvas) {
String text = getText().toString();
paint.setColor(getCurrentTextColor());
setTextSize(TypedValue.COMPLEX_UNIT_PX, getHeight() - (getHeight() / 10));
y = getTextSize() + getPaddingTop();
canvas.drawText(text, getWidth() + paint.measureText(text) - x, y, paint);
if (x == 0 || x > (getWidth() + (paint.measureText(text) * 2)))
x = paint.measureText(text);
else
x += speed;
invalidate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment