2011-11-16 18 views
1

因此,在嘗試將我的圖像正確拉伸到寬度後出現大量挫折之後,我意識到我真正的問題是setImageResource與setImageDrawable(我嘗試使用)的行爲不同。下面的XML給了我完美的寬度,縱橫比拉伸的圖像與setImageResource但setImageDrawable圖像沒有拉伸到寬度,我應該如何處理這個問題的任何建議?我用setImageDrawable,因爲我從互聯網上獲得的替代品,形象是歡迎的。如何處理ImageView setImageResource與setImageDrawable的區別

 <ImageView 
      android:id="@+id/item_image" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:adjustViewBounds="true"/> 

哦,我tryed scaletype,一個給最好的結果是centercrop但作物這是不期望。

回答

0

一個補丁修復我發現是使用位圖作爲setImageBitmap明顯的工作原理類似setImageResource。我仍然接受其他建議。

0

你只要把XML格式的ImageView變化
的android:layout_width = 「FILL_PARENT」
機器人:layout_hight = 「FILL_PARENT」

+0

沒有工作再試一次:)我不知道是否問題可以與我的imageview是在一個scrollview內的relativelayout內相關。問題依然存在,setImageResource工作,並且setImageDrawable沒有。 – Warpzit

0

你的圖像視圖的寬度是fill_parent,所以如果你的圖像的寬度不夠,那麼它將被拉伸。如果你不想使用fill_parent只是替換寬度「wrap_content」。

<ImageView 
     android:id="@+id/item_image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true"/> 
+0

我想要它伸展,這是我的頭痛。調用setImageDrawable之前調用 – Warpzit

+0

只需從圖像中繪製一個drawable並設置其寬度。 – jainal

+0

這可能會工作,但似乎比使用位圖更多的工作,我認爲位圖將是一條路。 – Warpzit

1

這個問題確實是陳舊的,但在任何情況下都會碰到它,這個問題有沒有完全錯誤的事,但在之前的Android 18版本的壞實現決策如果你的目標你18+可能不會看到這個問題。不過我目前針對企業擁有片劑在16

的解決方案是創建的ImageView的超重寫onMeasure執行長寬比:

class MyImageView extends ImageView { 
    public MyImageView(Context context) { 
     super(context); 
    } 
    public MyImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 
    public MyImageView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     float iwidth = (float) View.MeasureSpec.getSize(widthMeasureSpec); 
     float swidth = (float) getDrawable().getIntrinsicWidth(); 
     float sheight = (float) getDrawable().getIntrinsicHeight(); 
     float iheight = sheight/swidth * iwidth; 
     setMeasuredDimension((int)iwidth, (int)iheight); 
    } 
} 

在您的佈局:

<com.mypackage.MyImageView layout_width="match_parent" etc... /> 

如果您的目標是Android 18+ adjustViewBounds應該按預期工作。

+0

針對Android 22和您的修復幫助了我。 – Richard