2017-06-01 76 views
2

在我的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); 
     } 

我不是面對錯誤,但動畫不起作用。任何想法如何我可以從背景Thread動畫ImageView

回答

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>