Android studio骰子案例 怎么替换一张图片 前一张图片恢复原来图片

摇中一个数 前一个图片怎么取消替换 如图



package com.example.touzi;

import androidx.appcompat.app.AppCompatActivity;

import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    ImageButton photo0;
    ImageView[] imgs = new ImageView[8];
    int[] ids = {R.drawable.pics1,R.drawable.pics2,R.drawable.pics3,R.drawable.pics4,R.drawable.pics5,R.drawable.pics6,R.drawable.pics7,R.drawable.pics8};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylayout);

        photo0 = (ImageButton)findViewById(R.id.photo0);
        photo0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               int n = new Random().nextInt()%8;
               n = Math.abs(n);
                if(n == 6 || n == 7){
                    Toast.makeText(MainActivity.this,"再来一次",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(MainActivity.this,"摇中"+(n+1),Toast.LENGTH_LONG).show();
                }

                imgs[0] = (ImageView)findViewById(R.id.photo1);
                imgs[0] = (ImageView)findViewById(R.id.photo1);
                imgs[1] = (ImageView)findViewById(R.id.photo2);
                imgs[2] = (ImageView)findViewById(R.id.photo3);
                imgs[3] = (ImageView)findViewById(R.id.photo4);
                imgs[4] = (ImageView)findViewById(R.id.photo5);
                imgs[5] = (ImageView)findViewById(R.id.photo6);
                imgs[6] = (ImageView)findViewById(R.id.photo7);
                imgs[7] = (ImageView)findViewById(R.id.photo8);
                imgs[n].setImageResource(ids[n]);
            }
        });
    }
}

MainActivity.java代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:text=""
        android:textSize="28sp"
        android:textStyle="bold"
        android:gravity="center"
        android:textColor="#000"/>
    <ImageButton
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/mybtn"
        android:id="@+id/photo0"
        android:scaleType="fitXY"
        android:layout_centerInParent="true"/>
    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/pic7"
        android:id="@+id/photo7"
        android:layout_toRightOf="@id/photo0"
        android:layout_alignTop="@id/photo0"
        android:layout_marginLeft="20dp"/>
    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/pic8"
        android:id="@+id/photo8"
        android:layout_toLeftOf="@id/photo0"
        android:layout_alignTop="@id/photo0"
        android:layout_marginRight="20dp"/>
    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:id="@+id/photo2"
        android:src="@drawable/pic2"
        android:layout_above="@id/photo0"
        android:layout_alignRight="@id/photo0"
        android:layout_marginBottom="20dp"/>
    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/pic1"
        android:id="@+id/photo1"
        android:layout_toLeftOf="@id/photo2"
        android:layout_alignTop="@id/photo2"
        android:layout_marginRight="20dp"/>
    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/pic3"
        android:id="@+id/photo3"
        android:layout_toRightOf="@id/photo2"
        android:layout_alignTop="@id/photo2"
        android:layout_marginLeft="20dp"/>
    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/pic5"
        android:id="@+id/photo5"
        android:layout_below="@id/photo0"
        android:layout_alignLeft="@id/photo0"
        android:layout_marginTop="20dp"/>
    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/pic4"
        android:id="@+id/photo4"
        android:layout_toLeftOf="@id/photo5"
        android:layout_alignTop="@id/photo5"
        android:layout_marginRight="20dp"/>
    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/pic6"
        android:id="@+id/photo6"
        android:layout_toRightOf="@id/photo5"
        android:layout_alignTop="@id/photo5"
        android:layout_marginLeft="20dp"/>

</RelativeLayout>

布局代码

定义一个变量 记录上次选中的位置下标,每次重新选中时,先根据记录的先还原,设置新的选中图片,最好更新选中的下标。

下面是一段伪代码

//记录上一次选中的位置
int lastPosition=-1;


    void onclick(){

        int newPostion=新位置下标;

        if(lastPostion!=-1){
            
        //根据原来的位置还原
        images[lastPostion]=原图片数组[lastPostion];
            
    }

     images[newPosition]=选中图片的数组[newPosition];

    //更新选中的位置

    lastPosition=newPosition;

}