Last active
March 16, 2017 09:25
-
-
Save bestpika/6c528e72f1eaf978076dd0e930883a88 to your computer and use it in GitHub Desktop.
Android MarqueeTextView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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