Reputation: 661
Here is my problem: I put a image with resolution 200x200 in a imageview with height and width both be 200dip, but the image shown seemed larger than 200x200, here is how I found the problem.
I use below code to get the width and height of my screen. And found out my screen is 540x800, but the image width is larger than half of my screen.
WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
width = wm.getDefaultDisplay().getWidth();
height = wm.getDefaultDisplay().getHeight();
Is there anyone who can tell me why. Below is the xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation = "vertical"
android:background = "@drawable/mainbg2"
>
<LinearLayout
android:orientation="horizontal"
android:id="@+id/layout_menu_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<ImageView
android:id="@+id/img_menu_normal"
android:layout_width="200dip"
android:layout_height="200dip"
android:src="@drawable/menu_normal"/>
</LinearLayout>
</RelativeLayout>
Upvotes: 0
Views: 134
Reputation: 6563
Check this documentation.
Density-independent pixel (dp) A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way.
The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.
So I assume that your device is not mdpi which use bigger scale ratio. If you want to make exact pixel size on you device you can use 200px
. But first check the documentations link above.
Upvotes: 1
Reputation: 1091
it's display 200X200 dp size image in view you can check using this xmp change in you file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/layout_menu_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img_menu_normal"
android:layout_width="200dip"
android:layout_height="200dip"
android:background="@android:color/black"
android:src="@drawable/ic_launcher" />
</LinearLayout>
i hope you can understant what happen there. i am set image in centre of relative view and change background color of imageview to black and main view bg color to white.it's working code.
Upvotes: 0