水平滚动:从右往左。
垂直滚动:从下往上。
先来张效果图:
控件源码:
package com.otess.marquee;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;
import java.util.ArrayList;
import java.util.List;
/**
* Author:SkySmile
* Date:2020/1/14
* Description:跑马灯效果的View,目前可以从下往上、从右往左滚动
*/
public class MarqueeView extends android.support.v7.widget.AppCompatTextView {
//滚动方向
//不滚动
public static final int SCROLL_NO = 1;
//从下往上
public static final int SCROLL_BT = 2;
//从右往左
public static final int SCROLL_RL = 3;
//滚动速度
//慢速
public static final int SPEED_SLOW = 4;
//中速
public static final int SPEED_NORMAL = 5;
//快速
public static final int SPEED_FAST = 6;
//特快
public static final int SPEED_EXPRESS = 7;
//垂直滚动需要的数据
private float lineSpace;
private float verticalSpeed = 0.1f;
private List<String> textList = new ArrayList<>();
private StringBuilder textBuilder = new StringBuilder();
//水平滚动需要的数据
private float horizontalSpeed = 2f;
private Rect rect;
private Paint paint;
//默认不滚动
private int scrollType;
//每次更滚动的距离
private float scrollStep = 0f;
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);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MarqueeView);
scrollType = array.getInt(R.styleable.MarqueeView_scrollType, SCROLL_NO);
int scrollSpeed = array.getInt(R.styleable.MarqueeView_speedType, SPEED_NORMAL);
int color = array.getColor(R.styleable.MarqueeView_textColor, 0x000000);
lineSpace = array.getInt(R.styleable.MarqueeView_lineSpace,