How to animate a View around a circle?

前端 未结 3 1865
生来不讨喜
生来不讨喜 2020-12-29 15:11

I want to set an infinite move animation around a circle to a View like picture below.How can i achieve this?

This View may be a TextView o

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-29 15:42

    The problem is solved with this solution i made that rotate the container of the view clockwise and the TextView counter clockwise :)

    The layout:

    
    
        
    
    

    The animation:

        View v = findViewById(R.id.cont);
        View text = findViewById(R.id.text);
    
        int durationMillis = 10000;
    
        RotateAnimation r = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        r.setDuration(durationMillis);
        r.setInterpolator(new LinearInterpolator());
        r.setRepeatMode(Animation.RESTART);
        r.setRepeatCount(Animation.INFINITE);
        text.startAnimation(r);
    
        RotateAnimation rC = new RotateAnimation(360, 0,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rC.setDuration(durationMillis);
        rC.setInterpolator(new LinearInterpolator());
        rC.setRepeatMode(Animation.RESTART);
        rC.setRepeatCount(Animation.INFINITE);
        v.startAnimation(rC);
    

    But if you want to move an object in other paths and looking for a general solution, checkout the @Kevin's solution(Thanks to him).

提交回复
热议问题