2013-07-11 60 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