2012-09-26 37 views
0

在Android上正確處理多個屏幕尺寸的問題已經有數千次討論過。但是我找不到解決問題的辦法。簡而言之,我需要將自定義進度條與imageView對齊。我有3套imageView - ldpi(240x400),mdpi(320x480),hdpi(480x800)。我對齊的Java自定義查看下面的代碼:然而將視圖與不同分辨率的圖像對齊

 //get screen density 
     float density = getBaseContext().getResources().getDisplayMetrics().density; 

     //set the progress bar position according to screen density 
     if (density == 1.0f) 
     { 
      ImageView micImage = ((ImageView) findViewById(R.id.imageViewClk)); 
      Drawable drawing = micImage.getDrawable(); 
      Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap(); 

      // Get current dimensions 
      int width = bitmap.getWidth(); 
      int height = bitmap.getHeight(); 

      LayoutParams params = new LayoutParams((int)(height/13.94), (int)(height/13.94)); 
      params.setMargins((int)(width/2.30), 0, 0, (int)(height/2.75)); 

      params.addRule(RelativeLayout.ALIGN_LEFT,R.id.imageViewClk); 
      params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.imageViewClk); 
      myCustomTwistedProgressBar.setLayoutParams(params); 
     }else if (density == 1.5f){ 
      ImageView micImage = ((ImageView) findViewById(R.id.imageViewClk)); 
      Drawable drawing = micImage.getDrawable(); 
      Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap(); 

      int width = bitmap.getWidth(); 
      int height = bitmap.getHeight(); 

      LayoutParams params = new LayoutParams((int)Math.round(height/14.13), (int)Math.round(height/14.13)); 
      params.setMargins((int)Math.round(width/2.27), 0, 0, (int)Math.round(height/2.91)); 

      params.addRule(RelativeLayout.ALIGN_LEFT,R.id.imageViewClk); 
      params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.imageViewClk); 
      myCustomTwistedProgressBar.setLayoutParams(params); 
     }else if (density == 0.75f){ 
      ImageView micImage = ((ImageView) findViewById(R.id.imageViewClk)); 
      Drawable drawing = micImage.getDrawable(); 
      Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap(); 

      // Get current dimensions 
      int width = bitmap.getWidth(); 
      int height = bitmap.getHeight(); 

      LayoutParams params = new LayoutParams((int)(height/14.88), (int)(height/14.88)); 
      params.setMargins((int)(width/2.27), 0, 0, (int)(height/2.69)); 

      params.addRule(RelativeLayout.ALIGN_LEFT,R.id.imageViewClk); 
      params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.imageViewClk); 
      myCustomTwistedProgressBar.setLayoutParams(params); 
     } 

一切工作罰款不同的屏幕尺寸上,當我試圖檢查480X854分辨率自定義視圖的垂直對齊方式是不正確的。在相同的屏幕尺寸上使用480x800進行檢查,並再次正常工作。我比去大跳躍和檢查GalaxyTab和水平和垂直路線是錯誤的。現在我的第一個雖然是位圖的寬度和高度是圖像的不是實際調整大小的圖像視圖。所以我花了很多時間試圖獲得imageview的實際大小,甚至花費了viewTreeObserver,但結果都是相同的 - 正確的,不變的(未縮放的?)位圖大小。所以積極的是,問題不在這裏,我無法進一步解決。有沒有人有一個想法,爲什麼對齊無法正常工作?

PS:作爲在佈局XML文件中的圖像視圖我有2個配置爲長和notlong但這圖像具有在兩個相同的描述:

<ImageView 
android:src="@drawable/cloking" 
android:id="@+id/imageViewClk" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:layout_centerHorizontal="true" 
android:layout_above="@+id/imageViewProcess" 
android:adjustViewBounds="true" 
android:cropToPadding="false" 
android:layout_marginTop="60dp" 
android:scaleType="fitXY"> 
</ImageView> 

回答

0

Android將縮放圖像,但它將保持方面比例爲圖像。您無法通過佈局設置控制寬高比(據我所知)。我會通過選擇我想支持的幾個屏幕比例並製作更少的資源(支持縱橫比的圖像)來解決該問題。代碼如下所示:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth(); 
int height = display.getHeight(); 

ImageView image = (ImageView) findViewById(R.id.imageViewClk); 
if(width/height == aspectRatio1) 
{ 
    image.setImageResource(R.id.imageAspect1); 
} else if(width/height == aspectRatio2... 
+0

但是保持寬高比正是我所需要的!假設圖像爲237x424(hdpi版本),我知道我的自定義項目應該放在我的圖像上(您在代碼中看到計算結果)。然而,對於800而言,這是正確的,但對於854來說是正確的。我看到兩種可能性:要麼調整圖像大小,要麼以像素爲單位獲得「真實」大小(儘管我裁定只有一個但不是100% ),或圖像沒有調整大小(不需要因爲它適合和dpi資源是正確的),但正確計算的定位像素在某種程度上不正確地用於佈局中... –