2013-12-20 157 views
1

我想在Xamarin/Android項目中使用C#端口TwoDScrollViewImageView比例和寬度/高度變化

它工作正常。我們正在添加縮放手勢(我們正在顯示可由用戶放大/縮小的圖像)。這工作也很好。

問題是,TwoDScrollView依賴於ImageView.Width和ImageView.Height來進行計算,並且不會更改 - 在縮放後嘗試滾動時會導致問題。在調整,我們正在做的:

fullscreenImageView.ScaleX = _scaleFactor; 
fullscreenImageView.ScaleY = _scaleFactor; 
var lp = fullscreenImageView.LayoutParameters; 

lp.Width = (int) (fullscreenImageView.Drawable.IntrinsicWidth * _scaleFactor); 
lp.Height = (int) (fullscreenImageView.Drawable.IntrinsicHeight * _scaleFactor); 

fullscreenImageView.RequestLayout(); //the request layout was just a test, 
             // it doesn't work either way 

我們可以看到如何layoutparameter寬度和高度的變化,但View.Width和View.Height永不改變......它始終是我們加載的原始圖像的尺寸。我們如何才能做出更新? (一種解決方案是縮放位圖並將其分配給imageview,但這很糟糕並且速度很慢)。

謝謝。

回答

2

假設你fullscreenImageView延伸ImageView您可以添加這種方法,您fullscreenImageView

@Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    int width = MeasureSpec.getSize(widthMeasureSpec); 
    int height = width * getDrawable().getIntrinsicHeight()/getDrawable().getIntrinsicWidth(); 
    setMeasuredDimension(width, height); 
} 
+0

這實際上改變了大小。謝謝。 – rufo

3

根據this answer你應該能夠設置一個ImageView的對象的尺寸這樣

image_view.getLayoutParams().width = (int) (fullscreenImageView.Drawable.IntrinsicWidth * _scaleFactor); 
image_view.getLayoutParams().height = (int) (fullscreenImageView.Drawable.IntrinsicHeight * _scaleFactor); 

其中image_view是相同的ImageView實例的引用您TwoDScrollView正在尋求的尺寸。

+0

這是(幾乎)我有這個問題 - 它不會改變ImageView.Width(或高),但LayoutParameters,這並不好。 – rufo

+0

我的錯誤由於某種原因,我錯誤的lp參考這ImageView多數民衆贊成在導致你的問題。我在我的答案中編輯了變量名稱。從閱讀你的問題看來,解決方案似乎是在新維度可用時以及TwoDScrollView使用這些值之前立即更新寬度和高度。 – norlesh

相關問題