简体   繁体   中英

Rotate one circle around the other in android

I need to make an Canvas animation in Android studio (by using Java), where I need to present one circle in center of the screen and I need to have a few circles that are circling around that main center circle (similar as to planets are circling around the Sun). So, my problem is how to make an animation that will do this - make one circle go around the other center-fixed circle? With the below code I am trying to determine the logic for one circle and I would use that same/similar logic to animate the other circles as well. Please, share some info if you know how can I resolve this. Thank you.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    screenDimensions = getDisplayDimensions();
    mainCircleX = screenDimensions[0] / 2;
    mainCircleY = screenDimensions[1] / 2;
    mainRadius = screenDimensions[0] - (screenDimensions[0] * 0.9f);

    circle1X = mainCircleX + mainRadius + (screenDimensions[0] - screenDimensions[0] * 0.95f);
    circle1Y = mainCircleY;

bitmap = Bitmap.createBitmap(screenDimensions[0], screenDimensions[1], Bitmap.Config.ARGB_8888);
        canvas = new Canvas(bitmap);

        paint = new Paint();

        imageView = findViewById(R.id.imageView);
        imageView.setImageBitmap(bitmap);


        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        renderFrame();
                    }
                });

            }
        }, 200, 400);


    }

private void renderFrame() {

        paint.setColor(Color.YELLOW);
        canvas.drawColor(Color.parseColor("#8D99A0"));
        canvas.drawCircle(mainCircleX, mainCircleY, mainRadius, paint);

        paint.reset();

        paint.setColor(Color.parseColor("#CC4125"));
        paint.setStrokeWidth(10f);
        paint.setStyle(Paint.Style.STROKE);

        canvas.drawCircle(circle1X, circle1Y, 40, paint);
/*Need logic here that will make the circle with circle1X and circle1Y center coordinates, go around the main circle*/

imageView.invalidate();
}

I have tried to rotate canvas (canvas.rotate()), but that only solves the issue with one circle. Also, other circles need to go in different directions and different speeds, so rotating canvas will probably not do the trick. I am fairly new at this, so any help would be appreciated. Thanks.

I have found an acceptable solution. The option is to use canvas.rotate(), but to have separate Canvas and Paint object for every circle that I need to rotate around the main circle. So, here is the code that worked for me:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        screenDimensions = getDisplayDimensions();
        mainCircleX = screenDimensions[0] / 2;
        mainCircleY = screenDimensions[1] / 2;
        mainRadius = screenDimensions[0] - (screenDimensions[0] * 0.9f);


        circle1X = mainCircleX + mainRadius + (screenDimensions[0] - screenDimensions[0] * 0.95f);
        circle1Y = mainCircleY;
        circle2X = (mainCircleX + mainRadius + 100f);
        circle2Y = screenDimensions[1] - 1060f;
        circle3X = screenDimensions[0] - (screenDimensions[0] * 0.85f);
        circle3Y = mainCircleY;
        circle4X = screenDimensions[0] - (screenDimensions[0] * 0.85f);
        circle4Y = screenDimensions[1] - 570;

        bitmap = Bitmap.createBitmap(screenDimensions[0], screenDimensions[1], Bitmap.Config.ARGB_8888);
        canvasCentral = new Canvas(bitmap);
        canvas1 = new Canvas(bitmap);
        canvas2 = new Canvas(bitmap);
        canvas3 = new Canvas(bitmap);
        canvas4 = new Canvas(bitmap);

        paintCentral = new Paint();
        paint1 = new Paint();
        paint2 = new Paint();
        paint3 = new Paint();
        paint4 = new Paint();

        imageView = findViewById(R.id.imageView);
        imageView.setImageBitmap(bitmap);


        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        renderFrame1();

                    }
                });

            }
        }, 200, 400);


    }


    private void renderFrame1() {


        canvasCentral.drawColor(Color.parseColor("#8D99A0"));
        paintCentral.setColor(Color.YELLOW);
        canvasCentral.drawCircle(mainCircleX, mainCircleY, mainRadius, paintCentral);


        paint1.setColor(Color.parseColor("#CC4125"));
        paint1.setStrokeWidth(10f);
        paint1.setStyle(Paint.Style.STROKE);

        paint2.setColor(Color.parseColor("#CC4125"));
        paint2.setStrokeWidth(10f);
        paint2.setStyle(Paint.Style.STROKE);

        paint3.setColor(Color.parseColor("#CC4125"));
        paint3.setStrokeWidth(10f);
        paint3.setStyle(Paint.Style.STROKE);

        paint4.setColor(Color.parseColor("#CC4125"));
        paint4.setStrokeWidth(10f);
        paint4.setStyle(Paint.Style.STROKE);

        /*******FIRST CIRCLE*******/
        canvas1.drawCircle(circle1X, circle1Y, 40, paint1);
        canvas1.rotate(angle, screenDimensions[0] / 2, screenDimensions[1] / 2);

        /*******SECOND CIRCLE*******/
        canvas2.drawCircle(circle2X, circle2Y, 40, paint2);
        canvas2.rotate(-50, screenDimensions[0] / 2, screenDimensions[1] / 2);

        /*******THIRD CIRCLE*******/
        canvas3.drawCircle(circle3X, circle3Y, 40, paint3);
        canvas3.rotate(30, screenDimensions[0] / 2, screenDimensions[1] / 2);

        /*******FOURTH CIRCLE*******/
        canvas4.drawCircle(circle4X, circle4Y, 40, paint4);
        canvas4.rotate(-80, screenDimensions[0] / 2, screenDimensions[1] / 2);

        imageView.invalidate();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM