android github 源码学习 --文本滚动效果 TextViewScroll



文本滚动效果 TextViewScroll : https://github.com/LiLinXiang/TextViewScroll


比较有帮助的知识点:

// 如果有多行文字,则获取最长的一行文字宽度
String[] lineContent = getText().toString().split("\n");

//文本测量宽度
mTextWidth =(int) mPaint.measureText(lineContent[maxLineNumber]);

// 获取字体总高度
private int getTextHeight() {
    return getLineHeight() *getLineCount();
}

//文本滚动
@Override
protected void onDraw(Canvas canvas) {
    scrollType(scrollType);
    super.onDraw(canvas);
}

private void scrollType(int type) {
    if (scrollStatus) {
        switch (type) {
            case FROM_RIGHT:
                // 右到左
                if (x >=getTextWidth()){
                    x = -getWidth();
                }
                scrollTo(x, 0);
                x = x + speed;
                postInvalidate();
                break;
            case FROM_LEFT:
                // 左到右
                if (x <= -getWidth()){
                    x = getTextWidth();
                }
                scrollTo(x, 0);
                x = x - speed;
                postInvalidate();
            case FROM_TOP:
                // 上到下
                if (y <= -getHeight()) {
                    y = getTextHeight();
                }
                scrollTo(0, y);
                y = y - speed;
                postInvalidate();
                break;
            case FROM_BOTTOM:
                // 下到上
                if (y >= getTextHeight()) {
                    y = -getHeight();
                }
                scrollTo(0, y);
                y = y + speed;
                postInvalidate();
                break;
            default:
                break;
        }
    }
}

参考文件:

package com.example.lincoln.textviewscroll;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by Lincoln on 2016-11-22.
 */

public class TextViewScroll extends TextView {
    private int x, y;
    // 滚动速度
    private int speed = 5;
    // 字幕从那边出来
    public static final int FROM_RIGHT = 0;
    public static final int FROM_LEFT = 1;
    public static final int FROM_TOP = 2;
    public static final int FROM_BOTTOM = 3;
    private int scrollType = FROM_RIGHT;
    public static final boolean START = true;
    public static final boolean STOP = false;
    private boolean scrollStatus = START;


    public TextViewScroll(Context context) {
        super(context);
    }

    public TextViewScroll(Context context, AttributeSet attrs) {
        super(context, attrs);
        x = getTextWidth();
        y = getTextHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        scrollType(scrollType);
        super.onDraw(canvas);
    }

    private void scrollType(int type) {
        if (scrollStatus) {
            switch (type) {
                case FROM_RIGHT:
                    // 右到左
                    if (x >= getTextWidth()) {
                        x = -getWidth();
                    }
                    scrollTo(x, 0);
                    x = x + speed;
                    postInvalidate();
                    break;
                case FROM_LEFT:
                    // 左到右
                    if (x <= -getWidth()) {
                        x = getTextWidth();
                    }
                    scrollTo(x, 0);
                    x = x - speed;
                    postInvalidate();
                    break;
                case FROM_TOP:
                    // 上到下
                    if (y <= -getHeight()) {
                        y = getTextHeight();
                    }
                    scrollTo(0, y);
                    y = y - speed;
                    postInvalidate();
                    break;
                case FROM_BOTTOM:
                    // 下到上
                    if (y >= getTextHeight()) {
                        y = -getHeight();
                    }
                    scrollTo(0, y);
                    y = y + speed;
                    postInvalidate();
                    break;

                default:
                    break;
            }
        }

    }

    // 获取字体行宽度
    private int getTextWidth() {
        int mTextWidth;
        Paint mPaint = getPaint();
        if (getLineCount() > 1) {
            // 如果有多行文字,则获取最长的一行文字宽度
            String[] lineContent = getText().toString().split("\n");
            int maxLine = 0, maxLineNumber = 0;
            for (int i = 0; i < lineContent.length; i++) {
                if (lineContent[i].length() > maxLine) {
                    maxLine = lineContent[i].length();
                    maxLineNumber = i;
                }
            }
            mTextWidth = (int) mPaint.measureText(lineContent[maxLineNumber]);
        } else {
            mTextWidth = (int) mPaint.measureText(getText().toString());
        }
        return mTextWidth;
    }

    // 获取字体总高度
    private int getTextHeight() {
        return getLineHeight() * getLineCount();
    }

    public int getScrollType() {
        return scrollType;
    }

    public void setScrollType(int scrollType) {
        this.scrollType = scrollType;
        setScrollStatus(START);
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public boolean isScrollStatus() {
        return scrollStatus;
    }

    public void setScrollStatus(boolean scrollStatus) {
        this.scrollStatus = scrollStatus;
        postInvalidate();
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值