Aligning a view to an ImageView in different resolutions

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

Aligning a view to an ImageView in different resolutions

Postby srd.pl » Wed Sep 26, 2012 8:40 pm

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:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     //get screen density
  2.        float density = getBaseContext().getResources().getDisplayMetrics().density;
  3.  
  4.        //set the progress bar position according to screen density
  5.        if ( density == 1.0f)
  6.        {
  7.            ImageView micImage = ((ImageView) findViewById(R.id.imageViewClk));
  8.            Drawable drawing = micImage.getDrawable();
  9.             Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
  10.  
  11.             // Get current dimensions
  12.             int width = bitmap.getWidth();
  13.             int height = bitmap.getHeight();
  14.  
  15.             LayoutParams params = new LayoutParams((int)(height/13.94), (int)(height/13.94));
  16.             params.setMargins((int)(width/2.30), 0, 0, (int)(height/2.75));
  17.  
  18.             params.addRule(RelativeLayout.ALIGN_LEFT,R.id.imageViewClk);
  19.             params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.imageViewClk);
  20.             myCustomTwistedProgressBar.setLayoutParams(params);
  21.        }else if ( density == 1.5f ){
  22.            ImageView micImage = ((ImageView) findViewById(R.id.imageViewClk));
  23.            Drawable drawing = micImage.getDrawable();
  24.             Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
  25.  
  26.             int width = bitmap.getWidth();
  27.             int height = bitmap.getHeight();
  28.  
  29.             LayoutParams params = new LayoutParams((int)Math.round(height/14.13), (int)Math.round(height/14.13));
  30.             params.setMargins((int)Math.round( width/2.27), 0, 0, (int)Math.round(height/2.91));
  31.  
  32.             params.addRule(RelativeLayout.ALIGN_LEFT,R.id.imageViewClk);
  33.             params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.imageViewClk);
  34.             myCustomTwistedProgressBar.setLayoutParams(params);
  35.        }else if ( density == 0.75f ){
  36.            ImageView micImage = ((ImageView) findViewById(R.id.imageViewClk));
  37.            Drawable drawing = micImage.getDrawable();
  38.             Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
  39.  
  40.             // Get current dimensions
  41.             int width = bitmap.getWidth();
  42.             int height = bitmap.getHeight();
  43.  
  44.             LayoutParams params = new LayoutParams((int)(height/14.88), (int)(height/14.88));
  45.             params.setMargins((int)(width/2.27), 0, 0, (int)(height/2.69));
  46.  
  47.             params.addRule(RelativeLayout.ALIGN_LEFT,R.id.imageViewClk);
  48.             params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.imageViewClk);
  49.             myCustomTwistedProgressBar.setLayoutParams(params);
  50.        }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


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:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <ImageView
  2. android:src="@drawable/cloking"
  3. android:id="@+id/imageViewClk"
  4. android:layout_height="wrap_content"
  5. android:layout_width="wrap_content"
  6. android:layout_centerHorizontal="true"
  7. android:layout_above="@+id/imageViewProcess"
  8. android:adjustViewBounds="true"
  9. android:cropToPadding="false"
  10. android:layout_marginTop="60dp"
  11. android:scaleType="fitXY">
  12. </ImageView>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
srd.pl
Junior Developer
Junior Developer
 
Posts: 22
Joined: Wed Jun 02, 2010 12:20 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Exabot [Bot] and 4 guests