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();
}
好吧,那幫了一大堆,謝謝。現在,我只需要弄清楚如何讓槍圍繞一個固定點旋轉,因爲它現在正在旋轉,就好像它正在繞行一樣。 – Ryan 2014-10-16 22:24:38
使用旋轉(浮點度數,浮點數px,浮點數py)方法圍繞您的噴槍矩形的中心點旋轉。 – 2014-10-16 22:25:02
感謝一羣邁克爾 – Ryan 2014-10-16 22:27:11