2011-12-16 21 views
-2

我想創建一個視圖(LinearLayout),將填充整個屏幕,然後加載一些自定義背景圖像(bmp,png,jpg)。在運行期間,我想查看像素和某些顏色的座標。由於有許多不同的設備,我必須支持不同的分辨率。我的解決方案是有一個背景圖像,並讓它縮放到適當的屏幕大小。問題是,雖然圖像應該填滿整個屏幕,但背景位圖會縮放爲假大小。這可以防止我檢查位圖中的像素顏色,因爲我的座標從位圖返回假像素。關於查看,背景,位圖和縮放

我有以下佈局的xml:

<LinearLayout 
    android:id="@+id/viewWithBackground" 
    android:visibility="invisible" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
</LinearLayout> 

在res /繪製我有background_320x480.png。這個圖像應該縮放到屏幕尺寸。

在我執行下列活動的onCreate事件:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 
    setContentView(R.layout.activity_main); 
    viewWithBackground = findViewById(R.id.viewWithbackground); 





//the problem is that the width and height of the bitmap are not correct. Following log entries show sizes 
    Log.d(TAG, "viewWithBackground - width: " + viewWithBackground.getWidth() + ", height: " + viewWithBackground.getHeight()); 
    Log.d(TAG, "viewWithBackground.getBackground() - intrinsicWidth: " + viewWithBackground.getBackground().getIntrinsicWidth() + " intrinsicHeight: " + viewWithBackground.getBackground().getIntrinsicHeight()); 
    Log.d(TAG, "((BitmapDrawable)viewWithBackground.getBackground()).getBitmap() - width: " + ((BitmapDrawable)viewWithBackground.getBackground()).getBitmap().getWidth() + ", height:" + ((BitmapDrawable)viewWithBackground.getBackground()).getBitmap().getHeight()); 
} 

在示例:設備的顯示尺寸是480×800。 res/drawable中的圖像是32x x 480,Bitmap和BitmapDrawable是480x720。它沒有任何意義。

我的問題:如何獲取與屏幕上顯示的像素大小相同的背景位圖?

回答

1
  1. 如果設備顯示爲480×800和你不使用你的應用程序窗口的全屏主題高度將是800 - 通知欄的高度
  2. 繪製對象類有一個getBounds方法巫婆返回矩形其中內容繪製將被繪製
+0

1. \t我使用整個屏幕。沒有通知欄可見。 2. \t getBounds()方法並不重要。即使在屏幕上正確呈現視圖底層的背景位圖大小也是錯誤的。因此我無法在特定座標處探測像素顏色。 – Anderson 2011-12-16 11:24:09