簡體   English   中英

根據用戶觸摸沿其中心旋轉 canvas - Android

[英]Rotate canvas along its center based on user touch - Android

我想根據用戶觸摸在其中心軸上循環旋轉 canvas。

就像一個舊的電話撥號器。

我想基於中心旋轉

現在它基於左上角旋轉。所以我只能看到圖像旋轉的 1/4。

我試過如下

onDraw(Canvas canvas){
  canvas.save();
  // do my rotation
  canvas.rotate(rotation,0,0);
  canvas.drawBitmap( ((BitmapDrawable)d).getBitmap(),0,0,p );
  canvas.restore();
}

@Override
        public boolean onTouchEvent(MotionEvent e) {
                  float x = e.getX();
              float y = e.getY();
              updateRotation(x,y);
              mPreviousX = x;
              mPreviousY = y;
            invalidate();
        }

  private void updateRotation(float x, float y) {

          double r = Math.atan2(x - centerX, centerY - y);
            rotation = (int) Math.toDegrees(r);
        }

您可以使用 x=0; 而不是旋轉 Canvas;

x=x+50;
yourview.setRotation(x);

在觸摸事件和 x=x-50 上使用它來旋轉 backword

我認為這會有所幫助

您需要將此添加到您的自定義視圖方法中

@Override     
public void onSizeChanged (int w, int h, int oldw, int oldh){ 
  super.onSizeChanged(w, h, oldw, oldh);         

  screenW = w;         
  screenH = h; 
}

此方法將為您提供 canvas 大小,然后使用

canvas.rotate(旋轉,屏幕寬/2,屏幕高/2);

通過旋轉點旋轉 api:

canvas.rotate(rotation, 0, centerY);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM