一个频繁刷新内容的textview

    一个频繁刷新内容的textview,字符再多就会卡顿。

    想做一个类似logcat的日志打印工具,主要用于显示大量不停刷新的文本的文本框,此控件目前只能最大显示4096个字符。字符再多的话,跑一会界面就会比较卡。不知道大家有什么好的方法,欢迎留言。

 

public class ConsoleView extends ScrollView {
    public static final String START_SUFF = "";

    private static final int MAX_CHARACTERS = 4096;
    private static final long TOUCH_TIMEOUT = 4000L;
    private boolean mIsIdle = true;
    private long mLastTouchEventTime;
    private TextView mTextView;

    public ConsoleView(Context context) {
        this(context, null);
    }

    public ConsoleView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.mTextView = new TextView(context);
        this.mTextView.setHorizontallyScrolling(true);
        HorizontalScrollView hsView = new HorizontalScrollView(context);
        hsView.addView(this.mTextView);

        hsView.setHorizontalScrollBarEnabled(false);
        setScrollbarFadingEnabled(false);
        super.addView(hsView);
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        this.mTextView.setTextSize(getResources().getDimensionPixelSize(R.dimen.text_size_small) / metrics.scaledDensity);
        setTextColor(getResources().getColor(R.color.black));
        setBackgroundColor(getResources().getColor(R.color.white));
        clear();
    }

 

    public void append(CharSequence charSequence) {
        if (!(charSequence.toString().endsWith("\r") | charSequence.toString().endsWith("\n"))) {
            charSequence = charSequence + "\r\n";
        }
        CharSequence text = this.mTextView.getText();
        int localLength = text.length();
        int k;

        if (localLength <= MAX_CHARACTERS) {
            if (this.mIsIdle) {
                this.mTextView.setText(charSequence);
                this.mIsIdle = false;
            } else {
                this.mTextView.append(charSequence);
            }
        } else {

            StringBuilder localStringBuilder = new StringBuilder();
            localStringBuilder.append(text);
            k = localLength - 3686;
            while (k < localLength) {
                if (text.charAt(k) == '\n' || text.charAt(k) == '\r') {
                    int l=k+1;
                    if (text.charAt(l) == '\n' || text.charAt(l) == '\r'){
                        l=l+1;
                    }

                    localStringBuilder.replace(0, l, START_SUFF);
                    localStringBuilder.append(charSequence.toString());
                    this.mTextView.setText(localStringBuilder);
                    break;
                }
                k++;
            }
        }

 

        if (System.currentTimeMillis() - this.mLastTouchEventTime > TOUCH_TIMEOUT) {
            post(new Runnable() {
                public void run() {
                    ConsoleView.this.fullScroll(ScrollView.FOCUS_DOWN);
                }
            });
        }
    }

    public CharSequence getText() {
        return mTextView.getText();
    }

   public void setText(CharSequence charSequence) {
        this.mTextView.setText(charSequence);
    }

    public void clear() {
        this.mTextView.setText("");
        this.mIsIdle = true;
        this.mLastTouchEventTime = System.currentTimeMillis() - TOUCH_TIMEOUT;
    }

 

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        this.mLastTouchEventTime = System.currentTimeMillis();
        Context localContext = getContext();
        return ((localContext instanceof Activity)) && (((Activity) localContext).onTouchEvent(event)) || super.onTouchEvent(event);
    }

 

    public void setBackgroundColor(int paramInt) {
        super.setBackgroundColor(paramInt);
        this.mTextView.setBackgroundColor(paramInt);
    }

    public void setTextColor(int color) {
        this.mTextView.setTextColor(color);
    }

}

 

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值