2014-10-16 46 views
0

我一直在四处搜寻,似乎找不到此问题的解决方法。我试图显示一个在画布上空格的枪(作为一个RectF),它使用tick方法自动旋转。我正在保存画布,按照枪角旋转画布,然后绘制矩形,然后在画笔内部恢复画布...但它只是旋转一次。任何人都有如何让它不断旋转的想法?谢谢!在Android画布上旋转RectF


如果有人正在寻找类似的东西,那么在下面的评论中回答了这个问题。

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 to hold star positions 
ArrayList<PointF> stars = new ArrayList<PointF>(); 

//Number of stars 
int numberOfStars = 100; 

//PointFs for center of the sun and angle of gun 
PointF centerOfSun1 = new PointF(300,200); 
float angleOfGun = 1; 


// constructor 
public SpaceAnimator() { 

    //Create a white paint object 
    whitePaint = new Paint(); 
    whitePaint.setColor(Color.WHITE); 

    //Create a yellow paint object 
    yellowPaint = new Paint(); 
    yellowPaint.setColor(Color.YELLOW); 

    //create a blue paint object 
    bluePaint = new Paint(); 
    bluePaint.setColor(Color.BLUE); 

    //Set position of the stars 
    for(int i = 0; i < 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 and Y positions 
    } 

} 

/** 
* Interval between animation frames 
* 
* @return the time interval between frames, in milliseconds. 
*/ 
public int interval() { 
    return FRAME_INTERVAL; 
} 

/** 
* The background color. 
* 
* @return the background color onto which we will draw the image. 
*/ 
public int backgroundColor() { 
    // create/return the background color 
    return BACKGROUND_COLOR; 
} 

/** 
* Action to perform on clock tick 
* 
* @param g the canvas object on which to draw 
*/ 
public void tick(Canvas g) { 

    int height = g.getHeight(); 
    int width = g.getWidth(); 

    //draw the stars 
    for(int i = 0; i < numberOfStars; i++) 
    { 
     g.drawCircle(stars.get(i).x/100 * width, stars.get(i).y/100 * height, randomGen.nextInt(2), whitePaint); 
    } 

    //draw the first sun 
    g.drawCircle(centerOfSun1.x, centerOfSun1.y, 40, yellowPaint); 

    //rotate/draw the 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(); 
} 

回答

0

您发布的代码中没有任何内容正在递增(或递减)angleOfGun。

如果这是真的,那么只有“看到”它第一次旋转1度然后粘在那里才有意义。你需要在你的打勾方法的末尾添加这样一行:

angleOfGun = (angleOfGun + 1) % 360; 
+0

好吧,那帮了一大堆,谢谢。现在,我只需要弄清楚如何让枪围绕一个固定点旋转,因为它现在正在旋转,就好像它正在绕行一样。 – Ryan 2014-10-16 22:24:38

+0

使用旋转(浮点度数,浮点数px,浮点数py)方法围绕您的喷枪矩形的中心点旋转。 – 2014-10-16 22:25:02

+0

感谢一群迈克尔 – Ryan 2014-10-16 22:27:11