Я искал вокруг и не могу найти исправления для этого. Я пытаюсь отобразить пистолет (как RectF), который находится в пространстве на холсте, который автоматически вращается с помощью метода тика. Я сохраняю холст, поворачивая его под углом пистолета, затем рисую прямоугольник, а затем восстанавливая холст внутри метода тика ... но он только вращает пистолет один раз. У кого-нибудь есть идея, как заставить его вращаться непрерывно? Благодаря!Вращение RectF на холсте Android
Этот вопрос был отвечен в комментариях ниже, если кто-то ищет что-то подобное.
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
Используйте поворот (float degrees, float px, float py) для поворота вокруг центральной точки вашего пистолета. –
Спасибо, связка Майкл – Ryan