希望这很简单,因为我一直在尝试各种不同的方法来摆脱这种情况。
我正在制作一个包含时钟动画的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像素×28像素,并且(当前)是74×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);
答案 0 :(得分:-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位图比实际需要的要大得多。因此,如果你的centrex和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);
希望这有帮助。