Force Android to calculate measuredWidth, measuredHeight

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Force Android to calculate measuredWidth, measuredHeight

Postby KarlKarlsom » Sun Jan 24, 2010 11:01 pm

Hallo,
I'm stuck in a Problem. Hope somebody can help.

I have a ImageView in a LinearLayout.
The ImageView has the LayoutParams .FILL_PARENT for width and Height.
The ImageView is created during Runtime and added to the Layout.
All this is done in the OnCreat Function of my Activity.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public void onCreate(Bundle savedInstanceState)
  3.  
  4. {
  5.  
  6. super.onCreate(savedInstanceState);
  7.  
  8. setContentView(R.layout.main); 
  9.  
  10. PCVTest = new ImageView(this);
  11.  
  12. LinearLayout LL= (LinearLayout) findViewById(R.id.LinearLayout1);
  13.  
  14. LinearLayout.LayoutParams LLP = new LinearLayout.LayoutParams(
  15.  
  16.      LinearLayout.LayoutParams.FILL_PARENT,
  17.  
  18.      LinearLayout.LayoutParams.FILL_PARENT);
  19.  
  20. LL.addView(PCVTest, LLP);
  21.  
  22. PCVTest.setImageResource(R.drawable.test);
  23.  
  24. //my Problem comes here
  25.  
  26. }
  27.  
  28.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


So here is my problem:
In the OnCreat function I want to get the height and the width of the imageview.
But PCVTest.getMeasuredHeight(); returns 0 as long as it is called from the OnCreate function.
When I call PCVTest.getMeasuredHeight(); later from another function a correct Value is returned.
In the debugger I get the same Result.
The Values mMeasuredWidth and mMeasuredHeight of the ImageView are set after the OnCreate of the activity finished.

So how can I force Android to calculate those values in the OnCreat function?

Anybody has a clue?

For every help I would be very thankfull.

Thanks in advance.
KarlKarlsom
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Mon Dec 14, 2009 8:40 pm

Top

Solution

Postby KarlKarlsom » Mon Jan 25, 2010 6:15 pm

Hallo,
I found a solution by myselfe.

I don't call the calculation in the OnCreate function of the activity.
I override the OnDraw of the ImageView and do the Calculation there.
When OnDraw is called mMeasuredWith is allready calculated.
KarlKarlsom
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Mon Dec 14, 2009 8:40 pm

OnDraw

Postby KarlKarlsom » Mon Jan 25, 2010 6:58 pm

Ok,
I discovered that my app is now running very slowly.
This is because OnDraw is invoked too often. And every time the Calculation is done.
Therefor I added a boolean to the class wich is set to false after the first time OnDraw.
The result is now this.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         public void onDraw(Canvas C)
  3.  
  4.         {
  5.  
  6.                 super.onDraw(C);
  7.  
  8.                 if (!mIsInitialized)
  9.  
  10.                 {
  11.  
  12.                         //Calculation comes here
  13.  
  14.                        mIsInitialized=true;
  15.  
  16.                 }
  17.  
  18.         }
  19.  
  20.        
  21.  
  22.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

Works fine for me.
KarlKarlsom
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Mon Dec 14, 2009 8:40 pm

Re: Solution

Postby pskink » Mon Jan 25, 2010 9:02 pm

KarlKarlsom wrote:Hallo,
I found a solution by myselfe.

I don't call the calculation in the OnCreate function of the activity.
I override the OnDraw of the ImageView and do the Calculation there.
When OnDraw is called mMeasuredWith is allready calculated.


try to override onMeasure

pskink
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 9 guests