2010-12-28 5 views
0

私はこれを行う簡単な方法が必要であることを知っている、私はちょうどJavaがどのように十分に動作するのか分からない、と私は苦労しています。誰かがおそらく私を正しい方向に向けるだろうか?このコードをアンドロイドであまりにも乱雑にしようとしている人は誰でも助けてくれますか?

ImageButton image = (ImageButton) findViewById(R.id.card1); 
    ImageButton image2 = (ImageButton) findViewById(R.id.card2); 
    ImageButton image3 = (ImageButton) findViewById(R.id.card3); 
    ImageButton image4 = (ImageButton) findViewById(R.id.card4); 
    ImageButton image5 = (ImageButton) findViewById(R.id.card5); 
    ImageButton image6 = (ImageButton) findViewById(R.id.card6); 
    ImageButton image7 = (ImageButton) findViewById(R.id.card7); 
    ImageButton image8 = (ImageButton) findViewById(R.id.card8); 
    ImageButton image9 = (ImageButton) findViewById(R.id.card9); 
    ImageButton image10 = (ImageButton) findViewById(R.id.card10); 
    ImageButton image11 = (ImageButton) findViewById(R.id.card11); 
    ImageButton image12 = (ImageButton) findViewById(R.id.card12); 
    ImageButton image13 = (ImageButton) findViewById(R.id.card13); 
    ImageButton image14 = (ImageButton) findViewById(R.id.card14); 
    ImageButton image15 = (ImageButton) findViewById(R.id.card15); 
    //image.setImageResource(R.drawable.test2); 
    Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.vraag); 
    Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, buttonSize, buttonSize, true); 
    Bitmap bMap2 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag); 
    Bitmap bMapScaled2 = Bitmap.createScaledBitmap(bMap2, buttonSize, buttonSize, true); 
    Bitmap bMap3 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag); 
    Bitmap bMapScaled3 = Bitmap.createScaledBitmap(bMap3, buttonSize, buttonSize, true); 
    Bitmap bMap4 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag); 
    Bitmap bMapScaled4 = Bitmap.createScaledBitmap(bMap4, buttonSize, buttonSize, true); 
    Bitmap bMap5 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag); 
    Bitmap bMapScaled5 = Bitmap.createScaledBitmap(bMap5, buttonSize, buttonSize, true); 
    Bitmap bMap6 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag); 
    Bitmap bMapScaled6 = Bitmap.createScaledBitmap(bMap6, buttonSize, buttonSize, true); 
    Bitmap bMap7 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag); 
    Bitmap bMapScaled7 = Bitmap.createScaledBitmap(bMap7, buttonSize, buttonSize, true); 
    Bitmap bMap8 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag); 
    Bitmap bMapScaled8 = Bitmap.createScaledBitmap(bMap8, buttonSize, buttonSize, true); 
    Bitmap bMap9 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag); 
    Bitmap bMapScaled9 = Bitmap.createScaledBitmap(bMap9, buttonSize, buttonSize, true); 
    Bitmap bMap10 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag); 
    Bitmap bMapScaled10 = Bitmap.createScaledBitmap(bMap10, buttonSize, buttonSize, true); 
    Bitmap bMap11 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag); 
    Bitmap bMapScaled11 = Bitmap.createScaledBitmap(bMap11, buttonSize, buttonSize, true); 
    Bitmap bMap12 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag); 
    Bitmap bMapScaled12 = Bitmap.createScaledBitmap(bMap12, buttonSize, buttonSize, true); 
    Bitmap bMap13 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag); 
    Bitmap bMapScaled13 = Bitmap.createScaledBitmap(bMap13, buttonSize, buttonSize, true); 
    Bitmap bMap14 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag); 
    Bitmap bMapScaled14 = Bitmap.createScaledBitmap(bMap14, buttonSize, buttonSize, true); 
    Bitmap bMap15 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag); 
    Bitmap bMapScaled15 = Bitmap.createScaledBitmap(bMap15, buttonSize, buttonSize, true); 

    image.setImageBitmap(bMapScaled); 
    image2.setImageBitmap(bMapScaled2); 
    image3.setImageBitmap(bMapScaled3); 
    image4.setImageBitmap(bMapScaled4); 
    image5.setImageBitmap(bMapScaled5); 
    image6.setImageBitmap(bMapScaled6); 
    image7.setImageBitmap(bMapScaled7); 
    image8.setImageBitmap(bMapScaled8); 
    image9.setImageBitmap(bMapScaled9); 
    image10.setImageBitmap(bMapScaled10); 
    image11.setImageBitmap(bMapScaled11); 
    image12.setImageBitmap(bMapScaled12); 
    image13.setImageBitmap(bMapScaled13); 
    image14.setImageBitmap(bMapScaled14); 
    image15.setImageBitmap(bMapScaled15); 
+0

どのように配列を使用して?同じイメージを何度も読み込んでいるようですが、同じイメージを何度も繰り返し読み込むのではなく、繰り返し設定することはできないのでしょうか? –

答えて

3

おそらくコードを乱雑にするために配列を使用します。次のような方法を試してみてください。

// UPDATE: change from String to int 
int[] imageIds = new int[15] { 
    R.id.card1, R.id.card2, R.id.card3, 
    R.id.card4, R.id.card5, R.id.card6, 
    R.id.card7, R.id.card8, R.id.card9, 
    R.id.card10, R.id.card11, R.id.card12, 
    R.id.card13, R.id.card14, R.id.card15 
}; 

// load the source image only once 
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.vraag); 
Bitmap bScaled = Bitmap.createScaledBitmap(bMap, buttonSize, buttonSize, true); 

// render them all 
ImageButton[] imageButtons = new ImageButton[15]; 
for(int i = 0; i < imageButtons.size(); i++) { 
    ImageButton button = imageButtons[i] = (ImageButton) findViewById(imageIds[i]); 
    button.setImageBitmap(bMapScaled); 
} 

幸運を祈る!

+0

findViewById(imageIds [i]) - 整数を取る..どのように文字列を渡すことができます –

+0

ohh ..私はリファクタリングの部分を手伝っているだけです。実際にこれらの関数が何をしているのかは分かりません。私の推測。 –

+1

これは反射を使用する必要があります –

1

プライベートImageButtonイメージ[] [] =新しいイメージ[4] [4];

Class c=Class.forName("com.test.R$id"); 
Integer i = new Integer(c.getField("ToolsbuttonR1C1").getInt(new R.id())); 

//この場合、私たちはそれがR1C1形式であると仮定してみましょう - 行と列の

 for(int a=0,p=i;a<4;a++) 
      { 
       for(int b=0;b<4;b++) 
       { 
        image[a][b]=(Button)findViewById(p); 
        image[a][b].setOnClickListener(this); // an example 
        p++; 
       } 
     } 

IDが連続していない場合、これはおそらく、Uにエラーを与えるかもしれない...のような。もしuが をxmlのtablerowレイアウトにrowwiseを挿入していれば... R.idは連続していないので、最初のforループを別のP ++に追加する必要があります...

これはあなたですコードを減らすことができます..

+0

ありがとう!これは理にかなっており、構文をよりよく理解するのに役立ちます。 – clayton33

関連する問題