3

I have a question regarding the process of resizing an ImageView Have on orientation changes. I have to place a bitmap on the top of an image view. (Basically a marker placed by the user when touching it). On orientation change, I store the relatives position of the marker and intent to restaure it when the image is re-displayed.

for now I do it this way :

on restauration I call this :

handler.post(setCollectedAnswersRunnable);

refering to this :

Runnable setCollectedAnswersRunnable = new Runnable() {
 @Override
 public void run() {
  if (iv.getMeasuredHeight() != 0) {

   int x = Math.round(coordinateCollectedAnswer.x * iv.getMeasuredWidth() / (float) question.getImage().getWidth());
   int y = Math.round(coordinateCollectedAnswer.y * iv.getMeasuredHeight() / (float) question.getImage().getHeight());

   selectedX = x;
   selectedY = y;

   addPointerImageView(x, y);

  } else {
   handler.postDelayed(this, 330);
  }
 }
};

The important thing to note is this works very well when I go from a larger ImageView to a smaller one ie when passing from landscape to portrait.

But in the other way, it doesn't. The ImageView seems to be restaured first with the same size and then scaling up witch mess my marker up. So I certainely could check if it's the same size as in portrait in addition to the !=0 condition in my runnable. but first I'm not very proud of that way to do it, I'd rather be notified when the ImageView get it's final sizing. And I would prefer not passing the viewsize in my state bundle.

Is there a simpler way to achieve this ?


By the way I load the ImageView this way :

Picasso.with(iv.getContext())
                .load(AppConstants.SERVER_URL + "/api/" + question.getImage().getUrl())
                .config(Bitmap.Config.RGB_565)
                .into(new Target() {
                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                        iv.setImageBitmap(bitmap);
                        iv.setAdjustViewBounds(true);
                    }

                    @Override
                    public void onBitmapFailed(Drawable errorDrawable) {

                    }

                    @Override
                    public void onPrepareLoad(Drawable placeHolderDrawable) {

                    }
                });
Renaud Favier
  • 1,645
  • 1
  • 17
  • 33

1 Answers1

1

I´ve got a SurfaceView that keeps to be square. i´ve notice thatn when android runtime is doing all the stuff for layout will measure the component again and again and you never can tell when is the last time and the number of executions does not seem to be related to the amount of components on the screen, it is not random but maybe has something to do with the layout constraints of all the stuff around.

here´s my class overriding the measure ion order to keep square:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int w = MeasureSpec.getSize(widthMeasureSpec);
    int h = MeasureSpec.getSize(heightMeasureSpec);
    int size = w;
    if (w < h) {
        setMeasuredDimension(widthMeasureSpec, widthMeasureSpec);
    } else {
        size = h;
        setMeasuredDimension(heightMeasureSpec, heightMeasureSpec);
    }
    if (h > 0 && squareSurfaceViewListener != null) {
        squareSurfaceViewListener.onChangeSize(size);
    }
}

So I´d inherit the imageview, detect the change and reload the image.

Using picasso it´ll cache the stuff but, maybe, you could forget about that and call for a new load each time the component is measured or add some kind of observer in the middle.

eduyayo
  • 2,020
  • 2
  • 15
  • 35
  • in my case I think the imageView pass by 3 size. First 0, then it tries the size it previoulsy have, limited by the size of the container (which would explain why it works when passing from landscape to portrait) then ajust view bounds comes into play and it gets it final size. Would you think it's more than that? I think I'll try to compare the imageview size with its container's, and see if it matches. I have to go to eat I come back in the afternoon – Renaud Favier Jan 15 '16 at 11:34