It is necessary to simulate the movement of the planets in the solar system. Planets painted outline. You need to ask them to move. So far there are 3 planets. I want to ask them to move Timer'om. I can not figure out how to make the rotation of the planet in a circle and do not understand exactly where to call the schedule. in programming novice

public class SolarSystem extends View { public SolarSystem(Context context) { super(context); } Paint sun = new Paint(); Paint mercury = new Paint(); Paint venus = new Paint(); Paint ears = new Paint(); Paint circles = new Paint(); boolean stars = true; int X_Mercury = 700; int Y_Mercury = 750; int R_Mercury = 30; int A_Mercury = 1; @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.BLACK); if (stars) { for (int i = 0; i < 250; i++) { circles.setColor(Color.rgb(250, 250, 250)); canvas.drawCircle((float) (Math.random()*1080), (float) (Math.random()*1920), 1, circles); } for (int i = 0; i < 40; i++) { circles.setColor(Color.rgb(250, 250, 250)); canvas.drawCircle((float) (Math.random()*1080), (float) (Math.random()*1920), 3, circles); } for (int i = 0; i < 20; i++) { circles.setColor(Color.rgb(250, 250, 250)); canvas.drawCircle((float) (Math.random()*1080), (float) (Math.random()*1920), 4, circles); } stars = false; } circles.setColor(Color.argb(50, 250, 250, 250)); circles.setStyle(Paint.Style.STROKE); canvas.drawCircle(550, 800, 160, circles); canvas.drawCircle(550, 800, 275, circles); canvas.drawCircle(550, 800, 425, circles); sun.setColor(Color.YELLOW); canvas.drawCircle(550, 800, 100, sun); mercury.setColor(Color.rgb(184, 134, 11)); canvas.drawCircle(X_Mercury, Y_Mercury, R_Mercury, mercury); venus.setColor(Color.rgb(160, 82, 45)); canvas.drawCircle(650, 550, 45, venus); ears.setColor(Color.rgb(0, 0, 128)); canvas.drawCircle(250, 500, 60, ears); start(); } public void movingMercury(){ X_Mercury = (int) (700 + 30 * Math.cos(A_Mercury++)); //cos (t*a) Y_Mercury = (int) (750 + 30 * Math.sin(A_Mercury++)); } Timer tMercury = new Timer(); TimerTask ttMercury = new TimerTask() { @Override public void run() { movingMercury(); invalidate(); } }; public void start() { tMercury.schedule(ttMercury, 100); } } 

    1 answer 1

    My apologies, the previous answer was completely "the wrong steppe." Try replacing invalidate with postInvalidate (). Perhaps there is no drawing because the timer process is not "UIThread"

    UPDATE Well, since I’ve messed up in the first answer, I decided to collect your code. Everything is beautiful, the tips are as follows:

    1. Of course, in this case, only postInvalidate
    2. Remove start () from onDraw, it’s bad practice to start threads from overloaded methods without knowing where and when these methods will be called.
    3. Well, of course, in order not to constantly start, use scheduleAtFixedRate
    4. Start call once in aktiviti

        SolarSystem ss = new SolarSystem(this); setContentView(ss); ss.start(); 
    5. Do not forget that Math.cos () takes an argument in radians , it looks like degrees to you.

    ZY When drawing according to your code, the asterisks disappear, or remove the condition, or you need to think about how to save the primary random image for the subsequent drawing of the background ...

    • I did not quite catch your thought. How to make a class field from Viwe - anton.rynkovoy
    • The field of your class (inherited from View) should be canvas. - FatalException
    • yeah, got it. and how then to make so that the timer was called? where it will need to call? - anton.rynkovoy
    • Thank you, now everything is clear. Already done and works. - anton.rynkovoy
    • instead tell me, is it possible to add other views in the onDrow method, for example, pictures, buttons? - anton.rynkovoy