android - 从在后台线程上运行的任务启动动画

标签 android callback background

在我的 MainActivity 中,我有一个 callback,它在后台线程上异步运行。我想在 thread 运行时在 Imageview 上显示一个 Animation(旋转),所以我的回调是:

        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()
                        }
                    });
                }

startAnimation() 方法

 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);
        }

我没有遇到错误,但动画不工作。关于如何从后台 ThreadImageView 设置动画有什么想法吗?

最佳答案

这是一种使用 runOnUiThread 从工作线程启动动画的方法。运行时,logcat 输出应显示工作线程的 id > 1,而动画线程的 id == 1。

MainActivity.java

公共(public)类 MainActivity 扩展 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>

关于android - 从在后台线程上运行的任务启动动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44308697/

相关文章:

安卓 : Switch (ToggleButton) and threads

Android 是否有使用 addView 的 linearLayout 的 ClickListener?

java - 具有 3 个 TextView 和 1 个单选按钮的动态 ListView。这是可能的?

java - 安卓 : faster solution to copying one file to another

c# - 将 C++ 迁移到 C#(回调)

javascript - 我想确保我之前的函数必须在 Nodejs 服务器端脚本中的以下代码之前完全执行

javascript - 使用javascript在页面加载时切换背景

javascript - 通过 Window.open 的 Web Intent 回调事件只是没有发生

android - 窗口背景 : Image with selected color

ios - 如何使用 swift 在 Xcode 中设置背景图片?