Android 单击按钮时按时间间隔更改ImageView

Android 单击按钮时按时间间隔更改ImageView,android,imageview,counter,imagebutton,Android,Imageview,Counter,Imagebutton,我正在尝试制作一款类似锻炼的应用程序。我有一个全屏ImageView和一个带有播放图标的ImageButton。我希望当我单击ImageButton时,图标立即变为停止图标,ImageView显示第一个练习的图像,该图像将在20秒后变为第二个练习的图像,但是如果想暂停计数器,我再次单击ImageButton停止它 我的布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:l

我正在尝试制作一款类似锻炼的应用程序。我有一个全屏
ImageView
和一个带有播放图标的
ImageButton
。我希望当我单击
ImageButton
时,图标立即变为停止图标,
ImageView
显示第一个练习的图像,该图像将在20秒后变为第二个练习的图像,但是如果想暂停计数器,我再次单击
ImageButton
停止它

我的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#FF5722"
    android:orientation="vertical"
    android:id="@+id/linearLayout">
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Inicio"
        android:textColor="#ffff"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_margin="8dp"
        android:layout_marginLeft="16dp"
        android:id="@+id/Boton1"
        android:onClick="sendMessage"
        android:clickable="true"
        />
    </LinearLayout>
   <RelativeLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_margin="16dp"
   android:layout_below="@+id/linearLayout"
   >
   <ImageView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/imagenRutina"
       android:layout_marginBottom="45dp"
       />

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="50dp"
       android:layout_alignParentBottom="true"
       android:layout_alignParentLeft="true"
       android:layout_alignParentStart="true"
       android:background="#ff5722"
       android:orientation="horizontal"
       android:id="@+id/linearLayout3">
   </LinearLayout>
   <ImageButton

   android:layout_width="50dp"
   android:layout_height="50dp"
   android:id="@+id/imageButton"
   android:layout_alignTop="@+id/linearLayout3"
   android:layout_centerHorizontal="true"
   android:background="#ff5722"
   android:clickable="true"
    android:layout_marginTop="5dp"
       />
   </RelativeLayout>
</RelativeLayout>
当我打开活动时,我得到的错误是致命的异常


如果我还想在单击
ImageButton
时播放一些声音,我该怎么做?

在监听器外部创建imageArray、imageView和处理程序不要重新发明控制盘,请使用
 import android.content.Intent;
 import android.os.CountDownTimer;
 import android.os.Handler;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.ImageButton;
 import android.widget.ImageView;

 public class Rutina1Reproduccion extends AppCompatActivity {

     ImageButton mPlayButton;


     boolean isPlay = false;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rutina1_reproduccion);


    mPlayButton = (ImageButton) findViewById(R.id.imageButton);
    background="@drawable/default"
    mPlayButton.setBackgroundResource(R.drawable.playicon);
    mPlayButton.setOnClickListener(mTogglePlayButton);
}
View.OnClickListener mTogglePlayButton = new View.OnClickListener(){
    final int[] imageArray = {R.drawable.imagengemelos,    R.drawable.imagengluteos, R.drawable.imagenbrazos};
    final ImageView imageView = (ImageView) findViewById(R.id.imagenRutina);
    final Handler handler = new Handler();
    @Override
    public void onClick(View v) {

        if (isPlay) {
            v.setBackgroundResource(R.drawable.playicon);
            Runnable runnable = new Runnable() {
                int i = 0;

                public void run() {
                    imageView.setImageResource(imageArray[i]);
                    i++;
                    if (i > imageArray.length - 1) {
                        i = 0;
                    }
                    handler.postDelayed(this, 3000);  //for interval...
                }
            };
            handler.postDelayed(runnable, 2000); //for initial delay..
        }else{
            v.setBackgroundResource(R.drawable.stopicon);

        }

        isPlay = !isPlay; // reverse
    }

};




public void sendMessage(View view){
    Intent intent=new Intent(Rutina1Reproduccion.this,Rutina1.class);
    startActivity(intent);
    }}