2017-02-16 101 views
0

我創建了一個彩色背景動畫,它在按鈕單擊時開始(它平滑地更改背景顏色)。 我的問題是,我無法停止動畫(如果我再次點擊按鈕,它會提高動畫速度)。我已經嘗試創建一個while循環,但動畫不再工作。我的目標是,如果我按下圖像Btn,動畫就會開始,如果再次按下它,它應該停止。如何停止此動畫?

代碼:

imageButton_info.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v){ 

       // Handler and runnable to run the animation in devices sdk below honeycomb. 
       final Handler handler = new Handler(); 

       Runnable ChangeBackgroundRunnable = new Runnable() { 

        @Override 
        public void run() { 
         number++; 
         float[] hsvColor = {0, 1, 1}; 
         hsvColor[0] = 360f * number/100; 
         color.setBackgroundColor(Color.HSVToColor(hsvColor)); 

         handler.postDelayed(this, 80); 
         if (number >=100) 
          number = 1; 
        } 
       }; 
       number = 0; 
       handler.removeCallbacks(ChangeBackgroundRunnable); 
       handler.postDelayed(ChangeBackgroundRunnable, 0); 

     } 
    }); 
+0

使用ValueAnimator代替 – 2017-02-16 00:41:29

+0

但創建程序工作近乎完美,應該有辦法阻止它? – Nick88

+0

檢查此舊的計算器[答案](http://stackoverflow.com/a/9458785/5156016)。 – 2017-02-16 00:58:14

回答

0

看你的代碼(再次)你removeCallbacks方法傳遞您的Runnable的實例,它是尚未運行。

您需要將Runnable的實例存儲在Click Listener外部。我僅僅指剛敲了一個簡單的例子,顯示我的想法:

public class MainActivity extends AppCompatActivity { 

    TextView textView; 
    Button startBtn; 
    int counter = 0; 
    boolean running = false; 

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


     textView = (TextView) findViewById(R.id.tvCounter); 
     startBtn = (Button) findViewById(R.id.startBtn); 

     final Handler counterHandler = new Handler(Looper.getMainLooper()); //Just to be specific 
     final Runnable myRunnable = new Runnable() { 
      @Override 
      public void run() { 
       textView.setText(String.format("Count is %s", counter ++)); 
       counterHandler.postDelayed(this, 100); 
      } 
     }; 


     startBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if(running) { 
        counterHandler.removeCallbacks(myRunnable); 
        running = false; 
       } else { 
        counterHandler.post(myRunnable); 
        running = true; 
       } 


      } 
     }); 

    } 

} 

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    android:id="@+id/activity_main" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="example.com.test.MainActivity"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
    android:id="@+id/tvCounter"/> 

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
      android:id="@+id/startBtn" android:text="Start Stop Button"/> 
</RelativeLayout> 
+0

非常感謝您的回答,您的建議工作完美! – Nick88