Android Dragging and Scaling in FrameLayout
<p>Recently I have been trying to implement dragging and scaling on a picture that I place in a FrameLayout. What I want to achieve is simple: to be able to drag the picture around and zoom it. I went to the Android Developer website and followed the <a href="https://developer.android.com/training/gestures/scale.html#drag" rel="nofollow">guide there</a>.</p>

<p>Then following the code examples on that website I wrote <strong>MyCustomView</strong>:</p>

<pre><code>public class MyCustomView extends ImageView {
private static final int INVALID_POINTER_ID = 0xDEADBEEF;
private ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1.f;
private float mLastTouchX, mLastTouchY;
private int mActivePointerId = INVALID_POINTER_ID;
private LayoutParams mLayoutParams;
private int mPosX, mPosY;

public MyCustomView(Context context) {
    super(context);

    mScaleDetector = new ScaleGestureDetector(context, new CustomScaleListener());
    mLayoutParams = (LayoutParams) super.getLayoutParams();
    if (mLayoutParams != null) {
        mPosX = mLayoutParams.leftMargin;
        mPosY = mLayoutParams.topMargin;
    } else {
        mLayoutParams = new LayoutParams(300, 300);
        mLayoutParams.leftMargin = 0;
        mLayoutParams.topMargin = 0;
    }
}

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.save();
    canvas.scale(mScaleFactor, mScaleFactor);

    canvas.restore();
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    // Let the ScaleGestureDetector inspect all events
    mScaleDetector.onTouchEvent(ev);

    final int action = MotionEventCompat.getActionMasked(ev);

    switch (action) {
        case MotionEvent.ACTION_DOWN: {
            final int pointerIndex = MotionEventCompat.getActionIndex(ev);

            //final float x = MotionEventCompat.getX(ev, pointerIndex);
            //final float y = MotionEventCompat.getY(ev, pointerIndex);
            final float x = ev.getRawX();
            final float y = ev.getRawY();

            // Remember where we started (for dragging)
            mLastTouchX = x;
            mLastTouchY = y;
            // Save the ID of this pointer (for dragging)
            mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
            break;
        }

        case MotionEvent.ACTION_MOVE: {
            // Find the index of the active pointer and fetch its position
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);

            //final float x = MotionEventCompat.getX(ev, pointerIndex);
            //final float y = MotionEventCompat.getY(ev, pointerIndex);
            final float x = ev.getRawX();
            final float y = ev.getRawY();

            final float dx = x - mLastTouchX;
            final float dy = y - mLastTouchY;

            //TODO: Update the location of this view
            mPosX += dx;
            mPosY += dy;

            mLayoutParams.leftMargin += dx;
            mLayoutParams.topMargin += dy;
            super.setLayoutParams(mLayoutParams);

            invalidate();


            mLastTouchX = x;
            mLastTouchY = y;
            break;
        }

        case MotionEvent.ACTION_UP: {
            mActivePointerId = INVALID_POINTER_ID;
            break;
        }

        case MotionEvent.ACTION_CANCEL: {
            mActivePointerId = INVALID_POINTER_ID;
            break;
        }

        case MotionEvent.ACTION_POINTER_UP: {
            final int pointerIndex = MotionEventCompat.getActionIndex(ev);
            final int pointerID = MotionEventCompat.getPointerId(ev, pointerIndex);

            if (pointerID == mActivePointerId) {
                // This was our active pointer going up. Choose a new active pointer and
                // adjust accordingly
                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                //mLastTouchX = MotionEventCompat.getX(ev, newPointerIndex);
                //mLastTouchY = MotionEventCompat.getY(ev, newPointerIndex);
                mLastTouchX = ev.getRawX();
                mLastTouchY = ev.getRawY();
                mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
            }

            break;
        }
    }
    return true;
}




private class CustomScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor *= detector.getScaleFactor();

        mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));

        invalidate();

        return true;
    }
}
</code></pre>

<p>In the <strong>MainActivity</strong> I simply instantiated a <strong>MyCustomView</strong> object and attached it to ViewGroup at the background, which is a FrameLayout. The xml file has nothing but a FrameLayout there.</p>

<pre><code>    public class MainActivity extends AppCompatActivity {
    private ViewGroup layoutRoot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        layoutRoot = (ViewGroup) findViewById(R.id.view_root);

        final MyCustomView ivAndroid = new MyCustomView(this);
        ivAndroid.setImageResource(R.mipmap.ic_launcher);
        ivAndroid.setLayoutParams(new FrameLayout.LayoutParams(300, 300));
        layoutRoot.addView(ivAndroid);
    }
}
</code></pre>

<p>And here comes the problem that troubles me:
    The Android Developer website uses this to obtain the coordinates of the finger that touches the picture:</p>

<pre><code>final float x = MotionEventCompat.getX(ev, pointerIndex);
final float y = MotionEventCompat.getY(ev, pointerIndex);
</code></pre>

<p>But it works horribly! The picture moves, but it does not follow my finger exactly, it always moves LESS than my finger does, and most importantly, it flashes.</p>

<p>So that is why you can see in <strong>MyCustomView</strong> that I have commented out this line and instead used this code:</p>

<pre><code>final float x = ev.getRawX();
final float y = ev.getRawY();
</code></pre>

<p>While this time the picture moves smoothly in accordance with my finger, this change introduces a new problem. On the Android Developer <a href="https://developer.android.com/training/gestures/scale.html" rel="nofollow">website</a> for dragging and scaling, there is a design principle that says:</p>

<blockquote>
  <p>In a drag (or scroll) operation, the app has to keep track of the original pointer (finger), even if additional fingers get placed on the screen. For example, imagine that while dragging the image around, the user places a second finger on the touch screen and lifts the first finger. If your app is just tracking individual pointers, it will regard the second pointer as the default and move the image to that location.</p>
</blockquote>

<p>After I started using <strong>ev.getRawX()</strong> and <strong>ev.getRawY()</strong>, adding a second finger to the screen gives me exactly the problem stated above. But <strong>MotionEventCompat.<em>getX</em>(ev, pointerIndex)</strong> and <strong>MotionEventCompat.<em>getY</em>(ev, pointerIndex)</strong> does not.</p>

<p>Can somebody help me explain why it happens? I know that MotionEventCompat.getX(ev, pointerIndex) returns the coordinate after some sort of adjustment, and that ev.getRawX() returns the absolute coordinate. But I don't understand how exactly the adjustment works (Is there a formula or graphical explanation for it?). I also want to know why using MotionEventCompat.getX(...) would prevent the picture from jumping to the second finger on screen (after the first finger has been lifted).</p>

<p>Last but not least, the scaling code simply doesn't work AT ALL. If someone and teach me on that it will also be greatly appreciated!</p>