2013-09-27 48 views
0

我在顯示位圖時遇到了問題。我有一個從資源加載並放置在地圖上的位圖(標記),我從服務器下載具有特定尺寸的位圖(頭像)。然後我將頭像放置在標記的中心。結果是,在一部手機上,每件事都能正常工作,但在其他方面,這個頭像比標記更大。調整位圖大小以適應其他位圖

我該怎麼辦?

+0

你需要合併兩個位圖,然後把那個單位圖作爲標記 –

回答

0

做這樣希望這有助於你

googleMap.addMarker(new MarkerOptions().title(markerData.get(USER_NAME)) 
          .icon(BitmapDescriptorFactory.fromBitmap(combineImages(BitmapFactory.decodeResource(getResources(), R.drawable.your_pin_marker_from_resorece), bitmapAvatar))) 
          .position(new LatLng(Double.parseDouble(markerData.get(USER_LAT)), Double.parseDouble(markerData.get(USER_LONG))))); 

Combile圖片

public Bitmap combineImages(Bitmap frame, Bitmap image) { 
     Bitmap bitmapCreate = null; 
      Bitmap bitmapScale = null; 
     bitmapScale = Bitmap.createScaledBitmap(image, convertSizeToDeviceDependent(45), convertSizeToDeviceDependent(40), true); 

     bitmapCreate = Bitmap.createBitmap(frame.getWidth(), frame.getHeight(), Bitmap.Config.ARGB_8888); 

     Canvas comboImage = new Canvas(bitmapCreate); 

     comboImage.drawBitmap(bitmapScale, convertSizeToDeviceDependent(7), convertSizeToDeviceDependent(7), null); 
     comboImage.drawBitmap(frame, 0, 0, null); 
     if (frame != null) { 
      try { 
       bitmapScale.recycle(); 
       frame.recycle(); 
       image.recycle(); 
       bitmapScale = null; 
       frame = null; 
       image = null; 
      } catch (Throwable e) { 
      } 
     } 
     return bitmapCreate; 
    } 


public int convertSizeToDeviceDependent(int value) { 
     DisplayMetrics dm = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(dm); 
     return ((dm.densityDpi * value)/160); 
    } 
+0

你能解釋一下我的convertSizeToDeviceDependent( int值)方法? – ghost

+0

由於Android設備上有這麼多的分辨率和屏幕尺寸。所以如果我直接添加45硬編碼它不會給所有設備相同的輸出。這就是爲什麼這種方法將45作爲設備分辨率和屏幕尺寸。 –