Android - 使用onTouch获取拍摄照片的一部分

时间:2015-12-12 16:48:55

标签: android android-camera android-canvas ontouchlistener

我正在开发一个应用程序,用户可以从相机拍摄照片,然后应该能够拖动图像的一部分进行进一步分析。 应该以某种方式显示所选部分(我正在尝试一个Rect)。 活动如下:

public class PictureChopActivity extends Activity {

private static final String TAG = "PictureChopActivity";

ImageView taken;
Bitmap bm = CameraActivity.bm;
Bitmap tempBitmap;
Canvas tempCanvas;
//just to check...
private static float left_test = 200, top_test = 200, right_test = 500, bottom_test = 500;
private static float left, top, right, bottom;
Paint myPaint = new Paint();

private View.OnTouchListener sTouchListener = new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        Log.d(TAG, "onTouch called");

        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                left = event.getX();
                right = event.getX();
                top = event.getY();
                bottom = event.getY();

                Log.d(TAG, "ACTION_DOWN");

                break;

            case MotionEvent.ACTION_MOVE:

                if (event.getX() > left && event.getY() > bottom) {          //derecha arriba
                    right = event.getX();
                    top = event.getY();
                } else if (event.getX() > left && event.getY() < top) {      //derecha abajo
                    right = event.getX();
                    bottom = event.getY();
                } else if (event.getX() < right && event.getY() > bottom) {  //izquierda arriba
                    left = event.getX();
                    top = event.getY();
                } else if (event.getX() < right && event.getY() < top) {     //izquierda abajo
                    left = event.getX();
                    bottom = event.getY();
                }

                Log.d(TAG, "ACTION_MOVE");
                Log.d(TAG, "left = " + left);
                Log.d(TAG, "top = " + top);
                Log.d(TAG, "right = " + right);
                Log.d(TAG, "bottom = " + bottom);


                break;

            case MotionEvent.ACTION_CANCEL:

                Log.d(TAG, "ACTION_CANCEL");

                break;

            case MotionEvent.ACTION_UP:

                Log.d(TAG, "ACTION_UP");

                break;

        }

        tempCanvas.drawRoundRect(new RectF(left, top, right, bottom), 2, 2, myPaint);
        taken.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));

        return true;
    }

};




@Override
public void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (myPaint == null) {
        Log.d(TAG, "myPaint is null!");
    }

    //Just to check...
    tempBitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    tempCanvas = new Canvas(tempBitmap);
    tempCanvas.drawBitmap(bm, 0, 0, null);
    tempCanvas.drawRoundRect(new RectF(left_test, top_test, right_test, bottom_test), 2, 2, myPaint);
    //here a rect is drawn!!


    FrameLayout fl = new FrameLayout(this);

    taken = new ImageView(this);
    taken.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));
    taken.setOnTouchListener(sTouchListener);
    fl.addView(taken);


    this.setContentView(fl);

}

}

当我拍照并触摸屏幕时,矩形不会出现。

这段代码有什么问题吗?我应该尝试不同的方法吗?

编辑:我创建了一个测试矩形,最初成功绘制。

Logcat:

