android根据不同的分辨率来读取图片

刚自己折腾了个小程序,用来显示图片,但只能在一种分辨率的手机上看着比较舒服,其他分辨率的手机上有的太小有的太大,经过网上的查找资料,我自己写了一个程序来测试。

android对这种情况也考虑了,它在res目录下有三个(drawable-hdpi,drawable-mdpi,drawable-ldpi)三个子目录。

hdpi用来存放高分辨率的图片,比如WVGA (480x800),FWVGA (480x854)。

    mdpi用来存放中分辨率的图片,比如HVGA (320x480)。

    ldpi用来存放低分辨率的图片,比如QVGA (240x320)。

系统会根据机器的分辨率来分别到这几个文件夹中找对应的图片。

 

我在hdpi中放的是72×72大小的图片,在mdpi中放的是48×48大小的图片,在ldpi中放的是36*36大小的图片。

 

以下是我的xml布局文件,主要是三个ImageView来显示图片。

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <RelativeLayout android:id="@+id/all_apps_button_cluster" android:layout_width="fill_parent" android:layout_height="56dip" android:layout_gravity="bottom|center_horizontal" android:paddingTop="2dip" > <ImageView android:id="@+id/all_apps_button" android:paddingLeft="12dip" android:paddingRight="12dip" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_centerHorizontal="true" android:src="@drawable/all_apps_button_normal" /> <ImageView android:id="@+id/hotseat_left" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_toLeftOf="@id/all_apps_button" android:layout_marginLeft="4dip" android:src="@drawable/hotseat_phone_normal" /> <ImageView android:id="@+id/hotseat_right" android:layout_marginRight="4dip" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_toRightOf="@id/all_apps_button" android:src="@drawable/hotseat_browser_normal" /> </RelativeLayout> </LinearLayout> 

以下是我的AndroidManifest.xml中的代码:

其中<supports-screens

  android:largeScreens="true"

      android:normalScreens="true"

  android:anyDensity = "true"/>是不能省略的,我由于省略了,导致我折腾了一个上午。

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="lzu.xmlTest" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".xmlTest" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <supports-screens android:largeScreens="true" android:normalScreens="true" android:anyDensity = "true"/> </manifest> 

你可能感兴趣的:(android根据不同的分辨率来读取图片)