因项目需求,需实现文字跑马灯效果,且能动态设置文本的滚动速度、字体颜色、字体大小等,Android自带的TextView也能实现跑马灯效果,但有个问题,文字内容长度必须大于控件宽度才会滚动,且滚动速度不可动态设置;
So,就参考了相关文档,自己实现了以上需求,使用方法与TextView差不多,在此以作记录,方便日后复习、优化。上代码:
package com.xxx.test; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.text.TextUtils; import android.util.AttributeSet; import android.view.View; /** * Created by xRoon on 2016/2/27. */ public class MarqueeTextView extends View { private final float DEF_TEXT_SIZE = 25.0F;//The default text size private float mSpeed = 3.0F; //The default text scroll speed private boolean isScroll = true; //The default set as auto scroll private Context mContext; private Paint mPaint; private String mText;//This is to display the content private float mTextSize;//This is text size private int mTextColor; //This is text color private float mCoordinateX;//Draw the starting point of the X coordinate private float mCoordinateY;//Draw the starting point of the Y coordinate