2013-10-01 27 views
0

我有一個包含超過15個位圖的ArrayList。這個位圖必須從SD卡讀取。我的數組在循環中,所以我不想每次都從存儲位置讀取位圖,並且我想將此位圖保存爲緩存。我的位圖數組列表小於2 Mb。 我想用自定義視圖中的畫布顯示它。因此,我認爲將它保存爲緩存或其他因爲位圖數量而不錯。我認爲從SD卡讀取超過15位的位圖是沒有意義的,即使它們很輕(約15kb),請幫助我如何做到這一點。另外每個位圖中的 是256 * 256像素。下面類在android中存儲位圖的數組列表

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(); 

     } 
} 
+0

和你的問題是? (另外,顯然你有19張圖片,而不是15張)。 – njzk2

+0

另外,8888(32位)中的256x256是250Kb,這使得你的數組〜4Mb – njzk2

+0

它已經用jpeg壓縮並且使它成爲10-15Kb。 [鏈接] http://khm0.google.com/kh/v=132&hl=EN&x=0&y=0&z=0&s=並且我還寫了比「15」更多的內容。 –

回答

0

使用從SD卡獲取圖像。它將保持在緩存中的圖像。您也可以從網址以及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()方法這樣的額外代碼。