1

I am setting an image programatically so it looks like circle but it looks like oval shape in some devices. my code is-

    int circleLeft = (int) (width * 3.3) / 100;
    circleLayoutParams.setMargins(circleLeft, (int) (height * 0.29),
    circleLeft, (int) (height * 0.18));
    circleMenu.setLayoutParams(circleLayoutParams);

xml-

            <com.example.converter.view.CircleLayout
            android:id="@+id/main_circle_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/glow_circle"/>

i have put images in drawable folder.what is wrong with this code.is it image size problem?

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
gbl
  • 178
  • 2
  • 12
  • http://stackoverflow.com/questions/18378741/how-to-make-an-imageview-in-circular-shape http://stackoverflow.com/questions/3914329/round-button-in-android – srikayala Nov 28 '13 at 06:11
  • when i wrote this-circleLayoutParams.setMargins(circleLeft, (int) (height * 0.29),circleLeft, (int) (height * 0.18));...my image display in circle shape and this is the problem.i have to write each time for every device..why?? – gbl Nov 28 '13 at 06:19
  • every device has different pixel sizes and different resolution. so you need to follow generalised procedures to make them into circle shape. not by multiplying width, dividing height etc.... look at some valuable examples and then proceed further – srikayala Nov 28 '13 at 06:25

1 Answers1

0

I encountered same problem in my app. It is because variation of PPI and Screen size of different devices. I solved it by getting screen size. It can be done by following code:-

//method to get screen size
public ArrayList<Integer> GetDeviceScreenSize()
{
    ArrayList<Integer> size= new ArrayList<Integer>();

    if((android.os.Build.VERSION.SDK_INT >= 13)){

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int w = metrics.widthPixels;
        int h = metrics.heightPixels;
        size.add(0,w);
        size.add(1,h);

    }else{
        Display display = ((WindowManager)ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int width = display.getWidth();
        int height = display.getHeight();
        size.add(0, width);
        size.add(1,height);
    }
    return size;

}

After you get device screen size through this method, you can set height and width of some part of height and width you got from this method. It should be like:-

ArrayList<Integer> size= GetDeviceScreenSize();
float width = (( (float)size.get(0))/320)*135;
int circleLeft = (int) (width * 3.3) / 100;

Remember, you'll have to try dividing and multiplying the device width and height with different units to get the exact size that you want, but once you get it, It will work on all devices, no matter what is there screen size.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
DroidDev
  • 1,527
  • 4
  • 20
  • 42
  • :but i use this-- DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int width = metrics.widthPixels;int circleLeft = (int) (width * 3.3) / 100; is it same na.. – gbl Nov 28 '13 at 06:33
  • @gbl Yes it is same, but getting display metrics only work for sdk level greater than or equal to 13, that is what i have checked in condition. Otherwise i run the else part. You can just try it by copy pasting whole code. I had put ((Activity)ctx) before getWindowManager() because I was running this code in different class than activity. But now I have removed it from code. – DroidDev Nov 28 '13 at 06:39
  • :not working in my code :( can u pls tell me one thing.if i put different size of images in hdpi,mdpi then it will work?? means may b the problem with drawable folder – gbl Nov 28 '13 at 07:03
  • @gbl: yes surely it can be a potential problem. you can check from internet that what are the different sizes of images that should be put into different drawable folders to make the application's images look proper on different devices. And you must put images of those sizes in different folders to make application compatible to most of devices. Actually these folders are for different resolution densities and android picks the images from folder automatically according to the resolution of the device on which app is running. So yes, that can also be a problem. – DroidDev Nov 28 '13 at 07:07