拖动ImageView而不是附加到ImageView的图像

时间:2012-12-04 18:41:06

标签: java android android-fragments

  

可能重复:
  Android Drag/Animation of Views

我正在尝试创建一个包含可拖动ImageView的片段。我创建了一个片段,可以拖动,缩放和旋转ImageView(即图像)的源。但我需要能够触摸图像未覆盖的元素,但这是不可能的,因为我的解决方案仅在ImageView内部移动图像。所以我需要移动整个ImageView

片段XML:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout"
        android:gravity="top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/cardImage"
            android:contentDescription="@string/cardDescription"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="matrix"/>

    </LinearLayout>

片段java:

public class BusinessCardFragment extends Fragment
{   
    private ImageView myImage = null;
    private float oldDist = 1;
    private File _imageFile = null;

    public BusinessCardFragment()
    { }

    public BusinessCardFragment(File image)
    {
        _imageFile = image;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View v = inflater.inflate(R.layout.business_card_fragment, container, false);
        ImageView bcImg = (ImageView)v.findViewById(R.id.cardImage);
        bcImg.setOnTouchListener(onTableTouched);

        Bitmap cardBitmap = BitmapFactory.decodeFile(_imageFile.getAbsolutePath());
        bcImg.setImageBitmap(cardBitmap);

        return v;
    }

    public OnTouchListener onTableTouched = new OnTouchListener()
    {
        Matrix matrix = new Matrix();
        Matrix savedMatrix = new Matrix();

        static final int NONE = 0;
        static final int DRAG = 1;
        static final int ZOOM = 2;
        int mode = NONE;

        int currentXDirection = 0;
        int currentYDirection = 0;
        PointF start = new PointF();
        PointF mid = new PointF();
        PointF lastPoint = new PointF();
        float oldDist = 1f;
        final String TAG = "BusinessCardFragment, OnClick";

        public boolean onTouch(View v, MotionEvent event)
        {           
            final ImageView view = (ImageView) v;
            handleTouchEvents(event, view);

            return true;
        }

        private void handleTouchEvents(MotionEvent event, ImageView view)
        {
            switch (event.getAction() & MotionEvent.ACTION_MASK)
            {
                case MotionEvent.ACTION_DOWN:
                    savedMatrix.set(matrix);
                    start.set(event.getX(), event.getY());
                    Log.d(TAG, "mode=DRAG");
                    mode = DRAG;
                    break;
                case MotionEvent.ACTION_POINTER_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_UP:
                case MotionEvent.ACTION_POINTER_UP:                     
                    mode = NONE;
                    Log.d(TAG, "mode=NONE");
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (mode == DRAG)
                    {
                        Log.i(TAG, "Current X: " + event.getX() + " Start X: " + start.x + " Last X: " + lastPoint.x + " Translate X: " + (event.getX() - start.x));

                        float translateX = (event.getX() - start.x);
                        float translateY = (event.getY() - start.y);

                        currentXDirection = (int) (translateX - lastPoint.x);
                        currentYDirection = (int)(translateY - lastPoint.y);

                        matrix.set(savedMatrix);
                        matrix.postTranslate(translateX, translateY);

                        lastPoint.set(translateX, translateY);
                    }
                    else if (mode == ZOOM)
                    {                       
                        float newDist = spacing(event);
                        Log.d(TAG, "newDist=" + newDist);
                        if (newDist > 10f)
                        {
                            matrix.set(savedMatrix);
                            float scale = newDist / oldDist;
                            matrix.postScale(scale, scale, mid.x, mid.y);
                        }

                        if((oldDist - newDist) < 5 && (newDist - oldDist) > -5)
                        {
                            matrix.postRotate(fingersAngle(event), mid.x, mid.y);
                        }
                    }
                    break;
            }

            view.setImageMatrix(matrix);
        }

        private float spacing(MotionEvent event)
        {
            float x = event.getX(0) - event.getX(1);
            float y = event.getY(0) - event.getY(1);
            return (float)Math.sqrt(x * x + y * y);
        }

        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);
        }

        private float fingersAngle(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);       

            float angle = (float)Math.toDegrees(radians);
            Log.i("FingerAngle", "Angle: " + angle);

            return angle;
        }
    };

}

如何移动ImageView和图像,而不仅仅是图像?

0 个答案:

没有答案