8

I'm trying to get values from myImageView.getImageMatrix() method once my activity is ready. I tried using the onCreate() , onStart() , onResume() methods but the matrix I get is the default.

If I call myImageView.getImageMatrix() triggered by an OnClickListener, after my activity is visible, I get the right values.


Just to be more clear:

  • calling getImageMatrix onStart = Matrix{[1.0, 0.0, 0.0][0.0, 1.0, 0.0][0.0, 0.0, 1.0]}

  • calling getImageMatrix onClick = Matrix{[0.77488154, 0.0, 7.6717987][0.0, 0.77488154, 0.0][0.0, 0.0, 1.0]}

Shlomi Schwartz
  • 8,693
  • 29
  • 109
  • 186
  • The problem is that layout gets not measured and rendered until onResume() finished. Check this question to see how you could get notified when this happens: http://stackoverflow.com/questions/7750102/how-to-get-height-and-width-of-button – Flo Jan 05 '12 at 10:41
  • Thanks for that, the onWindowFocusChanged is called once the data is there, however it is called on each focus change. I could add a flag that will indicate init state but it does not seem so smooth. I think this is my solution for now, any thoughts? – Shlomi Schwartz Jan 05 '12 at 10:59

4 Answers4

18

You can also try this method:

ImageView myImageView = (ImageView) findViewById(R.id.myImageView);
ViewTreeObserver vto = myImageView.getViewTreeObserver();      
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {          
     @Override          
     public void onGlobalLayout() {              
        // do something now when the object is loaded 
        // e.g. find the real size of it etc          
        myImageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);        
     }      
});  
Lumis
  • 21,517
  • 8
  • 63
  • 67
  • removeGlobalOnLayoutListener() is now deprecated, starting with SDK version 16 (Jelly Bean) removeOnGlobalLayoutListener() should be used. – flo May 17 '15 at 18:58
4

I am not 100% but I needed to know something similar for my development and I found out that onWindowFocusChanged() is called when the view is loaded. I am not sure if that fills your needs or not.

MikeIsrael
  • 2,871
  • 2
  • 22
  • 34
  • Thanks for that, the onWindowFocusChanged is called once the data is there, however it is called on each focus change. I could add a flag that will indicate init state but it does not seem so smooth. I think this is my solution for now, any thoughts? – Shlomi Schwartz Jan 05 '12 at 11:02
  • oh yeah sorry forgot to add that. I have a global boolean I use to make sure it is the first call of focus changed and then do what I need to do. I guess it depends on what you need to do, but if you just need to access something the first time the screen is loaded then a global variable should fix it, if not the most elegant code. – MikeIsrael Jan 05 '12 at 11:08
  • Sorry man, I think both answers are great and I wish I can choose them both (Can I do it?). But I think the other solution is smoother. Anyway I really appreciate your answer! – Shlomi Schwartz Jan 08 '12 at 12:54
  • no don't think so, didn't notice you had another accepted answer. I think it is best if you accept the one you implemented so if the other answer works for you, that is the one that should be accepted. Was just worried that there was an issue with my answer, and if so wanted to know for my own code as well :) – MikeIsrael Jan 08 '12 at 13:05
1

Maybe you can use an onLayoutChangeListener but I am not sure.

C.d.
  • 9,932
  • 6
  • 41
  • 51
0

Perhaps you could use an AsyncTask (http://developer.android.com/reference/android/os/AsyncTask.html) that monitors whether the ImageView has loaded (maybe does not equal null).

You could display a progess dialog in onProgressUpdate and then continue your code from onPostExecute.

Tony
  • 2,335
  • 21
  • 25