gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 13:52:38 24 4
gpt4 key购买 nike

在我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com