簡體   English   中英

ImageView比例和寬度/高度變化

[英]ImageView Scale and Width/Height change

我試圖在Xamarin / Android項目中使用TwoDScrollView的C#端口。

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

問題是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,但這很糟糕而且很慢)。

謝謝。

根據此答案,您應該能夠像這樣設置ImageView對象的尺寸

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

其中image_view是對TwoDScrollView要查找尺寸的同一ImageView實例的引用。

假設您的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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM