从onTouchListener调用TextView setText()会卡住UI

时间:2018-04-06 11:01:28

标签: android textview handler ui-thread

以下是我的示例代码:

  @Override
public boolean onTouchEvent(MotionEvent e) {
    float center = mScrubberX + mScrubberWidth / 2;
    float handleLeft = center - HANDLE_WIDTH;
    float handleRight = center + HANDLE_WIDTH;
    switch (e.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (e.getX() > handleLeft && e.getX() < handleRight) {
                mActionDown = true;
                mActionDownX = e.getX() - mScrubberX;
            }
            break;
        case MotionEvent.ACTION_MOVE:
            float newLeft = e.getX() - mActionDownX;
            float newRight = newLeft + mScrubberWidth;

            if (mActionDown && !mIsStreaming) {
                if (newLeft > mScrubberContainer.getLeft() &&
                        newRight < mScrubberContainer.getRight()) {
                    mScrubberX = e.getX() - mActionDownX;
                    textView.setText(""+mScrubberX)
                } else if (newRight < mScrubberContainer.getRight()) {
                    mScrubberX = mScrubberContainer.getLeft();
                } else if (newLeft > mScrubberContainer.getLeft()) {
                    mScrubberX = mScrubberContainer.getRight() - mScrubberWidth;
                }
            }
            break;
        case MotionEvent.ACTION_UP:
            mActionDown = false;
            break;
        default:
            //Do nothing
    }
    invalidate();
    return true;
}

请检查行“textView.setText(”“+ mScrubberX)”触发了用户界面。我试过把它放在Handler,Asynctask.executor,runOnUIThread但没有任何效果。 请提出一些解决方案来解决这个问题。

0 个答案:

没有答案