0

我有一个包含超过 15 个位图的 ArrayList。此位图必须从 sdcard 中读取。我的数组在循环中。所以我不想每次都从存储中读取位图,我想将此位图保存为缓存。我的位图数组列表小于 2 Mb。我想在我的自定义视图中用画布显示它。所以我认为最好将它保存为缓存或其他东西,因为位图的数量。我认为从 sdcard 读取超过 15 个位图是没有意义的,即使它们很轻(大约 15kb)请帮助我如何做到这一点。另外每个位图是 256*256 像素。

Bitmap bmp;
String zoom = String.valueOf(ZoomLevel);
File file = new File(Environment.getExternalStorageDirectory()+"/GreatMap/"+zoom);
File image = FolderExist(ZoomLevel);
if (file.exists())
{   
    if(image.exists())
    {
        for(int a = 1; a < 20 ;a++)
        {
            bmp = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/GreatMap/"+zoom+"/"+a+".jpg");
            Images.add(a,bmp);                          

        }
        postInvalidate();

        }
}
4

2 回答 2

0

使用下面的类从 sd 卡获取图像。它将在缓存中保留图像。您还可以从 url 和 sd 卡获取图像。这对双方都有效。

只需初始化此类并获取图像。

imageLoader = new ImageLoader(this);

并调用其方法从 sd 卡中获取图像并将照片设置在您想要的位置。

public class ImageLoader {

MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews = Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
Handler handler = new Handler();// handler to display images in UI thread

Context mContext;
boolean imageWithReflection;

public ImageLoader(Context context) {
    fileCache = new FileCache(context);
    executorService = Executors.newFixedThreadPool(5);
    this.mContext = context;
    this.stubImageBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.default_image);
}

final Bitmap stubImageBitmap;

public void displayImageFromUrl(String url, ImageView imageView, boolean imageWithReflection) {
    this.imageWithReflection = imageWithReflection;
    imageViews.put(imageView, url);
    Bitmap bitmap = memoryCache.get(url);
    if (bitmap != null) {
        if (this.imageWithReflection)
            imageView.setImageBitmap(createReflectedImage(bitmap));
        else
            imageView.setImageBitmap(bitmap);
    } else {
        queuePhoto(url, imageView);
        if (this.imageWithReflection)
            imageView.setImageBitmap(createReflectedImage(stubImageBitmap));
        else
            imageView.setImageBitmap(stubImageBitmap);
    }
}

private void queuePhoto(String url, ImageView imageView) {
    PhotoToLoad p = new PhotoToLoad(url, imageView);
    executorService.submit(new PhotosLoader(p));
}

private Bitmap getBitmap(String url) {
    File f = fileCache.getFile(url);

    // from SD cache
    Bitmap b = decodeFile(f);
    if (b != null)
        return b;

    // from web
    try {
        Bitmap bitmap = null;
        URL imageUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
        conn.setConnectTimeout(30000);
        conn.setReadTimeout(30000);
        conn.setInstanceFollowRedirects(true);
        InputStream is = conn.getInputStream();
        OutputStream os = new FileOutputStream(f);
        ActivityUtils.copyStream(is, os);
        os.close();
        conn.disconnect();
        bitmap = decodeFile(f);
        return bitmap;
    } catch (Throwable ex) {
        ex.printStackTrace();
        if (ex instanceof OutOfMemoryError)
            memoryCache.clear();
        return null;
    }
}

// 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;
        FileInputStream stream1 = new FileInputStream(f);
        BitmapFactory.decodeStream(stream1, null, o);
        stream1.close();

        // Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE = 70;
        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;
        }

        // decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        FileInputStream stream2 = new FileInputStream(f);
        Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2);
        stream2.close();
        return bitmap;
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

// Task for the queue
private class PhotoToLoad {
    public String url;
    public ImageView imageView;

    public PhotoToLoad(String u, ImageView i) {
        url = u;
        imageView = i;
    }
}

class PhotosLoader implements Runnable {
    PhotoToLoad photoToLoad;

    PhotosLoader(PhotoToLoad photoToLoad) {
        this.photoToLoad = photoToLoad;
    }

    @Override
    public void run() {
        try {
            if (imageViewReused(photoToLoad))
                return;
            Bitmap bmp = getBitmap(photoToLoad.url);
            memoryCache.put(photoToLoad.url, bmp);
            if (imageViewReused(photoToLoad))
                return;
            BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
            handler.post(bd);
        } catch (Throwable th) {
            th.printStackTrace();
        }
    }
}

boolean imageViewReused(PhotoToLoad photoToLoad) {
    String tag = imageViews.get(photoToLoad.imageView);
    if (tag == null || !tag.equals(photoToLoad.url))
        return true;
    return false;
}

// Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable {
    Bitmap bitmap;
    PhotoToLoad photoToLoad;

    public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
        bitmap = b;
        photoToLoad = p;
    }

    public void run() {
        if (imageViewReused(photoToLoad))
            return;
        if (bitmap != null) {
            if (imageWithReflection)
                photoToLoad.imageView.setImageBitmap(createReflectedImage(bitmap));
            else
                photoToLoad.imageView.setImageBitmap(bitmap);
        } else {
            if (imageWithReflection)
                photoToLoad.imageView.setImageBitmap(createReflectedImage(stubImageBitmap));
            else
                photoToLoad.imageView.setImageBitmap(stubImageBitmap);
        }
    }
}

public void clearCache() {
    memoryCache.clear();
    fileCache.clear();
}

private Bitmap createReflectedImage(Bitmap originalImage) {
    final int reflectionGap = 4;

    int width = originalImage.getWidth();
    int height = originalImage.getHeight();

    // This will not scale but will flip on the Y axis
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);

    // Create a Bitmap with the flip matrix applied to it.
    // We only want the bottom half of the image
    Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width, height / 2, matrix, false);

    // Create a new bitmap with same width but taller to fit reflection
    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    // Create a new Canvas with the bitmap that's big enough for
    // the image plus gap plus reflection
    Canvas canvas = new Canvas(bitmapWithReflection);
    // Draw in the original image
    canvas.drawBitmap(originalImage, 0, 0, null);
    // Draw in the gap
    Paint deafaultPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
    // Draw in the reflection
    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    // Create a shader that is a linear gradient that covers the reflection
    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    // Set the paint to use this shader (linear gradient)
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

删除或忽略像 createReflectedImage() 方法这样的额外代码。

于 2013-10-01T09:57:14.380 回答
0

使用https://github.com/nostra13/Android-Universal-Image-Loader以获得更好的性能。这个图像加载器将完成您所需要的一切。

于 2013-10-01T10:02:45.747 回答