【问题标题】:Rotating a RectF on an Android canvas在 Android 画布上旋转 RectF
【发布时间】:2014-10-16 22:04:11
【问题描述】:

我一直在四处寻找,似乎无法找到解决此问题的方法。我正在尝试在画布上的空间中显示一把枪(作为 RectF),它使用 tick 方法自动旋转。我正在保存画布,将其旋转枪角,然后绘制矩形,然后在 tick 方法中恢复画布......但它只旋转枪一次。有人知道如何让它连续旋转吗?谢谢!


如果有人在寻找类似的东西,这个问题在下面的 cmets 中得到了回答。

    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();
}

【问题讨论】:

    标签: android canvas


    【解决方案1】:

    您发布的代码中的任何内容都没有增加(或减少)angleOfGun。

    如果这是真的,那么您只会“看到”它第一次旋转 1 度然后停留在那里是有道理的。您需要在 tick 方法的末尾添加这样的一行:

    angleOfGun = (angleOfGun + 1) % 360;
    

    【讨论】:

    • 好的,帮了很多忙,谢谢。现在,我只需要弄清楚如何让枪围绕一个固定点旋转,因为它现在正在绕着一个圆圈旋转,就好像它在绕着什么轨道运行一样。
    • 使用 rotate(float degree, float px, float py) 方法围绕你的枪的中心点进行旋转。
    • 非常感谢迈克尔
    • 嘿迈克尔,我正在尝试做一些类似于旋转枪的事情。我试图从枪中射出一些球,以枪的角度,但让它们直射。每当它们射出时,它们就会随着枪口不断旋转,就好像它们在轨道上一样。有什么建议么?我试过不恢复画布,这样我就可以在旋转后沿直线拍摄它们,但它没有融合在一起。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 2016-11-21
    相关资源
    最近更新 更多