2013-07-17 39 views
1

我想創建一個XML Drawable,我將使用它代替OSMDroid中的默認標記。
這是它應該是什麼樣子到底:XML Drawable由兩個圖像組成

custom marker

的黑色部分將在創建標記,因爲每個標記都會有不同的形象出現加載。

我試圖創建一個XML繪製對象(這些都會從數據庫中,如果that's重要的加載),它還挺作品,但黑色部分好像縮放到適合的圖像,從而使標記只是一個黑色的大廣場。 (沒有黑色部分,它工作正常)

我目前的XML:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 

    <item android:drawable="@drawable/marker"/>   // main 

    <item android:drawable="@drawable/markerfill" /> // black part 

</layer-list> 

我試圖用一個規模爲第二個項目,但我看着秤,它看起來像我不能用秤爲了這。

我想要什麼:
我加載黑匣子進入「主」的一部分,在必要時調整其大小(同時保持比例),並從Java的代碼更改。 我將使用OSMDroid的這個標記。

什麼是最好的方法呢?

回答

2

What I want: I load the black box into the "main" part, resize it if necessary (while keeping the proportions) and change it from Java-Code. I will be using this marker for OSMDroid.

如果第一層的圖像將不會被調整大小/縮放使用時(I不知道OSMDroid如何管理該標記),那麼你可以使用目前的方法LayerDrawable和「放置」動態圖像與LayerDrawable.setLayerInset()

public Drawable makeBasicMarker(Bitmap bitmap) { 
     Drawable[] layers = new Drawable[2]; 
     layers[0] = new BitmapDrawable(getResources(), 
       BitmapFactory.decodeResource(getResources(), R.drawable.marker)); 
     layers[1] = new BitmapDrawable(getResources(), bitmap); 
     LayerDrawable ld = new LayerDrawable(layers); 
     ld.setLayerInset(1, xx, xx, xx, xx); // xx would be the values needed so bitmap ends in the upper part of the image 
     return ld; 
    } 

如果縮放圖像,則需要製作自己的Drawable並自己繪製零件,以便適當放置它們。

+0

謝謝,那正是我想要的 –

0

試試這樣:1.用第一個drawable定義一個視圖; 2.使用drawBitmap()方法繪製第二個項目3.調用postInvalidate()方法重繪。

+0

你能給我一個例子/鏈接怎麼做2? –

0

繪製毫巴上MBG,參照那些:

空隙的onDraw(最終帆布帆布){

super.onDraw(canvas); 

float zoomX = mWidth/(float) mBG.getWidth(); 
float zoomY = mHeight/(float) mBG.getHeight(); 

canvas.drawBitmap(mBG, 0, 0, null); 

canvas.drawBitmap(mBG, new Rect(0, 0, mBG.getWidth(), mBG.getHeight()), 
    new RectF(0, 0, mWidth, mHeight), null); 

.... 
canvas.drawBitmap(mBar, new Rect(0, 0, (int) w, (int) h), new RectF(
    X_LOCATION * zoomX, Y_LOCATION * zoomY, (X_LOCATION + w) 
     * zoomX, (Y_LOCATION + h) * zoomY), null); 

} 
+0

看起來不錯,但我把它放在哪裏? (整個onDraw)在繪製地圖的類中? (=使用標記的地方) –

+0

把它放在mapviewer類 – hopetribe

+0

我把它放到我的地圖顯示的Activity中,但是我得到這個錯誤:「onDraw(Canvas)的方法未定義類型Activity」 –