0

我创建的自定义相机活动有问题。当用户按下按钮(Action_Down)时,我希望有一个软按钮聚焦相机,如果用户将他/她的手指抬起(Action_Up)到图像视图上则拍照,然后如果用户拖出则取消所有操作按钮区域的/提升到按钮之外。

下面是我注册到 ImageView 的触摸监听器;

public boolean onTouch(View v, MotionEvent event) {

    Rect rect = new Rect(v.getLeft(), v.getTop(), v.getRight(),
            v.getBottom());

    StringBuilder sb = new StringBuilder();
    sb.append("rect (l,t,r,b) - " + rect.left + "," + rect.top + ","
            + rect.right + "," + rect.bottom + " x-Event " + event.getX()
            + " y-event " + event.getY() + " event code"
            + event.getAction() + " rect Cont "
            + rect.contains((int) event.getRawX(), (int) event.getRawY()));
    Log.d("Touch", sb.toString());

    switch (v.getId()) {
    case R.id.CameraShutterButton: {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            softShutterPressed=true;

            shutter.setImageDrawable(getResources().getDrawable(
                    R.drawable.camera_shutter_pressed));
            if (focusingFlag == false) {
                focusingFlag = true;
                autoFocusIndicator.setImageDrawable(getResources()
                        .getDrawable(R.drawable.autofocus_focusing));
                autoFocusIndicator.setVisibility(View.VISIBLE);
                camera.autoFocus(this);
            }
            shutter.invalidate();
            return true;
        }

        if (event.getAction() == MotionEvent.ACTION_UP
                && rect.contains((int) event.getRawX(),
                        (int) event.getRawY())&&softShutterPressed) {

            shutter.setImageDrawable(getResources().getDrawable(
                    R.drawable.camera_shutter));
            softShutterPressed=false;
            toast.setText("Action Up");
            toast.show();
            if (focusedFlag == true && pictureEventOneShot == false) {

                camera.takePicture(null, null, null, this);

            }

            if (focusedFlag == false && focusingFlag == true
                    && pictureEventOneShot == false) {
                captureWhenFocused = true;
            }

            if (focusedFlag == false && focusingFlag == false
                    && pictureEventOneShot == false) {
                captureWhenFocused = true;
                focusingFlag = true;
                autoFocusIndicator.setImageDrawable(getResources()
                        .getDrawable(R.drawable.autofocus_focusing));
                autoFocusIndicator.setVisibility(View.VISIBLE);

                camera.autoFocus(this);
            }

            shutter.invalidate();
            return true;

        }

        if ((event.getAction() == MotionEvent.ACTION_MOVE && !rect
                .contains((int) event.getRawX(), (int) event.getRawY()))
                || (event.getAction() == MotionEvent.ACTION_UP && !rect
                        .contains((int) event.getRawX(),
                                (int) event.getRawY()))) {
            shutter.setImageDrawable(getResources().getDrawable(
                    R.drawable.camera_shutter));
            softShutterPressed=false;

            if (captureWhenFocused != true) {

                focusedFlag = false;
                focusingFlag = false;
                camera.cancelAutoFocus();
                autoFocusIndicator.setImageDrawable(getResources()
                        .getDrawable(R.drawable.autofocus_focusing));
                autoFocusIndicator.setVisibility(View.GONE);
            } else {

                // Nothing
            }

            shutter.invalidate();
            return true;
        }


    }

    default:
        break;


    }

    return false;
}

我遇到的问题是,当垃圾收集发生时,即使用户仍然将手指放在按钮上,也会错误地触发事件 Action_UP。以下是 logcat 输出:

