android - ACTION_MOVE 上的 ImageView 旋转

标签 android

我可以在 ACTION_DOWN 上旋转 ImageView 。

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

}

任何建议都会被采纳

最佳答案

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

关于android - ACTION_MOVE 上的 ImageView 旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6860630/

相关文章:

android - 安卓截图

android - android studio 中带有 firebase 的 Google 登录按钮,错误 : cannot find symbol variable default_web_client_id

java - 使屏幕按钮可滚动

android - 使用百分比填充的 Google Maps API v2 newLatLngBounds 在多窗口模式下抛出错误

android - Android Studio不支持的gradle版本,但我使用的是兼容版本

android - 如何将图片保存到文件?

android - 将 fragment 添加到对话框 fragment

android - 在android中通信服务器网络的最佳方式和灵活方式?

android - ImageButton onClick 改变颜色

android - Android 上的 GdxRuntimeException : couldn't load shared library 'gdx' for target