方式1
以下方式只有在TextView的文字超过一行时显示跑马灯效果
代码1:Activity
/**
* 自定义TextView TextView跑马灯
* */
public class MarqueeTextActivity extends AppCompatActivity {
private MarqueeText marqueeText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_marqueetext);
marqueeText= findViewById(R.id.activity_motionevent_marqueetext);
marqueeText.setText("借助 Android Things,您可以大规模构建和维护 IoT 设备。" +
"我们最近发布了 Android Things 1.0 正式版,它将为生产设备提供长期支持," +
"帮助您轻松地将 IoT 设备从原型设计推进到商品化。");
}
}
代码2:布局
效果:
完整代码:https://github.com/wujianning/CustomView
方式2
以下方式可以满足TextView不满一行也可实现跑马灯效果
1.添加依赖
compile 'com.dalong:marqueeview:1.0.0'
2.MarqueeView源码
public class MarqueeView extends SurfaceView implements SurfaceHolder.Callback{
public Context mContext;
private float mTextSize = 100; //字体大小
private int mTextColor = Color.RED; //字体的颜色
private int mBackgroundColor=Color.WHITE;//背景色
private boolean mIsRepeat;//是否重复滚动
private int mStartPoint;// 开始滚动的位置 0是从最左面开始 1是从最末尾开始
private int mDirection;//滚动方向 0 向左滚动 1向右滚动
private int mSpeed;//滚动速度
private SurfaceHolder holder;
private TextPaint mTextPaint;
private MarqueeViewThread mThread;
private String margueeString;
private int textWidth=0,textHeight=0;
private int ShadowColor=Color.BLACK;
public int currentX=0;// 当前x的位置
public int sepX=5;//每一步滚动的距离
public MarqueeView(Context context) {
this(context,null);
}
public MarqueeView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public MarqueeView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext=context;
init(attrs, defStyleAttr);
}
private void init(AttributeSet attrs, int defStyleAttr) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MarqueeView, defStyleAttr, 0);
mTextColor = a.getColor(R.styleable.MarqueeView_textcolor, Color.RED);
mTextSize = a.getDimension(R.styleable.MarqueeView_textSize, 48);
mBackgroundColor=a.getColor(R.styleable.MarqueeView_marqueebackground,Color.BLACK);
mIsRepeat=a.getBoolean(R.styleable.MarqueeView_isRepeat,false);
mStartPoint=a.getInt(R.styleable.MarqueeView_startPoint,0);
mDirection=a.getInt(R.styleable.MarqueeView_direction,0);
mSpeed=a.getInt(R.styleable.MarqueeView_speed,20);
a.recycle();
holder = this.getHolder();
holder.addCallback(this);
mTextPaint = new TextPaint();
mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setTextAlign(Paint.Align.LEFT);
}
public void setText(String msg){
if(!TextUtils.isEmpty(msg)){
measurementsText(msg);
}
}
protected void measurementsText(String msg) {
margueeString=msg;
mTextPaint.setTextSize(mTextSize);
mTextPaint.setColor(mTextColor);
mTextPaint.setStrokeWidth(0.5f);
mTextPaint.setFakeBoldText(true);
// 设定阴影(柔边, X 轴位移, Y 轴位移, 阴影颜色)
// mTextPaint.setShadowLayer(5, 3, 3, ShadowColor);
textWidth = (int)mTextPaint.measureText(margueeString);
Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
textHeight = (int) fontMetrics.bottom;
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
if(mStartPoint==0)
currentX=0;
else
currentX=width-getPaddingLeft()-getPaddingRight();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
this.holder=holder;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if(mThread!=null)
mThread.isRun = true;
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if(mThread!=null)
mThread.isRun = false;
}
/**
* 开始滚动
*/
public void startScroll(){
if(mThread!=null&&mThread.isRun)
return;
mThread = new MarqueeViewThread(holder);//创建一个绘图线程
mThread.start();
}
/**
* 停止滚动
*/
public void stopScroll(){
if(mThread!=null){
mThread.isRun = false;
mThread.interrupt();
}
mThread=null;
}
/**
* 线程
*/
class MarqueeViewThread extends Thread{
private SurfaceHolder holder;
public boolean isRun ;//是否在运行
public MarqueeViewThread(SurfaceHolder holder) {
this.holder =holder;
isRun = true;
}
public void onDraw() {
try {
synchronized (holder) {
if (TextUtils.isEmpty(margueeString)) {
Thread.sleep(1000);//睡眠时间为1秒
return;
}
Canvas canvas = holder.lockCanvas();
int paddingLeft = getPaddingLeft();
int paddingTop = getPaddingTop();
int paddingRight = getPaddingRight();
int paddingBottom = getPaddingBottom();
int contentWidth = getWidth() - paddingLeft - paddingRight;
int contentHeight = getHeight() - paddingTop - paddingBottom;
int centeYLine = paddingTop + contentHeight / 2;//中心线
if(mDirection==0) {//向左滚动
if(currentX <=-textWidth){
if(!mIsRepeat){//如果是不重复滚动
mHandler.sendEmptyMessage(ROLL_OVER);
}
currentX=contentWidth;
}else{
currentX-=sepX;
}
}else {// 向右滚动
if(currentX>=contentWidth){
if(!mIsRepeat){//如果是不重复滚动
mHandler.sendEmptyMessage(ROLL_OVER);
}
currentX=-textWidth;
}else{
currentX+=sepX;
}
}
if(canvas!=null)
canvas.drawColor(mBackgroundColor);
canvas.drawText(margueeString,currentX, centeYLine+dip2px(getContext(),textHeight)/2,mTextPaint);
holder.unlockCanvasAndPost(canvas);//结束锁定画图,并提交改变。
int a=textWidth/margueeString.trim().length();
int b=a/sepX;
int c=mSpeed/b==0?1:mSpeed/b;
Thread.sleep(c);//睡眠时间为移动的频率
}
}
catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (isRun) {
onDraw();
}
}
}
public static final int ROLL_OVER =100;
Handler mHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case ROLL_OVER:
stopScroll();
if(mOnMargueeListener!=null){
mOnMargueeListener.onRollOver();
}
break;
}
}
};
/**
* dip转换为px
* @param context
* @param dpValue
* @return
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
public void reset(){
int contentWidth = getWidth() - getPaddingLeft() - getPaddingRight();
if(mStartPoint==0)
currentX=0;
else
currentX=contentWidth;
}
/**
* 滚动回调
*/
public interface OnMargueeListener{
void onRollOver();//滚动完毕
}
OnMargueeListener mOnMargueeListener;
public void setOnMargueeListener(OnMargueeListener mOnMargueeListener){
this.mOnMargueeListener=mOnMargueeListener;
}
}
3.布局
只加了一个自定义控件,app:direction=”left” 表示向左滚动,根据自己的需求设定方向;app:isRepeat=”true” 是否重复滚动;app:speed=”100” 设置滚动的速度,值越小速度越快,值越大速度越慢,app:startPoint=”end” 是文字的起始点。
4.Java代码使用
marqueeView= (MarqueeView) findViewById(R.id.tv_marquee);
marqueeView.setText("跑马灯");
marqueeView.setFocusable(true);
marqueeView.requestFocus();
marqueeView.startScroll(); //开始