在 Android 中使用位图的最佳方式;)

发布于 2024-12-16 13:20:36 字数 1839 浏览 2 评论 0原文

我必须为平板电脑创建 Android 应用程序,应用程序将显示新杂志和他的页面。每本杂志大约有 70 页,每页都有封面图像,大小约为 700 000 字节。应用程序的主页显示大图像和带有图像的小画廊(画廊视图)。我在 android 3.2 的模拟器上工作。当我将图像添加到图库并尝试滑动它时,它工作不顺利。有时不会加载所有图像,LogCat 会向我显示此信息:

11-17 14:30:51.598: D/skia(5868): libjpeg error 105 <  Ss=%d, Se=%d, Ah=%d, Al=%d> from read_scanlines [128 168]
11-17 14:30:51.598: D/skia(5868): --- decoder->decode returned false

现在我将大约 7 张图像放入画廊中,我按如下方式缩放:

public Bitmap decodeFile(String f) {
    try {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        final int REQUIRED_SIZE=75;

        int width_tmp=o.outWidth, height_tmp=o.outHeight;
        int scale=1;
        while(true) {
            if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale*=2;
        }
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
}

并在画廊中显示如下:

    public View getView(int position, View convertView, ViewGroup parent) {
        View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem, null);
        ImageView iV = (ImageView) retval.findViewById(R.id.image);
        String path = ArrayHelper.list.get(position).get("pageId").toString();
        Bitmap bP = decodeFile(Environment.getExternalStorageDirectory() + "/MCW/" + path + "/head.jpg");
        iV.setImageBitmap(bP);
        return retval;
    }

将来我将在画廊中显示更多图像,我可以想象如何它会起作用的。

我的问题是:我应该做什么?我应该如何加载图像?

I must create android application for tablets, app will be shows new magazines and his pages. Every magazine has about 70 pages, and every page have cover as image which weighs about 700 000 bytes. Main page of app show big image and small gallery (Gallery View) with images. I work on emulator with andrid 3.2. When I add images to gallery and I try slide it, it does't work smoothly. Sometimes does't load all images and LogCat show me this info:

11-17 14:30:51.598: D/skia(5868): libjpeg error 105 <  Ss=%d, Se=%d, Ah=%d, Al=%d> from read_scanlines [128 168]
11-17 14:30:51.598: D/skia(5868): --- decoder->decode returned false

Now I put into gallery about 7 images which I scale like this:

public Bitmap decodeFile(String f) {
    try {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        final int REQUIRED_SIZE=75;

        int width_tmp=o.outWidth, height_tmp=o.outHeight;
        int scale=1;
        while(true) {
            if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale*=2;
        }
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
}

and show in gallery like this:

    public View getView(int position, View convertView, ViewGroup parent) {
        View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem, null);
        ImageView iV = (ImageView) retval.findViewById(R.id.image);
        String path = ArrayHelper.list.get(position).get("pageId").toString();
        Bitmap bP = decodeFile(Environment.getExternalStorageDirectory() + "/MCW/" + path + "/head.jpg");
        iV.setImageBitmap(bP);
        return retval;
    }

in future I will be have more imges in gallery and I can's imagine how it will be working.

My question is: what i should be doing ? How I should load images ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘东忘西忘不掉你 2024-12-23 13:20:36

您问了一个一般性问题,所以我所能做的就是给您一个一般性答案。您不应该为杂志中的整个页面提供位图。您应该只对页面的图片部分使用位图。其余的应该是实际的文本。这将大大减少您的内存占用。此外,您应该延迟加载这些位图。查看此讨论关于如何延迟加载图像的建议。

You've asked a general question, so the best I can do is give you a general answer. You shouldn't have a bitmap for an entire page in the magazine. You should only use bitmaps for the picture portions of the page. The rest should be actual text. This will dramatically cut down your memory foot print. Furthermore, you should then lazy load those bit maps. Checkout this discussion for advice on how to lazy load images.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文