Yes, there are methods for measuring and positioning views. They are called onMeasure and onLayout, respectively...
2012/9/27 Serdel <[email protected]> > I passed my progress bar object to my ImageView subclass but if I set the > layout params on the progress bar object they are not accepted (it is not > displayed at all). Of course in the xml file my custom image view is under > the progress bar so it is not covering it. So even If I do get the correct > resized parameters I can't use them to position my progress bar. Is there a > method in the main UI thread or inside my ImageView subclass that I can use > for that purpose? > > > On Thursday, September 27, 2012 11:26:49 AM UTC+2, Serdel wrote: >> >> >> "onMeasure may be called multiple times, yes." >> >> but why once with 480x817(so almost full screen) and 2nd time with >> 227x481 (the actual size that I want). Moreover I have noticed that it is >> called multiple times but always with this pair - first with almost >> fullscreen, 2nd correct. How can I filter to use only the 2nd one? I don't >> know the exact values that android can scale my image to. >> >> >> W dniu czwartek, 27 września 2012 11:23:16 UTC+2 użytkownik Kostya >> Vasilyev napisał: >>> >>> You're calling findViewById() on your image view object, which looks >>> among the view's children. Since the progress bar is not a child of your >>> image view, this returns null. Look in the activity instead, or have the >>> activity provide the progress bar to the image view, or -- that's how I'd >>> do it -- create a layout class that manages both image and progress views >>> as its children. >>> >>> onMeasure may be called multiple times, yes. >>> >>> -- K >>> >>> 2012/9/27 Serdel <[email protected]> >>> >>>> Hi Kostya, >>>> >>>> I am not sure what is complicated here... I just want to place my >>>> progress bar in the exact location on my imageview. If do it 'hard coded' >>>> so i.e. the image is 200x400 and I place it 50px from left and 100px from >>>> bottom than it will be misplaced if the image is resized by android. >>>> Therefore I want to get new resized parameters so I can calculate my >>>> position dynamically not hard coded - it cannot be put more simple. I ahve >>>> now subclassed my ImageView on in the onMeasure I finally got the rescaled >>>> size. However inside that method I get null when I call: >>>> >>>> myCustomTwistedProgressBar = ((CustomTwistedProgressBar) >>>> findViewById(R.id.progressBar)**) >>>> >>>> Moreover I also need to get the screen density before placing my progress >>>> bar (I have different image sizes for different densities). So I am still >>>> stuck. I don't understand why is it soooooo difficult to get the rescaled >>>> dimensions of an imageView in android. >>>> >>>> >>>> Also one very strange thing is that my onMeasure is called a couple of >>>> times when the activity starts - first it seems that the image is spread >>>> (width and height are full screen values) and the 2nd time they are >>>> correct. >>>> >>>> >>>> W dniu środa, 26 września 2012 23:46:37 UTC+2 użytkownik Kostya >>>> Vasilyev napisał: >>>>> >>>>> Not sure what your requirements are, but the code below seems way too >>>>> complicated to me. >>>>> >>>>> Subclass ViewGroup, draw the image in onDraw, override onMeasure / >>>>> onLayout and position the progress bar (or skip the drawing and add an >>>>> image view as a child... you'd need to position it too...) >>>>> >>>>> -- K >>>>> >>>>> 2012/9/26 Serdel <[email protected]> >>>>> >>>>>> The problem with properly handling multiple screen sizes on Android >>>>>> has been talked all over thousands of times. However I couldn't find a >>>>>> solution to m problem. In a nutshell I need to align my custom progress >>>>>> bar >>>>>> over an imageView. I've got 3 set of drawables for the imageView - >>>>>> ldpi(scaled for 240x400), mdpi(scaled for 320x480), hdpi(scaled for >>>>>> 480x800). I align my custom view in Java with the following code: >>>>>> >>>>>> //get screen density >>>>>> float density = >>>>>> getBaseContext().getResources(****).getDisplayMetrics().density; >>>>>> >>>>>> >>>>>> //set the progress bar position according to screen density >>>>>> if ( density == 1.0f) >>>>>> >>>>>> >>>>>> { >>>>>> ImageView micImage = ((ImageView) >>>>>> findViewById(R.id.imageViewClk****)); >>>>>> >>>>>> >>>>>> Drawable drawing = micImage.getDrawable(); >>>>>> Bitmap bitmap = ((BitmapDrawable)drawing).getB****itmap(); >>>>>> >>>>>> >>>>>> // Get current dimensions >>>>>> int width = bitmap.getWidth(); >>>>>> >>>>>> >>>>>> int height = bitmap.getHeight(); >>>>>> >>>>>> LayoutParams params = new >>>>>> LayoutParams((int)(height/13.**9**4), (int)(height/13.94)); >>>>>> >>>>>> >>>>>> params.setMargins((int)(width/****2.30), 0, 0, >>>>>> (int)(height/2.75)); >>>>>> >>>>>> >>>>>> >>>>>> params.addRule(RelativeLayout.****ALIGN_LEFT,R.id.imageViewClk); >>>>>> >>>>>> >>>>>> >>>>>> params.addRule(RelativeLayout.****ALIGN_BOTTOM,R.id.imageViewClk****); >>>>>> >>>>>> >>>>>> myCustomTwistedProgressBar.set****LayoutParams(params); >>>>>> }else if ( density == 1.5f ){ >>>>>> >>>>>> >>>>>> ImageView micImage = ((ImageView) >>>>>> findViewById(R.id.imageViewClk****)); >>>>>> >>>>>> >>>>>> Drawable drawing = micImage.getDrawable(); >>>>>> Bitmap bitmap = ((BitmapDrawable)drawing).getB****itmap(); >>>>>> >>>>>> >>>>>> int width = bitmap.getWidth(); >>>>>> int height = bitmap.getHeight(); >>>>>> >>>>>> >>>>>> LayoutParams params = new >>>>>> LayoutParams((int)Math.round(h****eight/14.13), >>>>>> (int)Math.round(height/14.13))****; >>>>>> >>>>>> >>>>>> params.setMargins((int)Math.ro****und( width/2.27), 0, 0, >>>>>> (int)Math.round(height/2.91)); >>>>>> >>>>>> >>>>>> >>>>>> params.addRule(RelativeLayout.****ALIGN_LEFT,R.id.imageViewClk); >>>>>> >>>>>> >>>>>> >>>>>> params.addRule(RelativeLayout.****ALIGN_BOTTOM,R.id.imageViewClk****); >>>>>> >>>>>> >>>>>> myCustomTwistedProgressBar.set****LayoutParams(params); >>>>>> }else if ( density == 0.75f ){ >>>>>> >>>>>> >>>>>> ImageView micImage = ((ImageView) >>>>>> findViewById(R.id.imageViewClk****)); >>>>>> >>>>>> >>>>>> Drawable drawing = micImage.getDrawable(); >>>>>> Bitmap bitmap = ((BitmapDrawable)drawing).getB****itmap(); >>>>>> >>>>>> >>>>>> // Get current dimensions >>>>>> int width = bitmap.getWidth(); >>>>>> >>>>>> >>>>>> int height = bitmap.getHeight(); >>>>>> >>>>>> LayoutParams params = new >>>>>> LayoutParams((int)(height/14.**8**8), (int)(height/14.88)); >>>>>> >>>>>> >>>>>> params.setMargins((int)(width/****2.27), 0, 0, >>>>>> (int)(height/2.69)); >>>>>> >>>>>> >>>>>> >>>>>> params.addRule(RelativeLayout.****ALIGN_LEFT,R.id.imageViewClk); >>>>>> >>>>>> >>>>>> >>>>>> params.addRule(RelativeLayout.****ALIGN_BOTTOM,R.id.imageViewClk****); >>>>>> >>>>>> >>>>>> myCustomTwistedProgressBar.set****LayoutParams(params); >>>>>> } >>>>>> >>>>>> >>>>>> The problem with properly handling multiple screen sizes on >>>>>> Android has been talked all over thousands of times. However I couldn't >>>>>> find a solution to m problem. In a nutshell I need to align my custom >>>>>> progress bar over an imageView. I've got 3 set of drawables for the >>>>>> imageView - ldpi(240x400), mdpi(320x480), hdpi(480x800). I align my >>>>>> custom >>>>>> view in Java with the following code: >>>>>> >>>>>> //get screen density >>>>>> float density = >>>>>> getBaseContext().getResources(****).getDisplayMetrics().density; >>>>>> >>>>>> >>>>>> //set the progress bar position according to screen density >>>>>> if ( density == 1.0f) >>>>>> >>>>>> >>>>>> { >>>>>> ImageView micImage = ((ImageView) >>>>>> findViewById(R.id.imageViewClk****)); >>>>>> >>>>>> >>>>>> Drawable drawing = micImage.getDrawable(); >>>>>> Bitmap bitmap = ((BitmapDrawable)drawing).getB****itmap(); >>>>>> >>>>>> >>>>>> // Get current dimensions >>>>>> int width = bitmap.getWidth(); >>>>>> >>>>>> >>>>>> int height = bitmap.getHeight(); >>>>>> >>>>>> LayoutParams params = new >>>>>> LayoutParams((int)(height/13.**9**4), (int)(height/13.94)); >>>>>> >>>>>> >>>>>> params.setMargins((int)(width/****2.30), 0, 0, >>>>>> (int)(height/2.75)); >>>>>> >>>>>> >>>>>> >>>>>> params.addRule(RelativeLayout.****ALIGN_LEFT,R.id.imageViewClk); >>>>>> >>>>>> >>>>>> >>>>>> params.addRule(RelativeLayout.****ALIGN_BOTTOM,R.id.imageViewClk****); >>>>>> >>>>>> >>>>>> myCustomTwistedProgressBar.set****LayoutParams(params); >>>>>> }else if ( density == 1.5f ){ >>>>>> >>>>>> >>>>>> ImageView micImage = ((ImageView) >>>>>> findViewById(R.id.imageViewClk****)); >>>>>> >>>>>> >>>>>> Drawable drawing = micImage.getDrawable(); >>>>>> Bitmap bitmap = ((BitmapDrawable)drawing).getB****itmap(); >>>>>> >>>>>> >>>>>> int width = bitmap.getWidth(); >>>>>> int height = bitmap.getHeight(); >>>>>> >>>>>> >>>>>> LayoutParams params = new >>>>>> LayoutParams((int)Math.round(h****eight/14.13), >>>>>> (int)Math.round(height/14.13))****; >>>>>> >>>>>> >>>>>> params.setMargins((int)Math.ro****und( width/2.27), 0, 0, >>>>>> (int)Math.round(height/2.91)); >>>>>> >>>>>> >>>>>> >>>>>> params.addRule(RelativeLayout.****ALIGN_LEFT,R.id.imageViewClk); >>>>>> >>>>>> >>>>>> >>>>>> params.addRule(RelativeLayout.****ALIGN_BOTTOM,R.id.imageViewClk****); >>>>>> >>>>>> >>>>>> myCustomTwistedProgressBar.set****LayoutParams(params); >>>>>> }else if ( density == 0.75f ){ >>>>>> >>>>>> >>>>>> ImageView micImage = ((ImageView) >>>>>> findViewById(R.id.imageViewClk****)); >>>>>> >>>>>> >>>>>> Drawable drawing = micImage.getDrawable(); >>>>>> Bitmap bitmap = ((BitmapDrawable)drawing).getB****itmap(); >>>>>> >>>>>> >>>>>> // Get current dimensions >>>>>> int width = bitmap.getWidth(); >>>>>> >>>>>> >>>>>> int height = bitmap.getHeight(); >>>>>> >>>>>> LayoutParams params = new >>>>>> LayoutParams((int)(height/14.**8**8), (int)(height/14.88)); >>>>>> >>>>>> >>>>>> params.setMargins((int)(width/****2.27), 0, 0, >>>>>> (int)(height/2.69)); >>>>>> >>>>>> >>>>>> >>>>>> params.addRule(RelativeLayout.****ALIGN_LEFT,R.id.imageViewClk); >>>>>> >>>>>> >>>>>> >>>>>> params.addRule(RelativeLayout.****ALIGN_BOTTOM,R.id.imageViewClk****); >>>>>> >>>>>> >>>>>> myCustomTwistedProgressBar.set****LayoutParams(params); >>>>>> } >>>>>> >>>>>> Everything worked fined on different screen sizes however when I >>>>>> tried to check on 480x854 resolution the vertical alignment of the custom >>>>>> view was incorrect. Checked with 480x800 on the same screen size and it >>>>>> again works. I than went for a big jump and checked in GalaxyTab and the >>>>>> horizontal and vertical alignments were wrong. Now my first though was >>>>>> that >>>>>> the bitmap width and height were the one of the image not the actual >>>>>> resized imageview. So I spent a lot of time on trying to get the real >>>>>> size >>>>>> of the imageview and even went for viewTreeObserver but the results were >>>>>> all the same - the correct, unchanged (unscaled?) bitmap size. So being >>>>>> positive that the problem is not here I couldn't get through further. I >>>>>> see >>>>>> 2 options here: >>>>>> >>>>>> -either I still can't get the true 'real' size of the imageview >>>>>> (although I think I have ruled this out by checking the size by adding >>>>>> listeners to biewTreeObserver) >>>>>> -or, although I get the right size and calculate the alignment >>>>>> parameters correctly, they are somehow 'incorrectly' (differently) used >>>>>> in >>>>>> the layout. >>>>>> >>>>>> >>>>>> Does anyone have an idea why the alignment is not working correctly? >>>>>> >>>>>> PS: as for the image view in layout xml file I have 2 configurations >>>>>> for long and notlong but this image has the same description in both: >>>>>> >>>>>> <ImageView >>>>>> android:src="@drawable/**clokin**g" >>>>>> >>>>>> >>>>>> android:id="@+id/**imageViewClk**" >>>>>> android:layout_height="wrap_**c**ontent" >>>>>> android:layout_width="wrap_**co**ntent" >>>>>> >>>>>> >>>>>> android:layout_**centerHorizont**al="true" >>>>>> android:layout_above="@+id/**im**ageViewProcess" >>>>>> >>>>>> android:adjustViewBounds="**tru**e" >>>>>> >>>>>> android:cropToPadding="false" >>>>>> android:layout_marginTop="**60d**p" >>>>>> android:scaleType="fitXY"> >>>>>> >>>>>> </ImageView> >>>>>> >>>>>> -- >>>>>> You received this message because you are subscribed to the Google >>>>>> Groups "Android Developers" group. >>>>>> To post to this group, send email to android-d...@**googlegroups.com >>>>>> >>>>>> To unsubscribe from this group, send email to >>>>>> android-developers+**unsubscribe**@googlegroups.com >>>>>> For more options, visit this group at >>>>>> http://groups.google.com/**group**/android-developers?hl=en<http://groups.google.com/group/android-developers?hl=en> >>>>>> >>>>> >>>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "Android Developers" group. >>>> To post to this group, send email to [email protected] >>>> To unsubscribe from this group, send email to >>>> android-developers+**[email protected] >>>> For more options, visit this group at >>>> http://groups.google.com/**group/android-developers?hl=en<http://groups.google.com/group/android-developers?hl=en> >>>> >>> >>> -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en > -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

