拖动时,图像视图隐藏在布局角落中

时间:2014-10-07 05:55:04

标签: android hide drag

实际上,当我试图将图像移动到另一个地方时,它移动得相当好,但每当图像进入布​​局角落时它就会隐藏。我想在角落限制ImageView而不是隐藏。

请帮忙

代码就在这里:

           image1.setOnTouchListener(new View.OnTouchListener() {

           @Override
          public boolean onTouch(View v, MotionEvent event) {
            ImageView view = (ImageView) v;
            image1.bringToFront();
            view.setScaleType(ImageView.ScaleType.MATRIX);

            // Dump touch event to log
            dumpEvent(event);

            // Handle touch events here...
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                savedMatrix.set(matrix);
                start.set(event.getX(), event.getY());
                mode = DRAG;
                break;

            case MotionEvent.ACTION_POINTER_DOWN:
                oldDist = spacing(event);
                if (oldDist > 10f) {
                    savedMatrix.set(matrix);
                    midPoint(mid, event);
                    mode = ZOOM;
                }

                lastEvent = new float[4];
                lastEvent[0] = event.getX(0);
                lastEvent[1] = event.getX(1);
                lastEvent[2] = event.getY(0);
                lastEvent[3] = event.getY(1);
                d = rotation(event);

                break;

            case MotionEvent.ACTION_UP:

            case MotionEvent.ACTION_POINTER_UP:
                mode = NONE;
                break;

            case MotionEvent.ACTION_MOVE:
                if (mode == DRAG) {
                    // ...
                    matrix.set(savedMatrix);
                    matrix.postTranslate(event.getX() - start.x,
                            event.getY() - start.y);
                } else if (mode == ZOOM && event.getPointerCount() == 2) {
                    float newDist = spacing(event);
                    Log.v("SS", "Count=" + event.getPointerCount());
                    Log.v("SS", "newDist=" + newDist);
                    matrix.set(savedMatrix);
                    if (newDist > 10f) {
                        float scale = newDist / oldDist;
                        System.out.println(newDist + " : " + oldDist
                                + " : " + scale + " : " + mid.x + " : "
                                + mid.y);
                        System.out.println("### Scale Value :: " + scale);
                        matrix.postScale(scale, scale, mid.x, mid.y);

                    }
                    if (lastEvent != null) {
                        newRot = rotation(event);
                        Log.v("Degreeeeeeeeeee", "!!!!!!!!! newRot="
                                + (newRot));
                        degree = Math.abs(newRot);
                        float r = newRot - d;
                        Log.v("Degreeeeeeeeeee rrrrr ",
                                "!!!!!!!!!!! rotate :: " + r);
                        matrix.postRotate(r, v.getMeasuredWidth() / 2,
                                v.getMeasuredHeight() / 2);
                    }
                }
                break;
            }

            view.setImageMatrix(matrix);
            return true; // indicate event was handled
        }

        /** Show an event in the LogCat view, for debugging */
        private void dumpEvent(MotionEvent event) {
            String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE",
                    "POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
            StringBuilder sb = new StringBuilder();
            int action = event.getAction();
            int actionCode = action & MotionEvent.ACTION_MASK;
            sb.append("event ACTION_").append(names[actionCode]);
            if (actionCode == MotionEvent.ACTION_POINTER_DOWN
                    || actionCode == MotionEvent.ACTION_POINTER_UP) {
                sb.append("(pid ").append(
                        action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
                sb.append(")");
            }
            sb.append("[");
            for (int i = 0; i < event.getPointerCount(); i++) {
                sb.append("#").append(i);
                sb.append("(pid ").append(event.getPointerId(i));
                sb.append(")=").append((int) event.getX(i));
                sb.append(",").append((int) event.getY(i));
                if (i + 1 < event.getPointerCount())
                    sb.append(";");
            }
            sb.append("]");
        }

        /** Determine the space between the first two fingers */
        private float spacing(MotionEvent event) {
            float x = event.getX(0) - event.getX(1);
            float y = event.getY(0) - event.getY(1);
            return FloatMath.sqrt(x * x + y * y);
        }

        /** Calculate the mid point of the first two fingers */
        private void midPoint(PointF point, MotionEvent event) {
            float x = event.getX(0) + event.getX(1);
            float y = event.getY(0) + event.getY(1);
            point.set(x / 2, y / 2);
        }

        /** Determine the degree between the first two fingers */
        private float rotation(MotionEvent event) {
            double delta_x = (event.getX(0) - event.getX(1));

            double delta_y = (event.getY(0) - event.getY(1));
            double radians = Math.atan2(delta_y, delta_x);
            // if (Constant.TRACE) Log.d("Rotation ~~~~~~~~~~~~~~~~~",
            // delta_x+" ## "+delta_y+" ## "+radians+" ## "+Math.toDegrees(radians));
            return (float) Math.toDegrees(radians);
        }
    });

Xml代码:

         <?xml version="1.0" encoding="utf-8"?>
         <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
               android:id="@+id/r"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:background="@drawable/repeatt"
               android:orientation="vertical" >

       <FrameLayout
              android:id="@+id/f"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginTop="25dp" >

        <ImageView
              android:id="@+id/imageView2"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:scaleType="matrix"
              android:src="@drawable/specs" />

       </FrameLayout> 
      </RelativeLayout>

0 个答案:

没有答案