Android problem with Image Rotate and Matrix -


hopefully easy 1 because i've been trying sorts of different ways rid of this.

i making android app incorporates clock animation. i've got working except 1 annoying thing.

i have second hand on clock , i'm using following code rotate around second hand center point. you'll notice i'm trying make analogue second hand sweeps instead of 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 job want... almost.

the problem hand shakes/jiggles around center point ever slightly, it's noticeable , spoils aesthetics.

i pretty suspect it's way it's rounding float value, hoping had experienced before , had ideas on how rid of it.

for reference second hand image 74 px x 28 px , (currently) 74 x 74 pixels .png middle of second hand crossing crossing point. i've tried making 75 x 75 there central pixel no luck.

any @ appreciated.

** update

i've tried change code in case decimals getting dropped still no luck i'm afraid. here option 2 i've tried , 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); 

i understand problem, have never encountered mysleft, sounds pretty obvious me. since rotations changes width , height of image, imprecision comes centrex - negx/2

i have not tested, suggest try:

matrix matrix=new matrix() //first translate place center of hand @ point(0,0) matrix.settranslate(-secondwidth/2,-secondheight/2); matrix.setrotate(secdegrees); //now place pivot point(0,0) @ expected location matrix.settranslate(centrex,centrey); newsecond = bitmap.createbitmap(secondhand, 0, 0, secondwidth, secondheight, matrix, false); c.drawbitmap(newsecond,0,0,null); 

of course, suboptimal, since newsecond bitmap larger needs be. if centrex , centrey big, might want translate less that, , draw translation of difference.

//now place pivot position hand can drawn without imprecion on 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); 

hope helps.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -