jasonpolites/gesture-imageview

Gamepad scale and move

TheNetStriker opened this issue · 0 comments

I wrote some code to support scaling and moving in an image in gesture-imageview using a gamepad. It works great with XBOX gamepad. Maybe it will also work for the Ouya gamepad, but I have to wait for mine before I can test this. :)

Those two methods belong in GestureImageView.java:

public PointF getTopLeft() {
    PointF topLeft = new PointF();
    int effectiveWidth = Math.round( (float) getImageWidth() * getScale() );
    int effectiveHeight = Math.round( (float) getImageHeight() * getScale() );
    float diff = (float)(effectiveWidth - displayWidth) / 2.0f;
    topLeft.x = centerX - diff;
    float diff2 = (float)(effectiveHeight - displayHeight) / 2.0f;
    topLeft.y = centerY - diff2;
    return topLeft;
}

public void AxisMoveAndScale(float scaleAxisValue, float xAxisValue, float yAxisValue) {
     float scale = startingScale + (scaleAxisValue * (15 - startingScale));          
     setScale(scale);

     PointF topLeft = getTopLeft();

     float x = centerX + (topLeft.x - centerX) * xAxisValue;
     float y = centerY + (topLeft.y - centerY) * yAxisValue;

     setPosition(x, y);
     redraw();
}

After that call the method from any activity like this:

public boolean onGenericMotionEvent(MotionEvent event) {
    if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
         if (event.getAction() == MotionEvent.ACTION_MOVE) {
             float scaleAxisValue = event.getAxisValue(MotionEvent.AXIS_LTRIGGER);
             float xAxisValue = event.getAxisValue(MotionEvent.AXIS_Z);
             float yAxisValue = event.getAxisValue(MotionEvent.AXIS_RZ);
             currentGestureImageView.AxisMoveAndScale(scaleAxisValue, xAxisValue, yAxisValue);
             return true;
         }
     }
    return super.onGenericMotionEvent(event);
}