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

コードを再配置しましたが、正常に動作します。R.drawable.picは25個ありますよね?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 に答える