1

可能的重复:
Android 拖动/视图动画

我正在尝试制作一个包含可拖动的片段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和图像而不只是图像?

4

0 回答 0