Android问题与图像旋转和矩阵 [英] Android problem with Image Rotate and Matrix

查看:131
本文介绍了Android问题与图像旋转和矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这是一个简单的方法,因为我一直在尝试各种不同的方法来消除这种情况.

Hopefully this is an easy one because I've been trying all sorts of different ways to get rid of this.

我正在制作一个包含时钟动画的android应用.除了一件非常烦人的事情,我一切都运转得很好.

I am making an android app which incorporates a clock animation. I've got everything working really well except one very annoying thing.

我的时钟是秒针,我正在使用以下代码将其绕秒针中心点旋转.正如您可能会注意到的那样,我正在尝试使它看起来像是模拟的秒针,因此它可以扫描而不仅仅是打勾.

I have a second hand on the clock and I'm using the following code to rotate it around a the second hand center point. As you'll probably notice I'm trying to make this look like an analogue second hand so it sweeps instead of just ticking.

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

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

It actually does just the job I want... almost.

问题在于手围绕中心点的晃动/摇动幅度如此之小,但这确实很引人注目,并且确实破坏了美观.

The problem is the hand shakes/jiggles around the center point ever so slightly, but it's really noticeable and really spoils the aesthetics.

我非常怀疑这是舍入浮点值的方式,但是我希望有人曾经经历过这一点,并且对如何摆脱它有任何想法.

I pretty much suspect that it's the way that it's rounding the float value, but I was hoping that someone had experienced this before and had any ideas on how to get rid of it.

作为参考,秒针图像最初为74 px x 28 px,并且(当前)为74 x 74像素.png,秒针的中间正好与交叉点相交.我还尝试将其设置为75 x 75,以便实际上也有一个中心像素,但没有运气.

For reference the second hand image was originally 74 px x 28 px and is (currently) 74 x 74 pixels .png with the middle of the second hand exactly crossing the crossing point. I've also tried making it 75 x 75 so that there is actually a central pixel too but no luck.

任何帮助都将不胜感激.

Any help at all would be appreciated.

**更新

我试图更改代码,以防万一小数点掉了,但恐怕还是没有运气.这是我尝试过并失败的选项2;

I've tried to change the code in case the decimals were getting dropped but still no luck I'm afraid. Here is option 2 I've tried and failed with;

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

推荐答案

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

I understand your problem, I have never encountered it mysleft, but it sounds pretty obvious to me. Since the rotations changes the width and height of the image, your imprecision comes from centrex - NegX/2

我尚未测试,但我建议您尝试:

I have not tested, but I suggest you try:

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和centery很大,则可能要翻译的内容要少一些,然后再翻译出差异.

Of course, this is suboptimal, since the newSecond bitmap is much larger than it actually needs to be. So if your centrex and centrey are big, you might want to translate less than that, and then draw with a translation of the difference.

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

希望这会有所帮助.

这篇关于Android问题与图像旋转和矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