Android:存储图像的引用

时间:2016-08-26 14:31:04

标签: java android

我几天前问了一个类似的问题,现在想要准确一点:

我希望一次又一次地显示相同的图像,并且不想每次都重新加载它以加快应用程序。

为了保持简单并解决我的问题,我刚刚创建了以下示例:

imageView = (ImageView) this.findViewById(R.id.imageView);

for (int i = 0; i < 1000; i++) {

     imageView.setImageResource(R.mipmap.myImage);
}

我正在加载我的图片&#39; myImage&#39; 1000次并且优先加载一次,然后通过它的参考显示它。

有没有办法在变量中存储图像的引用?或者我必须以某种方式缓存图像吗?

有谁知道如何管理这个?

4 个答案:

答案 0 :(得分:1)

您可以使用本文https://developer.android.com/training/displaying-bitmaps/cache-bitmap.html中的建议。

或使用其中一个库来管理图像(例如Picasso)。它会进行缓存,调整大小等而不是你。这是最简单的方法。

答案 1 :(得分:0)

它使用位图如下代码

Bitmap bitmap=null;
File f= new File(_path);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        try {
            bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

image.setImageBitmap(bitmap);

Android: How to load image into Bitmap

图像有各种形状和大小。在许多情况下,它们比典型应用程序用户界面所需的要大。让我们参考

https://developer.android.com/training/displaying-bitmaps/load-bitmap.html

祝你好运

答案 2 :(得分:-1)

您可以随时将图片存储到Bitmap静态对象和setImageBitmap来电。

要将drawable加载到Bitmap中,可以使用以下代码:

Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.myImage);

希望它有所帮助。

答案 3 :(得分:-1)

ImageView.setImageResource(int id)

public void setImageResource(@DrawableRes int resId) {
    // The resource configuration may have changed, so we should always
    // try to load the resource even if the resId hasn't changed.
    final int oldWidth = mDrawableWidth;
    final int oldHeight = mDrawableHeight;

    updateDrawable(null);
    mResource = resId;
    mUri = null;

    resolveUri();

    if (oldWidth != mDrawableWidth || oldHeight != mDrawableHeight) {
        requestLayout();
    }
    invalidate();
}

resolveUri()

private void resolveUri() {
    if (mDrawable != null) {
        return;
    }

    Resources rsrc = getResources();
    if (rsrc == null) {
        return;
    }

    Drawable d = null;

    if (mResource != 0) {
        try {
            d = mContext.getDrawable(mResource);
        } catch (Exception e) {
            Log.w("ImageView", "Unable to find resource: " + mResource, e);
            // Don't try again.
            mUri = null;
        }
    } else if (mUri != null) {
        String scheme = mUri.getScheme();
        if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) {
            try {
                // Load drawable through Resources, to get the source density information
                ContentResolver.OpenResourceIdResult r =
                        mContext.getContentResolver().getResourceId(mUri);
                d = r.r.getDrawable(r.id, mContext.getTheme());
            } catch (Exception e) {
                Log.w("ImageView", "Unable to open content: " + mUri, e);
            }
        } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)
                || ContentResolver.SCHEME_FILE.equals(scheme)) {
            InputStream stream = null;
            try {
                stream = mContext.getContentResolver().openInputStream(mUri);
                d = Drawable.createFromStream(stream, null);
            } catch (Exception e) {
                Log.w("ImageView", "Unable to open content: " + mUri, e);
            } finally {
                if (stream != null) {
                    try {
                        stream.close();
                    } catch (IOException e) {
                        Log.w("ImageView", "Unable to close content: " + mUri, e);
                    }
                }
            }
    } else {
            d = Drawable.createFromPath(mUri.toString());
        }

        if (d == null) {
            System.out.println("resolveUri failed on bad bitmap uri: " + mUri);
            // Don't try again.
            mUri = null;
        }
    } else {
        return;
    }

    updateDrawable(d);
}

事实上,当您随着时间的推移加载相同的图像时,resolveUri()将浪费大量时间。因此,您可以存储Drawable对象。

 context.getResources().getDrawable(resId);