2013-12-20 125 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