2011-06-21 30 views
0

我的地圖pin大小有一個奇怪的問題。爲了保持動態性,不同類別的地圖引腳存儲在網站的服務器上,以便即使在應用發佈後,也可以在任何時候更改它們。Android - 地圖標記以不同的方式顯示(dip/px或解壓縮)

我每次下載它時都會緩存這些針腳,而且如果服務器發回了一點信息,說明自上次下載以來發生了變化,我只會重新下載它們。第一次抓住引腳時,我使用位圖,然後將其保存到文件中,並且地圖標記尺寸正確。每次之後,我都會直接從映像文件加載保存的引腳版本。與使用第一次下載的位圖相比,這些顯示器的顯示效果要大得多較小的。起初,我認爲這是我保存PNG的方式的一個問題,但它們的尺寸是正確的(64 x 64)。這是dip/px問題,還是需要用某種選項來解壓縮圖像文件?

以下是我搶了圖像的第一次:

public static Bitmap loadMapPin(String category, int width, int height) { 
     URL imageUrl; 
     category = category.toLowerCase().replace(" ", ""); 
     try { 
      imageUrl = new URL(PIN_URL+category+".png"); 
      InputStream is = (InputStream) imageUrl.getContent(); 
      Options options = new Options(); 

      options.inJustDecodeBounds = true; //Only find the dimensions 

      //Decode without downloading to find dimensions 
      BitmapFactory.decodeStream(is, null, options); 

      boolean scaleByHeight = Math.abs(options.outHeight - height) >= Math.abs(options.outWidth - width); 
      if(options.outHeight * options.outWidth >= width * height){ 
       // Load, scaling to smallest power of 2 that'll get it <= desired dimensions 
       double sampleSize = scaleByHeight 
        ? options.outHeight/height 
        : options.outWidth/width; 
       options.inSampleSize = 
        (int)Math.pow(2d, Math.floor(
        Math.log(sampleSize)/Math.log(2d))); 
      } 
      options.inJustDecodeBounds = false; //Download image this time 

      is.close(); 
      is = (InputStream) imageUrl.getContent(); 
      Bitmap img = BitmapFactory.decodeStream(is, null, options); 
      return img; 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

而這裏的我如何加載它們從高速緩存的文件:

BitmapFactory.decodeFile(filepath); 

提前感謝!

回答

0

我發現,默認情況下,將圖像解壓縮爲位圖不會在高密度屏幕上進行縮放。您必須將密度設置爲無。換句話說,你指定圖像是爲了未知的密度。

解決方案:

Bitmap b = BitmapFactory.decodeFile(filepath); 
b.setDensity(Bitmap.DENSITY_NONE);