0

我有兩個活動。一個活動顯示來自一組圖像的一組隨機圖像。然後在10秒後,第二個活動顯示一組可點擊的隨機圖像。目標是讓用戶點擊兩個活動共有的圖像。該應用應該顯示正確的點擊次數和錯誤的點擊次數。我試圖比較兩種活動的可繪製性,但沒有運氣。請給我一個解決這個問題的方法。獲取點擊圖像的資源ID將有所幫助。但我無法找到任何能夠幫助我獲得這些的東西。我對Android來說相當陌生。[Android]比較兩個圖像/繪圖在Android的記憶遊戲?

MainActivity.java:

public class MainActivity extends Activity 
{ 
int i=5; 
static int[] imageViews ={ 
     R.id.imageView1,R.id.imageView2,R.id.imageView3, 
     R.id.imageView4,R.id.imageView5,R.id.imageView6, 
     R.id.imageView7,R.id.imageView8,R.id.imageView9, 
     R.id.imageView10,R.id.imageView11,R.id.imageView12, 
     R.id.imageView13,R.id.imageView14,R.id.imageView15, 
     R.id.imageView16,R.id.imageView17,R.id.imageView18 
    }; 

static int[] images = { 
    R.drawable.animalsimg1,R.drawable.animalsimg2,R.drawable.animalsimg3,   R.drawable.animalsimg4,R.drawable.animalsimg5,R.drawable.animalsimg6, 
     R.drawable.animalsimg7,R.drawable.animalsimg8,R.drawable.animalsimg9, 
     R.drawable.animalsimg10,R.drawable.animalsimg11,R.drawable.animalsimg12, 
     R.drawable.animalsimg13,R.drawable.animalsimg14,R.drawable.animalsimg15, 
     R.drawable.animalsimg16,R.drawable.animalsimg17,R.drawable.animalsimg18, 
     R.drawable.animalsimg19,R.drawable.animalsimg20 
    }; 

int[] a; 
int[] b; 
static int[] c=new int[5]; 



@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 


    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    a=RandomizeArray(images); 
    b=RandomizeArray(imageViews); 


    for(int j=0;j<4;j++){ 

     int imv = b[j]; 
     int im = a[j];    
     ImageView iv = (ImageView)findViewById(imv); 
     iv.setImageResource(im); 
     c[j]=a[j]; 

    } 







     new CountDownTimer(7000, 1000) { 

     TextView timertext=(TextView)findViewById(R.id.textView1); 

     public void onTick(long millisUntilFinished) { 
     timertext.setText("seconds remaining: " + millisUntilFinished/1000); 

     } 

     public void onFinish() { 
      timertext.setText("done!"); 

      finish(); 
      Intent intent=new Intent(MainActivity.this,SecondActivity.class); 
      startActivity(intent); 


     } 
     }.start(); 


    } 

public static int[] RandomizeArray(int[] array){ 
    Random rgen = new Random(); // Random number generator   

    for (int i=0; i<array.length; i++) { 
     int randomPosition = rgen.nextInt(array.length); 
     int temp = array[i]; 
     array[i] = array[randomPosition]; 
     array[randomPosition] = temp; 
    } 

    return array; 
} 

} 

SecondActivity.java:

public class SecondActivity extends Activity{ 


int[] a; 
int[] b; 
int p=0; 
int im; 
int imv; 
int[] c=new int[4]; 
int j; 
int correct=0; 
int wrong=0; 
TextView tv; 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.second); 
    tv=(TextView)findViewById(R.id.textView1); 


    a=RandomizeArray(MainActivity.images); 
    b=RandomizeArray(MainActivity.imageViews); 


    for(j=0;j<4;j++){ 

     imv = b[j]; 
     im = a[j];    
     ImageView iv = (ImageView)findViewById(imv); 
     iv.setImageResource(im); 

    } 
} 


    public void onClick(View v){ 


     for(p=0;p<4;p++){ 
      int s=c[p]; 
      if(((ImageView) v).getDrawable().getConstantState().equals(getResources().getDrawable(MainActivity.images[s]).getConstantState())){ 
       correct++; 
      } 
      else{ 
       wrong++; 
      } 


     v.setBackgroundResource(R.drawable.vattom_2); 
     tv.setText("Correct="+Integer.toString(correct)+" Wrong="+Integer.toString(wrong)); 
     } 
    } 


    public static int[] RandomizeArray(int[] array){ 
     Random rgen = new Random(); // Random number generator    

     for (int i=0; i<array.length; i++) { 
      int randomPosition = rgen.nextInt(array.length); 
      int temp = array[i]; 
      array[i] = array[randomPosition]; 
      array[randomPosition] = temp; 
     } 

     return array; 
    } 
    } 
+1

你應該將圖像(對圖像的引用)存儲在一個數組中。你會隨機選擇一個索引來獲取圖像。那麼你所要做的就是比較指數。 – alex

回答

0

您應該考慮使用標籤,我相信他們是容易比較,將解決你的問題