小视频app源码自定义跑马灯,可控制速度与方向
作者:互联网
小视频app源码自定义跑马灯,可控制速度与方向的相关代码
主要是通过继承自TextView实现自定义View,使用drawText方法不断重绘文字。
XML布局:
<com.ycq.myview.MarqueeText
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#339320"
android:maxLines="1"
android:textColor="#FF00FF"
android:textSize="21sp"/>
注意:需要设置为1行。可在XML中设置颜色,字号,不要设置文字内容。
Activity调用:
test = (MarqueeText) this.findViewById(R.id.test);
test.setMyContext("11111112225555");
test.setL2r(true);
test.setMySpeed(10);
test.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { start(); }});
setL2r设置方向,默认为从左向右;
setMySpeed设置速度,默认为5,说明:若速度设置<0,则默认为1,若>15,则默认15.
@Override
protected void onPause() {
super.onPause();
stop();
}
@Override
protected void onResume() {
super.onResume();
start();
}
@Override
protected void onDestroy() {
super.onDestroy();
stop();
}
public void start() {
test.startScroll();
}
public void stop() {
test.stopScroll();
}
销毁Activity,则不在执行。
具体实现:
package com.ycq.myview;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Created by yi on 2017/2/6.
*/
public class MarqueeText extends TextView implements Runnable {
private int currentScrollX = 0;// 当前滚动的位置
private boolean isStop = false;
private int textWidth;
private boolean isMeasure = false;
private String myContext = "";
private int vWidth;
private int mySpeed = 5;
private Boolean l2r = true;
//getPaint()获取系统画笔
public MarqueeText(Context context) {
super(context);
}
public MarqueeText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MarqueeText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (!isMeasure) {// 文字宽度只需获取一次就可以了
textWidth = (int) getPaint().measureText(myContext);
vWidth = getWidth();
isMeasure = true;
}
float baseline = getHeight() / 2 + getPaint().getTextSize() / 2 - getPaint().getFontMetrics().descent;
canvas.drawText(myContext, currentScrollX, baseline, getPaint());
}
@Override
public void run() {
if (!l2r) {//向左运动
currentScrollX -= mySpeed;// 滚动速度
if (currentScrollX < 0) {
if (Math.abs(currentScrollX) >= textWidth) {
currentScrollX = vWidth;
}
}
}
if (l2r) {//由左向右运动
currentScrollX += mySpeed;// 滚动速度
if (currentScrollX >= vWidth) {
currentScrollX = -textWidth;
}
}
invalidate();
postDelayed(this, 5);
if (isStop) {
return;
}
}
// 开始滚动
public void startScroll() {
isStop = false;
this.removeCallbacks(this);
post(this);
}
// 停止滚动
public void stopScroll() {
isStop = true;
}
public String getMyContext() {
return myContext;
}
public void setMyContext(String myContext) {
this.myContext = myContext;
textWidth = (int) getPaint().measureText(myContext);
}
public int getMySpeed() {
return mySpeed;
}
public void setMySpeed(int mySpeed) {
this.mySpeed = mySpeed;
if (mySpeed <= 0) {
this.mySpeed = 1;
}
if (mySpeed >= 15) {
this.mySpeed = 15;
}
}
public Boolean getL2r() {
return l2r;
}
public void setL2r(Boolean l2r) {
this.l2r = l2r;
}
}
float baseline = getHeight() / 2 + getPaint().getTextSize() / 2 - getPaint().getFontMetrics().descent;
canvas.drawText(myContext, currentScrollX, baseline, getPaint());
保证绘制的文字在控件的竖直中线位置。
以上就是 小视频app源码自定义跑马灯,可控制速度与方向的相关代码,更多内容欢迎关注之后的文章
标签:currentScrollX,自定义,void,跑马灯,源码,mySpeed,android,public,myContext 来源: https://www.cnblogs.com/yunbaomengnan/p/15304476.html