12-12 20:26:19.133 25944-25944/fogames.thecolourapp D/PictureChopActivity: onTouch called
12-12 20:26:19.133 25944-25944/fogames.thecolourapp D/PictureChopActivity: ACTION_DOWN
12-12 20:26:19.143 25944-25944/fogames.thecolourapp D/PictureChopActivity: onTouch called
12-12 20:26:19.143 25944-25944/fogames.thecolourapp D/PictureChopActivity: ACTION_MOVE
12-12 20:26:19.143 25944-25944/fogames.thecolourapp D/PictureChopActivity: left = 1506.0
12-12 20:26:19.143 25944-25944/fogames.thecolourapp D/PictureChopActivity: top = 666.0
12-12 20:26:19.143 25944-25944/fogames.thecolourapp D/PictureChopActivity: right = 1506.0
12-12 20:26:19.143 25944-25944/fogames.thecolourapp D/PictureChopActivity: bottom = 666.0
12-12 20:26:19.163 25944-25944/fogames.thecolourapp D/PictureChopActivity: onTouch called
12-12 20:26:19.163 25944-25944/fogames.thecolourapp D/PictureChopActivity: ACTION_MOVE
12-12 20:26:19.163 25944-25944/fogames.thecolourapp D/PictureChopActivity: left = 1506.0
12-12 20:26:19.163 25944-25944/fogames.thecolourapp D/PictureChopActivity: top = 666.0
12-12 20:26:19.163 25944-25944/fogames.thecolourapp D/PictureChopActivity: right = 1506.0
12-12 20:26:19.163 25944-25944/fogames.thecolourapp D/PictureChopActivity: bottom = 666.0
12-12 20:26:19.173 25944-25944/fogames.thecolourapp D/PictureChopActivity: onTouch called
12-12 20:26:19.173 25944-25944/fogames.thecolourapp D/PictureChopActivity: ACTION_MOVE
12-12 20:26:19.173 25944-25944/fogames.thecolourapp D/PictureChopActivity: left = 1506.0
12-12 20:26:19.173 25944-25944/fogames.thecolourapp D/PictureChopActivity: top = 666.0
12-12 20:26:19.173 25944-25944/fogames.thecolourapp D/PictureChopActivity: right = 1506.0
12-12 20:26:19.173 25944-25944/fogames.thecolourapp D/PictureChopActivity: bottom = 666.0
12-12 20:26:19.193 25944-25944/fogames.thecolourapp D/PictureChopActivity: onTouch called
12-12 20:26:19.193 25944-25944/fogames.thecolourapp D/PictureChopActivity: ACTION_MOVE
12-12 20:26:19.193 25944-25944/fogames.thecolourapp D/PictureChopActivity: left = 1506.0
12-12 20:26:19.193 25944-25944/fogames.thecolourapp D/PictureChopActivity: top = 666.0
12-12 20:26:19.193 25944-25944/fogames.thecolourapp D/PictureChopActivity: right = 1506.0
12-12 20:26:19.193 25944-25944/fogames.thecolourapp D/PictureChopActivity: bottom = 666.0
12-12 20:26:19.213 25944-25944/fogames.thecolourapp D/PictureChopActivity: onTouch called
12-12 20:26:19.213 25944-25944/fogames.thecolourapp D/PictureChopActivity: ACTION_MOVE
12-12 20:26:19.213 25944-25944/fogames.thecolourapp D/PictureChopActivity: left = 1506.0
12-12 20:26:19.213 25944-25944/fogames.thecolourapp D/PictureChopActivity: top = 666.0
12-12 20:26:19.213 25944-25944/fogames.thecolourapp D/PictureChopActivity: right = 1506.0
12-12 20:26:19.213 25944-25944/fogames.thecolourapp D/PictureChopActivity: bottom = 666.0
12-12 20:26:19.223 25944-25944/fogames.thecolourapp D/PictureChopActivity: onTouch called
12-12 20:26:19.223 25944-25944/fogames.thecolourapp D/PictureChopActivity: ACTION_MOVE
12-12 20:26:19.223 25944-25944/fogames.thecolourapp D/PictureChopActivity: left = 1497.2212
12-12 20:26:19.223 25944-25944/fogames.thecolourapp D/PictureChopActivity: top = 666.0
12-12 20:26:19.223 25944-25944/fogames.thecolourapp D/PictureChopActivity: right = 1506.0
12-12 20:26:19.223 25944-25944/fogames.thecolourapp D/PictureChopActivity: bottom = 665.22125
12-12 20:26:19.243 25944-25944/fogames.thecolourapp D/PictureChopActivity: onTouch called
12-12 20:26:19.243 25944-25944/fogames.thecolourapp D/PictureChopActivity: ACTION_MOVE
12-12 20:26:19.243 25944-25944/fogames.thecolourapp D/PictureChopActivity: left = 1494.1829
12-12 20:26:19.243 25944-25944/fogames.thecolourapp D/PictureChopActivity: top = 666.0
12-12 20:26:19.243 25944-25944/fogames.thecolourapp D/PictureChopActivity: right = 1506.0
12-12 20:26:19.243 25944-25944/fogames.thecolourapp D/PictureChopActivity: bottom = 665.0
12-12 20:26:19.263 25944-25944/fogames.thecolourapp D/PictureChopActivity: onTouch called
12-12 20:26:19.263 25944-25944/fogames.thecolourapp D/PictureChopActivity: ACTION_MOVE
12-12 20:26:19.263 25944-25944/fogames.thecolourapp D/PictureChopActivity: left = 1491.1027
12-12 20:26:19.263 25944-25944/fogames.thecolourapp D/PictureChopActivity: top = 666.0
12-12 20:26:19.263 25944-25944/fogames.thecolourapp D/PictureChopActivity: right = 1506.0
12-12 20:26:19.263 25944-25944/fogames.thecolourapp D/PictureChopActivity: bottom = 664.0
12-12 20:26:19.283 25944-25944/fogames.thecolourapp D/PictureChopActivity: onTouch called
12-12 20:26:19.283 25944-25944/fogames.thecolourapp D/PictureChopActivity: ACTION_MOVE
12-12 20:26:19.283 25944-25944/fogames.thecolourapp D/PictureChopActivity: left = 1486.6072
12-12 20:26:19.283 25944-25944/fogames.thecolourapp D/PictureChopActivity: top = 666.0
12-12 20:26:19.283 25944-25944/fogames.thecolourapp D/PictureChopActivity: right = 1506.0
12-12 20:26:19.283 25944-25944/fogames.thecolourapp D/PictureChopActivity: bottom = 664.0
12-12 20:26:19.293 25944-25944/fogames.thecolourapp D/PictureChopActivity: onTouch called
12-12 20:26:19.293 25944-25944/fogames.thecolourapp D/PictureChopActivity: ACTION_MOVE
12-12 20:26:19.293 25944-25944/fogames.thecolourapp D/PictureChopActivity: left = 1483.0435
12-12 20:26:19.293 25944-25944/fogames.thecolourapp D/PictureChopActivity: top = 666.0
12-12 20:26:19.293 25944-25944/fogames.thecolourapp D/PictureChopActivity: right = 1506.0
12-12 20:26:19.293 25944-25944/fogames.thecolourapp D/PictureChopActivity: bottom = 664.0
12-12 20:26:19.313 25944-25944/fogames.thecolourapp D/PictureChopActivity: onTouch called
12-12 20:26:19.313 25944-25944/fogames.thecolourapp D/PictureChopActivity: ACTION_MOVE
12-12 20:26:19.313 25944-25944/fogames.thecolourapp D/PictureChopActivity: left = 1480.0131
12-12 20:26:19.313 25944-25944/fogames.thecolourapp D/PictureChopActivity: top = 666.0
12-12 20:26:19.313 25944-25944/fogames.thecolourapp D/PictureChopActivity: right = 1506.0
12-12 20:26:19.313 25944-25944/fogames.thecolourapp D/PictureChopActivity: bottom = 664.0
12-12 20:26:19.333 25944-25944/fogames.thecolourapp D/PictureChopActivity: onTouch called
12-12 20:26:19.333 25944-25944/fogames.thecolourapp D/PictureChopActivity: ACTION_MOVE
12-12 20:26:19.333 25944-25944/fogames.thecolourapp D/PictureChopActivity: left = 1476.9796
12-12 20:26:19.333 25944-25944/fogames.thecolourapp D/PictureChopActivity: top = 666.0
12-12 20:26:19.333 25944-25944/fogames.thecolourapp D/PictureChopActivity: right = 1506.0
12-12 20:26:19.333 25944-25944/fogames.thecolourapp D/PictureChopActivity: bottom = 664.0
12-12 20:26:19.343 25944-25944/fogames.thecolourapp D/PictureChopActivity: onTouch called
12-12 20:26:19.343 25944-25944/fogames.thecolourapp D/PictureChopActivity: ACTION_MOVE
12-12 20:26:19.343 25944-25944/fogames.thecolourapp D/PictureChopActivity: left = 1473.9349
12-12 20:26:19.343 25944-25944/fogames.thecolourapp D/PictureChopActivity: top = 666.0
12-12 20:26:19.343 25944-25944/fogames.thecolourapp D/PictureChopActivity: right = 1506.0
12-12 20:26:19.343 25944-25944/fogames.thecolourapp D/PictureChopActivity: bottom = 664.0
12-12 20:26:19.363 25944-25944/fogames.thecolourapp D/PictureChopActivity: onTouch called
12-12 20:26:19.363 25944-25944/fogames.thecolourapp D/PictureChopActivity: ACTION_MOVE
12-12 20:26:19.363 25944-25944/fogames.thecolourapp D/PictureChopActivity: left = 1470.8989
12-12 20:26:19.363 25944-25944/fogames.thecolourapp D/PictureChopActivity: top = 666.0
12-12 20:26:19.363 25944-25944/fogames.thecolourapp D/PictureChopActivity: right = 1506.0
12-12 20:26:19.363 25944-25944/fogames.thecolourapp D/PictureChopActivity: bottom = 664.0
12-12 20:26:19.383 25944-25944/fogames.thecolourapp D/PictureChopActivity: onTouch called
12-12 20:26:19.383 25944-25944/fogames.thecolourapp D/PictureChopActivity: ACTION_MOVE
12-12 20:26:19.383 25944-25944/fogames.thecolourapp D/PictureChopActivity: left = 1466.3025
12-12 20:26:19.383 25944-25944/fogames.thecolourapp D/PictureChopActivity: top = 666.0
12-12 20:26:19.383 25944-25944/fogames.thecolourapp D/PictureChopActivity: right = 1506.0
12-12 20:26:19.383 25944-25944/fogames.thecolourapp D/PictureChopActivity: bottom = 663.0
12-12 20:26:19.393 25944-25944/fogames.thecolourapp D/PictureChopActivity: onTouch called
12-12 20:26:19.393 25944-25944/fogames.thecolourapp D/PictureChopActivity: ACTION_MOVE
12-12 20:26:19.393 25944-25944/fogames.thecolourapp D/PictureChopActivity: left = 1461.7457
12-12 20:26:19.393 25944-25944/fogames.thecolourapp D/PictureChopActivity: top = 666.0
12-12 20:26:19.393 25944-25944/fogames.thecolourapp D/PictureChopActivity: right = 1506.0
12-12 20:26:19.393 25944-25944/fogames.thecolourapp D/PictureChopActivity: bottom = 663.0

topright值不变。 ACTION_DOWN应存储第一个角落,ACTION_MOVE应存储相反角落,但结果不符合预期。

1 个答案:

答案 0 :(得分:0)

我终于找到了错误,这很简单!

我假设Y轴方向错误:事实上,top < bottom,所以条件必须改变:

if (event.getX() > left && event.getY() < bottom) {          //extend right up
                    right = event.getX();
                    top = event.getY();
                } else if (event.getX() > left && event.getY() > top) {      //extend right down
                    right = event.getX();
                    bottom = event.getY();
                } else if (event.getX() < right && event.getY() < bottom) {  //extend left up
                    left = event.getX();
                    top = event.getY();
                } else if (event.getX() < right && event.getY() > top) {     //extend left down
                    left = event.getX();
                    bottom = event.getY();
                }