1

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

我正在制作一个包含时钟动画的 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);
4

1 回答 1

-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);

希望这可以帮助。

于 2010-12-06T13:42:59.637 回答