Sharana
Sharana

Reputation: 35

Image view rotation on ACTION_MOVE

I'm able to rotate image view on ACTION_DOWN.

but its not working for ACTION_MOVE & ACTION_UP.

public class RotateImage extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Rotate rotate;
        rotate = new Rotate(this);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
            requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(rotate);
    }
}

public class Rotate extends ImageView {
    Paint paint;
    int direction = 0;
    private float x;
    private float y;
    int degree = 0;
    float a, b, c;
    private float centerX ;
    private float centerY ;
    private float newX ;
    private float newY ;

    public Rotate(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(2);
        paint.setStyle(Style.STROKE);

        this.setImageResource(R.drawable.direction1);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        int height = this.getHeight();
        int width = this.getWidth();
        centerX = width/2;
        centerY = height/2;
        canvas.rotate(direction, width / 2, height / 2);
        super.onDraw(canvas);

    }

    public void setDirection(int direction) {
        this.direction = direction;
        this.invalidate();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        System.out.println("now i'm in BEFORE calling MotionEvent.ACTION_MOVE ");

        if (event.getAction() == MotionEvent.ACTION_DOWN) {

            x = event.getX();
            y = event.getY();

            newX =  centerX-x;
            newY = centerY-y;

            updateRotation( newX,  newY); 

            }
            else if (event.getAction() == MotionEvent.ACTION_MOVE) {

                x = event.getX();
                y = event.getY();

                newX =  centerX-x;
                newY = centerY-y;

                updateRotation( newX,  newY); 

                }
            else if (event.getAction() == MotionEvent.ACTION_UP) {

                    x = event.getX();
                    y = event.getY();

                    newX =  centerX-x;
                    newY = centerY-y;

                    updateRotation( newX,  newY); 

                    }
        return super.onTouchEvent(event);
    }

    private void updateRotation(float newX2, float newY2) {
        // TODO Auto-generated method stub

        degree = (int)Math.toDegrees(Math.atan2(newY, newX))-90;

        setDirection(degree);
    }

}

any suggestion would be appropriated

Upvotes: 2

Views: 2202

Answers (1)

Che Jami
Che Jami

Reputation: 5151

Return true instead of super.onTouchEvent(event) in your onTouchEvent method. This will inform the OS that you are handling the touch event allowing it to provide you with further touch events (e.g. MotionEvent.ACTION_MOVE).

Upvotes: 7

Related Questions