2013-07-11 57 views
0

如何將imageview數組傳遞給以下代碼。代碼是將數組中的所有圖像調整大小並將其放入linearlayout。當然,我的代碼在時間只拍攝1張圖片。圖像數組for循環位圖Android

ImageView的數組:

private Integer[] Imgid = { 
       R.drawable.pic1, 
       R.drawable.pic2, 
       R.drawable.pic3, 

     }; 


    Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
       Imgid[5]); // currently taking only 1 image 


     int width = bitmapOrg.getWidth(); 
     int height = bitmapOrg.getHeight(); 
     int newWidth = 200; 
     int newHeight = 200; 

     // calculate the scale - in this case = 0.4f 
     float scaleWidth = ((float) newWidth)/width; 
     float scaleHeight = ((float) newHeight)/height; 

     // createa matrix for the manipulation 
     Matrix matrix = new Matrix(); 
     // resize the bit map 
     matrix.postScale(scaleWidth, scaleHeight); 
     // rotate the Bitmap 
     matrix.postRotate(0); 

     // recreate the new Bitmap 
     Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
          width, height, matrix, true); 

     // make a Drawable from Bitmap to allow to set the BitMap 
     // to the ImageView, ImageButton or what ever 
     BitmapDrawable bmd = new BitmapDrawable(getResources(),resizedBitmap); 



     LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.Linear); 
     for(int x=0;x<25;x++) { 
      ImageView imageView = new ImageView(this); 
      imageView.setPadding(2, 0, 9, 5); 
      imageView.setImageDrawable(bmd);    


linearLayout1.addView(imageView); 
    } 
+0

水平方向 – user2451541

回答

0

我重排的代碼,它工作正常。 你有25個R.drawable.pic,對吧? 每次添加ImageView時新的LinearLayout都是錯誤的。

private Integer[] Imgid = { 
    R.drawable.pic1, 
    R.drawable.pic2, 
    R.drawable.pic3, 
}; 

LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.Linear); 
for(int x=0;x<25;x++) { 
    Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),Imgid[x]); 


    int width = bitmapOrg.getWidth(); 
    int height = bitmapOrg.getHeight(); 
    int newWidth = 200; 
    int newHeight = 200; 

    // calculate the scale - in this case = 0.4f 
    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 

    // createa matrix for the manipulation 
    Matrix matrix = new Matrix(); 
    // resize the bit map 
    matrix.postScale(scaleWidth, scaleHeight); 
    // rotate the Bitmap 
    matrix.postRotate(0); 

    // recreate the new Bitmap 
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
         width, height, matrix, true); 

    // make a Drawable from Bitmap to allow to set the BitMap 
    // to the ImageView, ImageButton or what ever 
    BitmapDrawable bmd = new BitmapDrawable(getResources(),resizedBitmap); 

    ImageView imageView = new ImageView(this); 
    imageView.setPadding(2, 0, 9, 5); 
    imageView.setImageDrawable(bmd);    

    linearLayout1.addView(imageView); 
} 
+0

謝謝,這是我所需要的 – user2451541