代码之家  ›  专栏  ›  技术社区  ›  fredley

在Android画布上旋转位图

  •  7
  • fredley  · 技术社区  · 14 年前

    我有一些画在画布上的物体作为SurfaceView的一部分。我希望能够以编程方式旋转这些。 myParticle.setRotation(90);

    public class Particle {
    
      public void draw(Canvas canvas){
        image.setBounds((int)(xPos), (int)(yPos), (int)(xPos+radius), (int)(yPos+radius));
        image.draw(canvas);
      }
    
    }
    
    2 回复  |  直到 14 年前
        1
  •  5
  •   Lavekush Agrawal rekire    10 年前

    你只要打个电话就行了

    canvas.rotate(90) :) // 90 is degree.

        2
  •  55
  •   Bondax codejitsu    9 年前

    在我看来,这样做更干净:

    Matrix rotator = new Matrix();
    
    // rotate around (0,0)
    rotator.postRotate(90);
    
    // or, rotate around x,y
    // NOTE: coords in bitmap-space!
    int xRotate = ...
    int yRotate = ...
    rotator.postRotate(90, xRotate, yRotate);
    
    // to set the position in canvas where the bitmap should be drawn to;
    // NOTE: coords in canvas-space!
    int xTranslate = ...
    int yTranslate = ...
    rotator.postTranslate(xTranslate, yTranslate);
    
    canvas.drawBitmap(bitmap, rotator, paint);
    

    编辑 :Eddie想知道旋转发生在哪个点。

    编辑 :AndrewOrobator想知道如何设置画布目标坐标