2012-03-02 30 views
1

我已經搜索EVERYWHERE爲這個答案。自定義Drawables與android

我想了解如何繪製一個自定義形狀在xml中製作成位圖或畫布。

這是我的矩形簡單的xml代碼。

<?xml version="1.0" encoding="UTF-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid android:color="#f0f000" /> 
    <stroke android:width="3dp" android:color="#ff0000" /> 
    </shape> 

然後在這裏,我試圖簡單地獲取形狀並將其放在位圖上,然後將該位圖繪製到畫布上?

myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.shape); 

    canvas.drawBitmap(myBitmap, canvas.getHeight() /2, canvas.getWidth()/2, p); 

如果有人能幫助,我會非常感激。

回答

2

這是一個繪製 ...試試這個:

Drawable drawable = getResources().getDrawable(R.drawable.shape); 
drawable.setBounds(0,0, canvas.getWidth(), canvas.getHeight()); 
drawable.draw(canvas); 

NB:很明顯,你可以做getDrawable()位只有一次,而不是在每一個平局()。