10-29 10:57:00.657: D/Touch(4455): rect (l,t,r,b) - 704,24,819,139 x-Event 56.75714 y-event 39.312866 event code2 rect Cont true
10-29 10:57:00.712: D/Touch(4455): rect (l,t,r,b) - 704,24,819,139 x-Event 56.75714 y-event 39.80368 event code2 rect Cont true
10-29 10:57:00.774: D/Touch(4455): rect (l,t,r,b) - 704,24,819,139 x-Event 56.75714 y-event 40.294464 event code2 rect Cont true
10-29 10:57:00.938: D/Touch(4455): rect (l,t,r,b) - 704,24,819,139 x-Event 56.75714 y-event 40.294464 event code1 rect Cont true
10-29 10:57:02.579: D/dalvikvm(4455): GC_FOR_MALLOC freed 1557K, 57% free 3124K/7175K, external 3166K/3417K, paused 82ms
10-29 10:57:02.696: D/dalvikvm(4455): GC_CONCURRENT freed 6K, 43% free 4099K/7175K, external 2885K/3417K, paused 2ms+6ms

我已经验证,当触发垃圾收集时(通过在 DDMS 视图中转储堆)会触发此错误操作。

更新日志

下面是当我按下按钮强制进行堆转储,然后在垃圾收集之前看到错误的操作事件时的日志。

10-29 12:57:08.884: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 55.88568 y-event 59.92639 event code0 rect Cont true
10-29 12:57:08.923: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 55.88568 y-event 59.92639 event code2 rect Cont true
10-29 12:57:08.985: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 55.88568 y-event 59.92639 event code2 rect Cont true
10-29 12:57:09.477: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 55.88568 y-event 59.92639 event code2 rect Cont true
10-29 12:57:14.430: D/ddm-heap(9532): Heap dump: [DDMS]
10-29 12:57:15.063: I/dalvikvm(9532): hprof: dumping heap strings to "[DDMS]".
10-29 12:57:16.610: I/dalvikvm(9532): hprof: heap dump completed (3503KB)
10-29 12:57:16.618: D/dalvikvm(9532): GC_HPROF_DUMP_HEAP freed 44K, 57% free 3121K/7239K, external 3166K/3417K, paused 2180ms
10-29 12:57:22.137: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 55.88568 y-event 59.92639 event code2 rect Cont true
10-29 12:57:22.173: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 55.88568 y-event 59.92639 event code2 rect Cont true
10-29 12:57:22.243: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 55.88568 y-event 59.92639 event code2 rect Cont true
10-29 12:57:22.266: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 55.88568 y-event 59.92639 event code2 rect Cont true
10-29 12:57:22.332: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 55.88568 y-event 59.92639 event code2 rect Cont true
10-29 12:57:22.353: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 55.88568 y-event 59.92639 event code2 rect Cont true
10-29 12:57:22.829: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 55.88568 y-event 59.92639 event code2 rect Cont true
10-29 12:57:22.860: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 55.88568 y-event 59.92639 event code2 rect Cont true
10-29 12:57:23.649: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 54.52762 event code2 rect Cont true
10-29 12:57:23.673: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 53.54602 event code2 rect Cont true
10-29 12:57:23.712: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 52.07361 event code2 rect Cont true
10-29 12:57:23.720: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 51.582825 event code2 rect Cont true
10-29 12:57:23.751: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 50.601227 event code2 rect Cont true
10-29 12:57:23.766: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 49.61963 event code2 rect Cont true
10-29 12:57:23.782: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 49.128845 event code2 rect Cont true
10-29 12:57:23.798: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 48.147247 event code2 rect Cont true
10-29 12:57:23.813: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 47.16565 event code2 rect Cont true
10-29 12:57:23.829: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 46.674835 event code2 rect Cont true
10-29 12:57:23.837: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 46.18405 event code2 rect Cont true
10-29 12:57:23.845: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 45.693237 event code2 rect Cont true
10-29 12:57:23.868: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 44.71164 event code2 rect Cont true
10-29 12:57:23.884: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 44.220856 event code2 rect Cont true
10-29 12:57:23.907: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 43.730072 event code2 rect Cont true
10-29 12:57:23.915: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 43.239258 event code2 rect Cont true
10-29 12:57:23.938: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 42.748474 event code2 rect Cont true
10-29 12:57:24.016: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 42.748474 event code2 rect Cont true
10-29 12:57:24.055: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 44.71164 event code2 rect Cont true
10-29 12:57:24.079: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 45.202454 event code2 rect Cont true
10-29 12:57:24.102: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 45.693237 event code2 rect Cont true
10-29 12:57:24.102: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 46.18405 event code2 rect Cont true
10-29 12:57:24.118: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 46.674835 event code2 rect Cont true
10-29 12:57:24.134: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 47.16565 event code2 rect Cont true
10-29 12:57:24.157: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 47.656433 event code2 rect Cont true
10-29 12:57:24.165: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 49.785706 y-event 48.147247 event code2 rect Cont true
10-29 12:57:24.188: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.914246 y-event 48.63803 event code2 rect Cont true
10-29 12:57:24.212: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.914246 y-event 49.128845 event code2 rect Cont true
10-29 12:57:24.243: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.914246 y-event 49.61963 event code2 rect Cont true
10-29 12:57:24.259: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.914246 y-event 50.110413 event code2 rect Cont true
10-29 12:57:24.283: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.914246 y-event 50.601227 event code2 rect Cont true
10-29 12:57:24.329: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.914246 y-event 51.09201 event code2 rect Cont true
10-29 12:57:24.360: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.042847 y-event 51.09201 event code2 rect Cont true
10-29 12:57:24.384: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.042847 y-event 51.582825 event code2 rect Cont true
10-29 12:57:24.399: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.042847 y-event 52.07361 event code2 rect Cont true
10-29 12:57:24.438: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.042847 y-event 52.564423 event code2 rect Cont true
10-29 12:57:24.470: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.042847 y-event 53.055206 event code2 rect Cont true
10-29 12:57:24.493: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.042847 y-event 53.54602 event code2 rect Cont true
10-29 12:57:24.509: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.042847 y-event 54.036804 event code2 rect Cont true
10-29 12:57:24.532: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.042847 y-event 54.52762 event code2 rect Cont true
10-29 12:57:24.556: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.042847 y-event 55.018402 event code2 rect Cont true
10-29 12:57:24.579: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.042847 y-event 55.509186 event code2 rect Cont true
10-29 12:57:24.618: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.042847 y-event 56.0 event code2 rect Cont true
10-29 12:57:24.815: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.042847 y-event 56.490784 event code2 rect Cont true
10-29 12:57:26.274: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.042847 y-event 56.490784 event code2 rect Cont true
10-29 12:57:26.306: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.042847 y-event 56.490784 event code2 rect Cont true
10-29 12:57:26.391: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.042847 y-event 56.490784 event code2 rect Cont true
10-29 12:57:27.446: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.042847 y-event 54.52762 event code2 rect Cont true
10-29 12:57:30.548: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.042847 y-event 54.036804 event code2 rect Cont true
10-29 12:57:41.118: D/Touch(9532): rect (l,t,r,b) - 704,24,819,139 x-Event 48.042847 y-event 54.036804 event code1 rect Cont true
10-29 12:57:42.720: D/dalvikvm(9532): GC_FOR_MALLOC freed 647K, 57% free 3123K/7239K, external 2941K/3417K, paused 80ms
10-29 12:57:42.727: I/dalvikvm-heap(9532): Grow heap (frag case) to 9.082MB for 1128458-byte allocation
10-29 12:57:42.774: D/dalvikvm(9532): GC_FOR_MALLOC freed <1K, 42% free 4225K/7239K, external 2941K/3417K, paused 35ms
10-29 12:57:42.860: D/dalvikvm(9532): GC_CONCURRENT freed 5K, 42% free 4226K/7239K, external 2941K/3417K, paused 2ms+5ms

我的问题是我有什么选择来防止这种行为?有没有办法在一段时间内防止垃圾收集?

4

0 回答 0