0

如何将 imageview 数组传递给以下代码。代码是它从数组中获取所有图像调整大小并将其放入linearlayout. 目前,我的代码一次只拍摄 1 张图像。

图像视图数组:

    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);
    }
4

1 回答 1

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);
}
于 2013-07-11T08:09:49.293 回答