Rotate Bitmap on Canvas Android

爱⌒轻易说出口 提交于 2019-12-25 11:12:36

问题


I have a class extending SurfaceView. I have an image of a needle which i have drawn on canvas through Bitmap. I want to rotate this needle with one fixed point on click of a button. What is the logic for rotating needle ? Suppose i have called onDraw(Canvas canvas) on click of a button and i want to rotate needle by 5 degrees every time. this is my code

    public class PingPongSurfaceView extends SurfaceView implements SurfaceHolder.Callback
    {
      private Bitmap needle = null;

      public PingPongSurfaceView(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
            setBackgroundResource(R.drawable.puzzle);

        needle = BitmapFactory.decodeResource(getResources(), R.drawable.sui);


        }

protected void onDraw(Canvas canvas) 
          {
        int needlex =0;
        int needley = 0;

            needlex = (mWidth/2) - needle.getWidth()+9;
    needley = (mHeight - (needle.getHeight()/2)-70);

            canvas.drawBitmap(needle, needlex, needley, null); //Bitmap to be rotate

          }

    }

回答1:


You should call canvas.save() then canvas.rotate(5, centerX, centerY) canvas.drawBitmap(...) and then canvas.restore()



来源:https://stackoverflow.com/questions/17137568/rotate-bitmap-on-canvas-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!