ACTION_MOVE上的图像视图旋转

时间:2011-07-28 14:28:44

标签: android

我可以在ACTION_DOWN上旋转图像视图。

但它不适用于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);
    }

}

任何建议都会被挪用

1 个答案:

答案 0 :(得分:7)

super.onTouchEvent(event)方法中返回 true 而不是onTouchEvent。这将通知操作系统您正在处理触摸事件,从而允许它为您提供进一步的触摸事件(例如MotionEvent.ACTION_MOVE)。