2

Is this a good way for all android devices ? I have an imageview as background, and I resize it according to the resolution of the android device.

private Bitmap getResizedBitmap() 
{
    Bitmap resizedBitmap;
    Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.background_3ball);
    x640 = bmp.getWidth();
    y960 = bmp.getHeight(); 

    if(screenRatio > ratioOrginal)
    {
        imageWidth=(int)((double)y960*widthMain/heightMain);
        imageHeight=y960;
        int x = (x640-imageWidth)/2;
        resizedBitmap = Bitmap.createBitmap(bmp,x,0, imageWidth, imageHeight);
    }
    else if(screenRatio < ratioOrginal)
    {
        imageWidth=x640;
        imageHeight=(int)((double)x640*heightMain/widthMain);
        int y = (y960-imageHeight)/2;
        resizedBitmap = Bitmap.createBitmap(bmp,0,y, imageWidth, imageHeight);
    }
    else
    {
        return bmp;
    }



    tw.setText(widthMain + "\n" + heightMain + "\n" + imageWidth + "\n" + imageHeight);

    return resizedBitmap;
}

private void SetBackground() 
{
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    widthMain = metrics.widthPixels;
    heightMain = metrics.heightPixels;
    screenRatio = ((double)heightMain) / widthMain;

    Bitmap resizedBitmap = getResizedBitmap();

    imageBackground.setImageBitmap(resizedBitmap); 
}

using dp or another thing makes the image look bad. For example I have circe in the backgorund and it seems like an elipse while scaling it automatically

 for example my image has dimensions of 640x960.
 ratioOrginal is 960:640=1.5
 if a device is mdpi and having dimension of 320x480
 screenRatio=480/320=1.5 and it is okay.
 but if it is mdpi and having dimension of 480x800
 screenRatio=900/480 is NOT 1.5.
 both are mdpi, but the ratio is not standard.
 I can not understand how people do this
 in supporting multiple screens there is one xml for mdpi's
 how it get's different resolutions.
user2248097
  • 21
  • 1
  • 3
  • Do You want to do this for Your button (like this question is) or Do You want to change size of image views at runtime? If You want to set this to Your layout to be fixed and not changed at runtime this is not a good way to do it, scaling bitmaps programmatically and set them to an imageButton needs big resources at runtime. Why don´t You set the different sizes in Your layout.xml? You should read this http://developer.android.com/guide/practices/screens_support.html – Opiatefuchs Jun 21 '13 at 10:43
  • I editted my quesiton – user2248097 Jun 21 '13 at 12:09

1 Answers1

1

I would recommend to use ScaleType instead.

Android recommends to use dp and to create different layout-files for different screen sizes.

Supporting Multiple Screens

  • for example my image has dimensions of 640x960. ratioOrginal is 960:640=1.5 if a device is mdpi and having dimension of 320x480 screenRatio=480/320=1.5 and it is okay. but if it is mdpi and having dimension of 480x800 screenRatio=900/480 is NOT 1.5. both are mdpi, but the ratio is not standard. I can not understand how people do this in supporting multiple screens there is one xml for mdpi's how does it get different resolutions?? – user2248097 Jun 21 '13 at 14:55
  • if the original ratio doesn't equals one of the other dpis you can use the ScaleType.CENTER_CROP. "Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding). The image is then centered in the view. From XML, use this syntax: android:scaleType="centerCrop"." – Biagio Cannistraro Jun 25 '13 at 09:27