【问题标题】:canvas image rotation on with center like dialer像拨号器一样使用中心旋转画布图像
【发布时间】:2013-08-14 14:14:12
【问题描述】:

我有一个具有圆形视图的方形图像,我正在使用以下代码使用画布在圆心绘制该图像:

Bitmap bitmapCompass = BitmapFactory.decodeResource(getResources(), R.drawable.compass_rim);
  float w2= (w-bitmapCompass.getWidth())/2;
  float h2= (h-bitmapCompass.getHeight())/2;

  canvas.drawBitmap(bitmapCompass, w2, h2, paint);

我想像拨号器一样相对于中心旋转此图像。面临的问题是围绕该圆心旋转图像。

用于旋转的代码:

Bitmap bitmapCompass = BitmapFactory.decodeResource(getResources(), R.drawable.compass_back);
  float w2= (w-bitmapCompass.getWidth())/2;
  float h2= (h-bitmapCompass.getHeight())/2;
  Matrix matbitmapCompass = new Matrix();
     matbitmapCompass.postRotate((float)90);
     Bitmap bmpRotateCompass = Bitmap.createBitmap(bitmapCompass, 0,0, bitmapCompass.getWidth(), bitmapCompass.getHeight(), matbitmapCompass, true);
  canvas.drawBitmap(bmpRotateCompass, w2, h2, paint);

我们将不胜感激。

谢谢

【问题讨论】:

    标签: android android-canvas sensors


    【解决方案1】:

    我想你会想要旋转画布:

    canvas.save();
    canvas.rotate(angle, rotationCenterX, rotationCenterY);
    // draw here
    canvas.restore();
    

    【讨论】:

    • 感谢您的回复,请查看此网址xaraxone.com/webxealot/workbook11/page_4.htm 我想使用相同类型的图像想要从传感器 api 传递角度进行旋转。假设 C(h, k) 是圆心,那么如何以定义的角度旋转物体,中心将固定,圆将像拨号器一样移动。
    • 如果我理解正确的话,我想你需要 screenCenterX 和 screenCenterY,它们是你想要绘制的 X 和 Y 屏幕坐标。这些坐标是您要传递给 canvas.rotate 的坐标。然后在绘制位图时,需要在 screenCenterX - bitmapWidth / 2 和 screenCenterY - bitmapHeight / 2 处进行绘制,以确保位图的中心与屏幕上的旋转点对齐。
    • 请检查上面共享的代码,我正在做同样的位图 bitmapCompass = BitmapFactory.decodeResource(getResources(), R.drawable.compass_rim);浮动 w2= (w-bitmapCompass.getWidth())/2;浮动 h2= (h-bitmapCompass.getHeight())/2; canvas.drawBitmap(bitmapCompass, w2, h2, paint);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 2010-09-26
    • 2015-01-06
    • 2016-03-13
    • 2011-12-26
    • 1970-01-01
    相关资源
    最近更新 更多