在我的MainActivity
中,我有一个callback
,它在后台线程上异步运行。我想在Animation
运行时Imageview
上显示thread
(轮换),所以我的回调是:
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);
}
我没有遇到错误,但动画不起作用。关于如何从背景ImageView
动画Thread
的任何想法?
答案 0 :(得分:1)
这是一种使用runOnUiThread从工作线程启动动画的方法。运行时,logcat输出应该显示工作线程的id> 1,而动画线程的id == 1.
<强> MainActivity.java 强>
公共类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>