1

I am trying to use a C# port of TwoDScrollView in a Xamarin/Android project.

It works fine. We are adding scale gesture (we are showing an image that can be zoomed in/out by the user). That works fine as well.

The issue is that TwoDScrollView relies on ImageView.Width and ImageView.Height to make its calculation and that does NOT change - causing problems when trying to scroll after scaling. On resizing we are doing:

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

We can see how the layoutparameter width and height change, but the View.Width and View.Height never change... it is always the dimensions of the original image we loaded. How can we make that update? (one solution is to scale the bitmap and assign it to the imageview, but that's lousy and slow).

thanks.

rufo
  • 5,158
  • 2
  • 36
  • 47

2 Answers2

3

According to this answer you should be able to set the dimensions of an ImageView object like this

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

where image_view is a reference to the same ImageView instance that your TwoDScrollView is looking to for dimensions.

Community
  • 1
  • 1
norlesh
  • 1,749
  • 11
  • 23
  • That's (almost) what I have in the question - it doesn't change the ImageView.Width (or Height), but the LayoutParameters, and that's not good. – rufo Dec 31 '13 at 14:19
  • my mistake for some reason I mistook lp for a reference to this ImageView thats causing your problmes. I edited the variable name in my answer. From reading your question it still seems like the solution is to update the width and height as soon as the new dimensions are available and before TwoDScrollView makes use of the values. – norlesh Dec 31 '13 at 15:27
2

Assuming your fullscreenImageView extends ImageView you can add this method in your 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);
}
Akshat Agarwal
  • 2,837
  • 5
  • 29
  • 49