在背景上拖动对象而不是在屏幕周围

时间:2015-10-08 08:52:03

标签: android

我正在寻找一种解决方案,让一些符号(数量为5个)在背景imageview上移动。

因此,如果我移动背景,符号必须保留在我放弃背景的位置上。

我想知道的是,要搜索什么才能获得正确的信息。

这是我到目前为止所做的:

public class MyTouchListener implements View.OnTouchListener {

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        switch (view.getId()) {
            case R.id.background:
                ImageView view2 = (ImageView) view;
                view2.setScaleType(ImageView.ScaleType.MATRIX);
                float scale;

                dumpEvent(event);
                //Handle touch events here...

                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN: //first finger down only
                        savedMatrix.set(matrix);
                        start.set(event.getX(), event.getY());
                        Log.d(TAG, "mode=DRAG");
                        mode = DRAG;
                        break;
                    case MotionEvent.ACTION_UP://first finger lifted

                    case MotionEvent.ACTION_POINTER_UP: //second finger lifted
                        mode = NONE;
                        Log.d(TAG, "mode=NONE");
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN: //first and second finger down
                        oldDist = spacing(event);
                        Log.d(TAG, "oldDist=" + oldDist);
                        if (oldDist > 10f) {
                            savedMatrix.set(matrix);
                            midPoint(mid, event);
                            mode = ZOOM;
                            Log.d(TAG, "mode=ZOOM");
                        }
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (mode == DRAG) {
                            //test.set(skizze.getX(),skizze.getY());
                            //feuerloescher.setX(test.x);
                            //feuerloescher.setY(test.y);
                            int[] locations = new int[2];
                            feuerloescher.getLocationOnScreen(locations);

                            Log.d("Test",String.valueOf(locations[0]) + " " + String.valueOf(locations[1]));

                            matrix.set(savedMatrix);
                            matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); //create the transformation in the matrix of points
                        } else if (mode == ZOOM) {
                            float[] f = new float[9];
                            //pinch zooming
                            float newDist = spacing(event);
                            Log.d(TAG, "newDist=" + newDist);

                            if (newDist > 10f) {
                                matrix.set(savedMatrix);
                                scale = newDist / oldDist; //setting the scaling of the matrix...
                                //if scale > 1 means zoom in
                                // if scale < 1 means zoom out
                                matrix.postScale(scale, scale, mid.x, mid.y);
                                //scale = Math.max(MIN_ZOOM, Math.min(scale, MAX_ZOOM));
                            }
                            matrix.getValues(f);
                            float scaleX = f[Matrix.MSCALE_X];
                            float scaleY = f[Matrix.MSCALE_Y];

                            //Einstellungen minimaler oder maximaler zoom
                            if (scaleX <= MIN_ZOOM) {
                                matrix.postScale((MIN_ZOOM) / scaleX, (MIN_ZOOM) / scaleY, mid.x, mid.y);

                            } else if (scaleX >= MAX_ZOOM) {
                                matrix.postScale((MAX_ZOOM) / scaleX, (MAX_ZOOM) / scaleY, mid.x, mid.y);
                            }
                        }
                        break;
                }
                view2.setImageMatrix(matrix); //display the transformation on screen
                break;
            case R.id.symbol1:
                dumpEvent(event);
                final int X = (int) event.getRawX();
                final int Y = (int) event.getRawY();
                //Definition MotionEvent.ACTION_MASK: Bit mask of the parts of the action code that are the action itself.
                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:
                        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) view.getLayoutParams();
                        //RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                        _xDelta = X - layoutParams.leftMargin;
                        _yDelta = Y - layoutParams.topMargin;
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        break;
                    case MotionEvent.ACTION_POINTER_UP:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        FrameLayout.LayoutParams layoutParams1 = (FrameLayout.LayoutParams) view.getLayoutParams();
                        //RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                          //      .getLayoutParams();

                        layoutParams1.leftMargin = X - _xDelta;
                        layoutParams1.topMargin = Y - _yDelta;
                        layoutParams1.rightMargin = -250;
                        layoutParams1.bottomMargin = -250;
                        view.setLayoutParams(layoutParams1);
                        break;

                }
                root.invalidate();

                break;
        }
        return true;
    }
}

有背景和符号1。 如您所见,我也为背景实现了缩放功能。 但这是另一个我不想讨论的问题。 我的第一个问题是如果我移动所有符号,请移动所有符号。

这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

忽略如何使图像可拖动。

它应该使用RelativeLayout并覆盖可拖动视图的onTouchListener。

只需获取动作事件ACTION_UP事件的值即可获得用户放下图像的X,Y。

然后设置它:

RelativeLayout.LayoutParams prms = view.getLayoutParams();
prms.setMargins(X,Y,0,0);
view.setLayoutParams(prms);

希望这有帮助。