Defining the Visual Element
To create the visual representation of the circle, define a drawable resource file named res/drawable/round_shape.xml. This creates a solid oval shape.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#FF5722" />
<size
android:width="150dp"
android:height="150dp" />
</shape>
Configuring the Layout
In the layout file res/layout/activity_main.xml, add the target View component and center it within the parent container.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/rotatingIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_shape"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Implementing Animation Logic
The following logic utilizes ObjectAnimator to apply a continuous rotation effect to the view. This approach allows for smooth, infinite looping by manipulating the view's rotation property directly.
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class RotationActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View targetElement = findViewById(R.id.rotatingIndicator);
// Initialize the animator to rotate the view from 0 to 360 degrees
ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(targetElement, "rotation", 0f, 360f);
// Set the duration for a full rotation in milliseconds
rotationAnimator.setDuration(1200);
// Configure the animation to repeat infinitely
rotationAnimator.setRepeatCount(ValueAnimator.INFINITE);
// Ensure smooth linear movement without acceleration/deceleration
rotationAnimator.setRepeatMode(ValueAnimator.RESTART);
// Start the animation sequence
rotationAnimator.start();
}
}