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 <adam.lichwierow...@gmail.com> > 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 <adam.lich...@gmail.com> >> >>> 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.**94), >>> (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.**88), >>> (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.**94), >>> (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.**88), >>> (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/**cloking" >>> >>> android:id="@+id/**imageViewClk" >>> android:layout_height="wrap_**content" >>> android:layout_width="wrap_**content" >>> >>> android:layout_**centerHorizontal="true" >>> android:layout_above="@+id/**imageViewProcess" >>> android:adjustViewBounds="**true" >>> >>> android:cropToPadding="false" >>> android:layout_marginTop="**60dp" >>> 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+**unsubscr...@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 android-developers@googlegroups.com > To unsubscribe from this group, send email to > android-developers+unsubscr...@googlegroups.com > 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 android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en