OutofMemoryError ImageView

时间:2012-06-20 09:49:56

标签: android out-of-memory viewflipper

我有一个imageview的问题,我有77张图片jpg并有126个引用文字,只是简单的应用程序我想要的是我想显示所有报价与背景图片,我使用viewflipper所以当我向左/向右滑动进入下一个报价。我给你我现在使用的代码。希望任何人都可以帮助我..真的浪费我的时间来解决它。我的图片:320x480。

                    vf = (ViewFlipper) findViewById(R.id.pages);
            for (i = 0; i<totalphrase;i++) {
                Log.i("ID",""+i);
                var = arrayphrase.get(i);

                //iv.setBackgroundColor(Color.rgb((int)Math.ceil(Math.random()*100), (int)Math.ceil(Math.random()*100), (int)Math.ceil(Math.random()*100)));
                FrameLayout a = new FrameLayout(this);
                FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT); 
                a.setLayoutParams(lp);

                b = new ImageView(this);
                LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
                FrameLayout.LayoutParams lp2= new FrameLayout.LayoutParams(740, 800,Gravity.CENTER);
                //b.setBackgroundColor(Color.rgb((int)Math.ceil(Math.random()*100), (int)Math.ceil(Math.random()*100), (int)Math.ceil(Math.random()*100)));

   // i got outofmemoryerror with this, Imgid[] -> my picture in drawable there ara 77picture
    b.setBackgroundResource(Imgid[(int)(Math.random()*Imgid.length)]);
    // -- ERROR --  outofmemory             
                b.setLayoutParams(lp1);
                a.addView(b);

                c = new ScrollView(this);
                c.setLayoutParams(lp2);
                //c.setBackgroundColor(Color.CYAN);
                c.setBackgroundColor(Color.argb(65, 53, 52, 52));
                //idsc = c.getId();
                a.addView(c);

                LinearLayout d = new LinearLayout(this);
                d.setLayoutParams(lp1);
                //d.setBackgroundResource(R.drawable.background0);
                c.addView(d);


                TextView e = new TextView(this);
                LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(730, 800);
                e.setLayoutParams(lp1);
                e.setText(var.phraseKey);

                d.addView(e);


                vf.addView(a);

            }

        }

我想要的结果如下:example that i want to build in android

这是xml

        <?xml version="1.0" encoding="utf-8"?>

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:orientation="vertical"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:id="@+id/fl1"
        >


        <ViewFlipper

            android:id="@+id/pages"

            android:layout_width="fill_parent"

            android:layout_height="fill_parent" 

            android:background="@android:color/transparent"

            android:layout_gravity="top"
            >
        </ViewFlipper>


    </FrameLayout>

3 个答案:

答案 0 :(得分:1)

我建议创建一个包含注释和图片路径的2 dim数组。 对于图片,请创建缩略图。我正在使用此代码

BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 4;
pic = BitmapFactory.decodeFile(strPicPath, options);
image.setImageBitmap(pic);

我不确定图像是否因较大的inSampleSize而变小......你必须尝试

图片采用XML格式:

<ImageView
                android:id="@+id/imagePreview"
                android:layout_width="134dp"
                android:layout_height="146dp"
                android:layout_weight="0.36"
                android:onClick="imageClick" />
</LinearLayout>

我使用on click方法打开图库中的图像,但你可以进行缩放或你想要的。

答案 1 :(得分:0)

要修复OutOfMemory,你应该做类似的事情:

这是一个代码我参考它的工作好检查它

BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options);

此inSampleSize选项可减少内存消耗。

这是一个完整的方法。首先,它读取图像大小而不解码内容本身。然后它找到最好的inSampleSize值,它应该是2的幂。最后图像被解码。

//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        //The new size we want to scale to
        final int REQUIRED_SIZE=70;

        //Find the correct scale value. It should be the power of 2.
        int scale=1;
        while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
            scale*=2;

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
}

答案 2 :(得分:0)

在更改图像之前尝试放置此代码:

imageView.getDrawable().getBitmap().recycle();