本文介绍了在Canvas Android上旋转位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展SurfaceView的类。我有一个通过Bitmap在画布上绘制的针的图像。我想在单击按钮时将针旋转一个固定点。旋转针的逻辑是什么?假设我在单击按钮时调用了onDraw(Canvas canvas),我想每次将针旋转5度。这是我的代码

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

          }

    }


推荐答案

您应致电然后 canvas.drawBitmap(...),然后

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

这篇关于在Canvas Android上旋转位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 15:36