Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start an animation from a task running on a background thread

In my MainActivity I have a callback whose runs asynchronously on a background thread. I would like to show an Animation (rotation) over an Imageview while the thread is running , so my callback is:

        pull.addChangeListener(new Replication.ChangeListener() {
            @Override
            public void changed(Replication.ChangeEvent event) {

               if (REPLICATION_ACTIVE)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                           startAnimation();//I call here to the animation                              
                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                         stopAnimation()
                        }
                    });
                }

The startAnimation() method

 private void startAnimation(){
                isAnimationDone = true;
                imgSincronizacion.setImageResource(R.mipmap.btn_sync_on);
                Animation rotation= AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotation);
                rotacion.setRepeatMode(Animation.ABSOLUTE);
                rotacion.setRepeatCount(Animation.INFINITE);
                imgSincronizacion.startAnimation(rotation);
        }

Im not facing errors but the animation does not not work. Any idea about how I can animate an ImageView from a background Thread ?

like image 814
JoCuTo Avatar asked Dec 16 '25 13:12

JoCuTo


1 Answers

Here's an approach that starts an animation from a worker thread, using runOnUiThread. When run, the logcat output should show that the worker thread has an id > 1 while the animation thread has id == 1.

MainActivity.java

public class MainActivity extends AppCompatActivity {

private static final String TAG = MainActivity.class.getSimpleName();

private ImageView imageView = null;
private Button button = null;

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

    imageView = (ImageView)findViewById(R.id.imageView);
    button = (Button)findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startAnimationFromBackgroundThread();
        }
    });

}

public void startAnimationFromBackgroundThread() {
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    executorService.submit(new Runnable() {
        @Override
        public void run() {
            // this runs on a background thread
            Log.v(TAG, "Worker thread id:" + Thread.currentThread().getId());
            MainActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.v(TAG, "Animation thread id:" + Thread.currentThread().getId());
                    Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim);
                    imageView.startAnimation(animation);
                }
            });
        }
    });
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.albertcbraun.animationsimple.MainActivity">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@android:drawable/btn_star_big_on"
        tools:layout_editor_absoluteX="176dp"
        tools:layout_editor_absoluteY="239dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Animate"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />
</android.support.constraint.ConstraintLayout>

anim.xml

<set android:shareInterpolator="false"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.4"
        android:fromYScale="1.0"
        android:toYScale="0.6"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false"
        android:duration="700" />
    <set android:interpolator="@android:anim/decelerate_interpolator">
        <scale
            android:fromXScale="1.4"
            android:toXScale="0.0"
            android:fromYScale="0.6"
            android:toYScale="0.0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:startOffset="700"
            android:duration="400"
            android:fillBefore="false" />
        <rotate
            android:fromDegrees="0"
            android:toDegrees="-45"
            android:toYScale="0.0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:startOffset="700"
            android:duration="400" />
    </set>
</set>
like image 122
albert c braun Avatar answered Dec 19 '25 06:12

albert c braun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!