【问题标题】:Android problem with Image Rotate and Matrix图像旋转和矩阵的Android问题
【发布时间】:2010-11-15 23:03:55
【问题描述】:

希望这很容易,因为我一直在尝试各种不同的方法来摆脱它。

我正在制作一个包含时钟动画的 android 应用程序。除了一件非常烦人的事情之外,我的一切都运行良好。

我的时钟上有秒针,我正在使用以下代码围绕秒针中心点旋转它。您可能会注意到,我试图让它看起来像一个模拟秒针,以便它扫动而不只是滴答作响。

        public float secdegrees, secondwidth, secondheight;

        secondMatrix = new Matrix();
        secondwidth = secondHand.getWidth();
        secondheight = secondHand.getHeight();
        secdegrees = anglepersec * secnow;
        secdegrees += anglepluspermilli * millis;
        secondMatrix.setRotate(secdegrees, secondwidth/2, secondheight / 2);
        newSecond = Bitmap.createBitmap(secondHand, 0, 0,
               (int) secondwidth, (int) secondheight, secondMatrix, true);
        c.drawBitmap(newSecond, (centrex - newSecond.getWidth()/2),
               ((face.getHeight()/2) - newSecond.getHeight()/2), null);

它实际上只是完成了我想要的工作......几乎。

问题是手在中心点附近晃动/晃动非常轻微,但它确实很明显并且确实破坏了美感。

我几乎怀疑它是四舍五入浮点值的方式,但我希望有人以前经历过这种情况并且对如何摆脱它有任何想法。

作为参考,二手图像最初为 74 像素 x 28 像素,并且(当前)为 74 x 74 像素 .png,秒针的中间正好穿过交叉点。我也尝试将其设为 75 x 75,这样实际上也有一个中心像素,但没有运气。

任何帮助都将不胜感激。

** 更新

我尝试更改代码以防小数被丢弃,但恐怕仍然没有运气。这是我尝试过但失败的选项 2;

        secondMatrix = new Matrix();
        secondwidth = secondHand.getWidth();
        secondheight = secondHand.getHeight();
        secdegrees = anglepersec * secnow;
        secdegrees += anglepluspermilli * millis;
        secondMatrix.setRotate(secdegrees, secondwidth/2, secondheight / 2);
        newSecond = Bitmap.createBitmap(secondHand, 0, 0, (int) secondwidth,
              (int) secondheight, secondMatrix, true);
        float secW = newSecond.getWidth()/2;
        float secH = newSecond.getHeight()/2;

        // NEW CODE HERE
        float calcdeg = secdegrees % 90;
        calcdeg = (float) Math.toRadians(calcdeg);
        float NegY = (float) ((secondwidth*Math.cos(calcdeg)) +
             (secondwidth * Math.sin(calcdeg)));
        c.drawBitmap(newSecond, centrex - NegY/2,
              ((face.getHeight()/2) - NegY/2), null);

【问题讨论】:

  • secondwidth 和 secondheight 是浮点数还是整数?
  • 他们是花车。我已经更新了上面的代码。
  • 我认为这个问题与stackoverflow.com/q/3603646/94363重复

标签: android matrix rotation clock shake


【解决方案1】:

我了解您的问题,我从来没有遇到过它,但这对我来说听起来很明显。由于旋转会改变图像的宽度和高度,因此您的不精确来自centrex - NegX/2

我没有测试过,但我建议你试试:

Matrix matrix=new Matrix()
//first translate to place the center of the hand at Point(0,0)
matrix.setTranslate(-secondWidth/2,-secondHeight/2);
matrix.setRotate(secdegrees);
//now place the pivot Point(0,0) at its expected location
matrix.setTranslate(centreX,centreY);
newSecond = Bitmap.createBitmap(secondHand, 0, 0, secondWidth, secondHeight, matrix, false);
c.drawBitmap(newSecond,0,0,null);

当然,这是次优的,因为 newSecond 位图比它实际需要的大得多。因此,如果您的 centerx 和 centrey 很大,您可能希望翻译得更少,然后绘制差异的翻译。

//now place the pivot to a position where the hand can be fully drawn without imprecion on the future location of Point(0,0)
matrix.setTranslate(secondWith,secondHeight);
newSecond = Bitmap.createBitmap(secondHand, 0, 0, secondWidth, secondHeight, matrix, false);
c.drawBitmap(newSecond,centrex-secondWidth,centrey-secondHeight,null);

希望这会有所帮助。

【讨论】:

  • 感谢您的详细反馈。我会尝试这个解决方案并回复你。
猜你喜欢
  • 2011-08-31
  • 2022-09-30
  • 2016-06-23
  • 1970-01-01
  • 1970-01-01
  • 2011-01-28
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
相关资源
最近更新 更多