2012-12-03 19 views
0

我試圖在運行時設置ImageButton的圖像。它應該填充屏幕的寬度並使用盡可能多的高度空間,因爲它需要正確縮放。ImageButton LayoutProblem with setImageDrawable

爲遵循的ImageButton的聲明:

<ImageButton 
     android:id="@+id/map_plan_topview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true" 
     android:onClick="onMapImageClicked" 
     android:background="#0000" 
     android:scaleType="fitXY" /> 

現在我正在嘗試設置與圖像:

 ImageButton topView = (ImageButton) findViewById(R.id.map_plan_topview); 
     Drawable d = Drawable.createFromPath(path); 
     topView.setImageDrawable(d); 

的圖像寬度進行縮放FILL_PARENT,但不正確地拉伸高度。 如果我用 topView.setImageResource(R.drawable.button_where_citymap);來代替,一切正常,但我確實想在運行時從文件路徑設置源。

我在做什麼錯?

回答

0

fitXY延伸圖像不尊重比例。如果比例關係,然後用center_crop,或center_inside

您可以在代碼中設置ScaleType還有:

ImageButton topView = (ImageButton) findViewById(R.id.map_plan_topview); 
Drawable d = Drawable.createFromPath(path); 
topView.setScaleType(ScaleType.CENTER_CROP); 
topView.setImageDrawable(d); 
+0

我認爲adjustViewBounds保留了圖像的aspectRatio。然而,當我用CENTER_CROP和CENTER_INSIDE嘗試它時,它在使用setImageRessource時都可以工作,但是當我使用setImageDrawable時它不會這麼做。 CENTER_CROP不顯示整個圖像(雖然它是正確縮放的),CENTER_INSIDE顯示整個圖像,但不填充父母的寬度。 – sebay

0

你應該代替 「setImage Drawable」 使用 「SetImageResource」 ...!