从可绘制高效的Andr​​oid载入图像 [英] Android Load Images from Drawable Efficiently

查看:108
本文介绍了从可绘制高效的Andr​​oid载入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid开发,我已经有很多困难,找出如何有效地加载图像。我的问题的短版如下:我在我的RES /文件夹绘制某些PNG图像。我怎样才能加载这些图片到Android设备,而不用担心出现OutOfMemory错误?

I'm new to Android development and I've had a lot of difficulty figuring out how to load images efficiently. The short version of my question is as follows: I have some png images in my res/drawable folder. How can I load these images onto an Android device without risking an OutOfMemory error?

下面是关于我的问题,一些细节和我试图修复它:

Here are some more details about my problem and my attempts to fix it:

我写了一个非常简单的程序,仅仅显示一个图像。这里的code:

I've written a very simple program that simply displays one image. Here's the code:

public class TitleScreenActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_title_screen);
}

和这里的code在R.layout.activity_title_screen:

And here's the code in R.layout.activity_title_screen:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@color/white"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/title_screen_logo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:maxWidth="20000dp"
        android:maxHeight="20000dp"
        android:scaleType="fitCenter"
        android:contentDescription="@string/accessibility"
        android:src="@drawable/title_screen_logo" />
</RelativeLayout>

不幸的是,当我在模拟器上运行这个程序,我收到java.lang.OutOfMemoryError。我决定了这个错误的原因是,我从R.drawable.title_screen_logo加载,因为当我打开一个不同的图像程序工作正常图像。什么是这个令人困惑的是,图像是一个非常合理的大小考虑到我只装入一张:183KB。因为我只装载一个图像,我不明白为什么它是造成此错误。可以在错误被其他比大小图像的属性引起的?

Unfortunately, when I run this program on an emulator, I get a java.lang.outOfMemoryError. I determined the cause of this error to be the image I'm loading from R.drawable.title_screen_logo because the program works fine when I load a different image. What's confusing about this is that the image is an extremely reasonable size considering that I'm only loading one: 183KB. Since I'm only loading one image, I don't understand why it's causing this error. Can the error be caused by a property of the image other than size?

我花了几天时间研究这个问题,并发现了一些半体面的解决方案,但没有一样工作完全。我用毕加索尝试,并提出以下修改我的活动:

I've spent days researching this issue and have found some half-decent solutions, but none that work perfectly. I've tried using Picasso and made the following modification to my Activity:

public class TitleScreenActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_title_screen);
        ImageView imageView = (ImageView) findViewById(R.id.title_screen_logo);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(displayMetrics);
        int width = displayMetrics.widthPixels;
        Picasso.with(this)
                .load(R.drawable.title_screen)
                .resize(width, (int) ((width) *  0.736284058685735))
                .into(imageView);
}

基本上,这是什么code做的是找到该设备的宽度,然后调整图像有宽度和高度适中(0.736284058685735是图像高度:宽度比这code的作品,但我有这种方法相当的几个问题:

Basically, what this code does is find the width of the device and then resize the image to have that width and the appropriate height (0.736284058685735 is image's height:width ratio. This code works, but I have quite a few issues with this approach:


  1. 这似乎过多/不必要需要运行时每一个形象我想加载过程中做到这一点。使用这种方法会大大减少我的code的可读性。

  2. 我不得不删除机器人:我的ImageView src属性,因此没有在code RAN的毕加索部分之前超载的内存。这意味着,直到毕加索放入它的东西,这实际上造成在该屏幕只是空白程序后,会打开一个短暂而明显的延迟了的ImageView不会显示任何内容。

  3. 其实,这似乎并没有彻底解决问题;它的工作原理,如果我用毕加索缩小图片的宽度屏幕宽度的一半(然后让ImageView的规模它备份的缩放属性),但它仍然创建了一个内存不足的错误,如果我只是按比例缩小图片,以在屏幕的整个宽度。而扩展到屏幕的宽度使得其加载的一半,它会创建质量的明显的损失,它看起来像一个任意号码;我不能保证内存更少的设备将能够加载缩放到其宽度的一半的版本。

由于这些问题,我现在考虑将一堆是必要的,以适应每个Android系统指定的屏幕大小和密度(华电国际,MDPI,小,正常,只等的最小尺寸和分辨率不同的图像)不过,我不知道这甚至会解决问题,因为像这么小开始。我不知道有多少小我真的质量下降之前做它,我也不能确定我需要多少小,使其避免内存错误。我觉得必须有一个更好的办法,但对我的生活,我不能找到它...

Because of these issues, I'm now considering making a bunch of different images that are only the minimum size and resolution necessary to fit on each Android-specified screen size and density (hdpi, mdpi, small, normal, etc.) However, I'm not sure that this will even solve the problem since the image was so small to begin with. I'm not sure how much smaller I can really make it before decreasing quality, and I'm also not sure how much smaller I'd need to make it to avoid the memory errors. I feel like there must be a better way, but for the life of me I can't find it...

推荐答案

在使用这个链接转换768,16的PNG图像(xxhdpi,MDPI等。)
    https://romannurik.github.io/AndroidAssetStudio/nine-patches.html
    自动图像chaged根据您的设​​备

you shoud convert your png image (xxhdpi,mdpi etc..) using this link https://romannurik.github.io/AndroidAssetStudio/nine-patches.html automatically image chaged depending upon your device

这篇关于从可绘制高效的Andr​​oid载入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