2011-10-25 53 views
0

我寫了一個自定義視圖,我畫自己。我需要這個,因爲我必須繪製帶有邊框的邊框,並在其中包含邊界和其中的某些內容。Android:邊界的比例寬度?

此刻我使用5px作爲圓形邊界的寬度。這在ldpi屏幕上看起來不錯,在mdpi屏幕上很好,我認爲它在hdpi屏幕上看起來很糟糕。

我該如何編寫視圖來縮放不同屏幕的邊界?目前,自定義視圖獲得了以像素爲單位設置寬度的屬性。

如何在繪圖過程中使用其他尺寸?

回答

2

想想DP,而不是像素,並獲得像素使用下面的代碼對應,DP值:如果定義在一個XML資源文件的尺寸,像這樣的

/** 
* Convert a dimension in dip to px 
* @param dip 
* @param context 
* @return px 
*/ 
public static int dipToPx(int dip, Context context) { 
    return (int) (dip * context.getResources().getDisplayMetrics().density); 
} 

或者:

<resources> 
    <dimen name="common_padding_large">20dp</dimen> 
</resources> 

可以獲取該維度的像素值與

getContext().getResources().getDimension(R.dimen.common_padding_large); 
+0

我認爲這會奏效 - 謝謝! – schlingel

1

你應該看看使用dp或密度獨立像素。 This page詳細介紹瞭如何支持不同的屏幕。

+0

謝謝,我知道這個鏈接,但內華達州呃在文檔的末尾看到了「將dp單元轉換爲像素單元」部分。 – schlingel