Monday 15 September 2014

Rotating a RectF on an Android canvas -



Rotating a RectF on an Android canvas -

i've been searching around , can't seem find prepare this. i'm trying display gun (as rectf) that's in space on canvas automatically rotating tick method. i'm saving canvas, rotating gun angle, drawing rectangle, , restoring canvas within of tick method... rotates gun 1 time. have thought of how create rotate continuously? thanks!

this question answered in comments below if searching similar.

public class spaceanimator implements animator { // constants private static final int frame_interval = 60; // animation-frame interval, in milliseconds private static final int background_color = color.black; // background color // paint objects private paint whitepaint; private paint yellowpaint; private paint bluepaint; //random number generator random randomgen = new random(); //pointf array hold star positions arraylist<pointf> stars = new arraylist<pointf>(); //number of stars int numberofstars = 100; //pointfs center of sun , angle of gun pointf centerofsun1 = new pointf(300,200); float angleofgun = 1; // constructor public spaceanimator() { //create white paint object whitepaint = new paint(); whitepaint.setcolor(color.white); //create yellowish paint object yellowpaint = new paint(); yellowpaint.setcolor(color.yellow); //create bluish paint object bluepaint = new paint(); bluepaint.setcolor(color.blue); //set position of stars for(int = 0; < numberofstars; i++) { int randstarx = randomgen.nextint(100); //random x initial position int randstary = randomgen.nextint(100); //random y initial position stars.add(new pointf(randstarx, randstary)); //set x , y positions } } /** * interval between animation frames * * @return time interval between frames, in milliseconds. */ public int interval() { homecoming frame_interval; } /** * background color. * * @return background color onto draw image. */ public int backgroundcolor() { // create/return background color homecoming background_color; } /** * action perform on clock tick * * @param g canvas object on draw */ public void tick(canvas g) { int height = g.getheight(); int width = g.getwidth(); //draw stars for(int = 0; < numberofstars; i++) { g.drawcircle(stars.get(i).x/100 * width, stars.get(i).y/100 * height, randomgen.nextint(2), whitepaint); } //draw first sun g.drawcircle(centerofsun1.x, centerofsun1.y, 40, yellowpaint); //rotate/draw gun g.save(); g.rotate(angleofgun); g.drawrect(new rectf(width/2 - 20, height/2 - 20, width/2 + 20, height/2 + 20), bluepaint); g.restore(); }

nothing in code you've posted incrementing (or decrementing) angleofgun.

if that's true, makes sense you'll "see" rotate first time 1 grade , stick there. you'll need add together line end of tick method:

angleofgun = (angleofgun + 1) % 360;

android canvas

No comments:

Post a Comment