您的位置:首页 > 移动开发 > Android开发

一个频繁刷新内容的textview

2016-02-19 14:10 956 查看
    一个频繁刷新内容的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);

    }

}

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android textview