Android:如何提高按钮点击响应速度?

时间:2016-11-28 09:09:23

标签: android

我正在尝试为Android制作时钟应用程序。我设法实现了秒表功能,但它似乎仍然充满了问题。

响应太慢,即当我点击开始按钮时响应很慢,所以它不会立即启动。单击停止按钮时也是如此。

目前提供的方法还不够吗?还是有更好的解决方案替代方案?请帮帮我。

public class StopWatchView extends LinearLayout{

    private int tenMSecs = 0;
    private Timer timer = new Timer();
    private TimerTask timerTask = null;
    private TimerTask showTimeTask = null;

    private TextView tvHour, tvMinute, tvSecond, tvMSecond;
    private Button btnSWStart, btnSWResume, btnSWReset, btnSWPause, btnSWRecord;
    private ListView lvWatchTimeList;
    private ArrayAdapter<String> adapter;

    private static final int MSG_WHAT_SHOW_TIME = 1;

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

    public StopWatchView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public StopWatchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate(){
        super.onFinishInflate();

        tvHour = (TextView)findViewById(R.id.tvHour);
        tvHour.setText("0");
        tvMinute = (TextView)findViewById(R.id.tvMinute);
        tvMinute.setText("0");
        tvSecond = (TextView)findViewById(R.id.tvSecond);
        tvSecond.setText("0");
        tvMSecond = (TextView)findViewById(R.id.tvMSceond);
        tvMSecond.setText("0");

        btnSWRecord = (Button)findViewById(R.id.btnSWRecord);
        btnSWRecord.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.insert(String.format("%d:%d:%d.%d",
                        tenMSecs/100/60/60,
                        tenMSecs/100/60%60,
                        tenMSecs/100%60,
                        tenMSecs%100),
                        0);
            }
        });

        btnSWPause = (Button)findViewById(R.id.btnSWPause);
        btnSWPause.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                stopTimer();

                btnSWPause.setVisibility(View.GONE);
                btnSWResume.setVisibility(View.VISIBLE);
                btnSWReset.setVisibility(View.VISIBLE);
                btnSWRecord.setVisibility(View.GONE);
            }
        });

        btnSWReset = (Button)findViewById(R.id.btnSWReset);
        btnSWReset.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                stopTimer();
                tenMSecs = 0;
                adapter.clear();

                btnSWStart.setVisibility(View.VISIBLE);
                btnSWPause.setVisibility(View.GONE);
                btnSWReset.setVisibility(View.GONE);
                btnSWRecord.setVisibility(View.GONE);
                btnSWResume.setVisibility(View.GONE);
            }
        });

        btnSWResume = (Button)findViewById(R.id.btnSWResume);
        btnSWResume.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startTimer();

                btnSWResume.setVisibility(View.GONE);
                btnSWReset.setVisibility(View.GONE);
                btnSWRecord.setVisibility(View.VISIBLE);
                btnSWPause.setVisibility(View.VISIBLE);
            }
        });

        btnSWStart = (Button)findViewById(R.id.btnSWStart);
        btnSWStart.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startTimer();

                btnSWStart.setVisibility(View.GONE);
                btnSWPause.setVisibility(View.VISIBLE);
                btnSWRecord.setVisibility(View.VISIBLE);
            }
        });

        btnSWRecord.setVisibility(View.GONE);
        btnSWPause.setVisibility(View.GONE);
        btnSWReset.setVisibility(View.GONE);
        btnSWResume.setVisibility(View.GONE);

        lvWatchTimeList = (ListView)findViewById(R.id.lvWatchTimeList);
        adapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_list_item_1);
        lvWatchTimeList.setAdapter(adapter);

        showTimeTask = new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(MSG_WHAT_SHOW_TIME);
            }
        };
        timer.schedule(showTimeTask,200,200);
    }

    private void startTimer(){
        if(timerTask == null){
            timerTask = new TimerTask() {
                @Override
                public void run() {
                    tenMSecs++;
                }
            };
            timer.schedule(timerTask,10,10);
        }
    }

    private void stopTimer(){
        if(timerTask != null){
            timerTask.cancel();
            timerTask = null;
        }
    }

    public void onDestory(){
        timer.cancel();
    }

    private Handler handler = new Handler(){
        public void handleMessage(Message msg){
            switch (msg.what){
                case MSG_WHAT_SHOW_TIME:
                    tvHour.setText(tenMSecs/100/60/60+"");
                    tvMinute.setText(tenMSecs/100/60%60+"");
                    tvSecond.setText(tenMSecs/100%60+"");
                    tvMSecond.setText(tenMSecs%100+"");
                    break;
                default:
                    break;
            }
        };
    };
}

2 个答案:

答案 0 :(得分:1)

如果您的按钮位于Scrollable父级中,则会导致延迟响应。尝试不可滚动的容器或覆盖OnTouchListener

   button.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                button.setPressed(true);
            }
            return onTouchEvent(motionEvent);
        }
    });

答案 1 :(得分:0)

对于遇到此问题的其他人,如果您使用的是com.gc.materialdesign.views.ButtonFlat,则可以提高波动速度以提高点击响应速度,例如:

button.rippleSpeed *= 12 // or some other number of similar magnitude