android TextView 垂直自動滾動

public class VerticalScrollTextView extends TextView {
    private float step = 0f;
    private Paint mPaint;
    private float width;
    private final List<String> textList = new ArrayList<String>();
    private boolean scrollEnable = false;
    private String mtext;
    private Boolean finish = true;
 
    public VerticalScrollTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    public VerticalScrollTextView(Context context) {
        super(context);
    }
 
    public void setFontSize(float d) {
        this.setTextSize(d);
    }
 
    public void setScrollEnable(boolean enable) {
        scrollEnable = enable;
    }
 
    public void setList(List<String> list) {
        mtext = "";
        for (String text : list) {
            mtext += text;
        }
        setText(mtext);
    }
 
    public void updateUI() {
        finish = false;
        invalidate();
    }
 
    public Boolean getFinish() {
        return finish;
    }
 
    public void setFinish(Boolean finish) {
        this.finish = finish;
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (scrollEnable) {
            width = MeasureSpec.getSize(widthMeasureSpec);
            final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            if (widthMode != MeasureSpec.EXACTLY) {
                throw new IllegalStateException(
                    "ScrollLayout only canmCurScreen run at EXACTLY mode!");
            }
 
            float length = 0;
            if (mtext == null || mtext.length() == 0) {
                return;
            }
 
            textList.clear();
            mPaint = getPaint();
 
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < mtext.length(); i++) {
                if (length < width) {
                    builder.append(mtext.charAt(i));
                    if (mtext.charAt(i) == '\n') {
                        textList.add(builder.toString().substring(0,
                            builder.toString().length() - 1));
                        builder.delete(0, builder.length() - 1);
                        length = mPaint.measureText(mtext.substring(i, i + 1));
                    } else {
                        length += mPaint.measureText(mtext.substring(i, i + 1));
                        if (i == mtext.length() - 1) {
                            textList.add(builder.toString());
                        }
                    }
                } else {
                    textList.add(builder.toString().substring(0, builder.toString().length() - 1));
                    builder.delete(0, builder.length() - 1);
                    length = mPaint.measureText(mtext.substring(i, i + 1));
                    i--;
                }
 
            }
        }
    }
 
    @Override
    public void onDraw(Canvas canvas) {
        if (scrollEnable) {
            if (textList.size() == 0)
                return;
            mPaint = getPaint();
            for (int i = 0; i < textList.size(); i++) {
                String text = textList.get(i);
                float height = this.getHeight() + (i + 1) * mPaint.getTextSize() - step;
                canvas.drawText(text, 0, height, mPaint);
            }
            invalidate();
            step = step + 0.3f;
            if (step >= this.getHeight() + textList.size() * mPaint.getTextSize()) {
                finish = true;
                step = 0;
            }
        } else {
            super.onDraw(canvas);
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章