0xOsiris
0xOsiris

Reputation: 277

Rotating an image with onTouchListener without scaling it

I have got this simple code to rotate an image with onTouchListener, but it seems to be zooming in and out on the image as well. I am really new to using onTouchListener. Could someone tell me what is happening, or how to fix this. Anything helps thanks:)

public class MainActivity extends Activity implements OnTouchListener {

private ImageView dialer;
private float y=0;
public boolean onTouch(View v, MotionEvent event) {
    double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY());
    int rotation=(int)Math.toDegrees(r);
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            break;
        case MotionEvent.ACTION_MOVE:
            //x=event.getX();
            y=event.getY();
            updateRotation(rotation);
            break;
        case MotionEvent.ACTION_UP:
            break;
    }//switch

    return true;

}//onTouch
private void updateRotation(double rot){
    float newRot= new Float(rot);
    Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.round_button_big);
    Matrix matrix= new Matrix();
    matrix.postRotate(newRot,bitmap.getWidth()/2,bitmap.getHeight()/2);
    if(y>250){
        Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
        dialer.setImageBitmap(reDrawnBitmap);
    }
    else{
        Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
        dialer.setImageBitmap(reDrawnBitmap);
    }
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dialer = (ImageView) findViewById(R.id.image);
    dialer.setOnTouchListener(this);
}//onCreate

}

Upvotes: 0

Views: 473

Answers (1)

Corina Gheorghe
Corina Gheorghe

Reputation: 198

Try to use this code on touch event:

switch (event.getAction()) {    
    case MotionEvent.ACTION_DOWN:
        // reset the touched quadrants
        for (int i = 0; i < quadrantTouched.length; i++) {
            quadrantTouched[i] = false;
        }
        allowRotating = false;
        startAngle = getAngle(event.getX(), event.getY());
        break;
    case MotionEvent.ACTION_MOVE:
        double currentAngle = getAngle(event.getX(), event.getY());
        rotateDialer((float) (startAngle - currentAngle));
        startAngle = currentAngle;
        break;
    case MotionEvent.ACTION_UP:
        allowRotating = true;
        break;
}

// set the touched quadrant to true
quadrantTouched[getQuadrant(event.getX() - (dialerWidth / 2), dialerHeight - event.getY() - (dialerHeight / 2))] = true;
detector.onTouchEvent(event);
return true;

And here you can find an example :http://code.tutsplus.com/tutorials/android-sdk-creating-a-rotating-dialer--mobile-8868

Upvotes: 1

Related Questions