Android不同密度文件下图片盘踞内存大小
Android不同密度文件下图片占据内存大小
做了一个测试,我手机的dpi是hdpi,于是我在drawable-xhpi的目录下放置了一个360*600大小的背景图,测试这个背景图片占据内存的大小。
布局文件文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/pg_login_background"
android:id="@+id/root"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/test_hello"
/>
<Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试背景占据内存大小"
android:id="@+id/test_memory"/>
<TextView
android:id="@+id/hint_memory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="测试图片占据多少内存"/>
</LinearLayout>
测试内存大小的代码如下:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.test_memory).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View root=findViewById(R.id.root);
BitmapDrawable background=(BitmapDrawable)root.getBackground();
Bitmap bmp=background.getBitmap();
TextView tv= (TextView) findViewById(R.id.hint_memory);
int size=bmp.getRowBytes()*bmp.getHeight();
tv.setText("背景图片占据内存内存大小 "+size+" byte");
}
});
}
测试结果如下:

于是我又做了令外一个测试,手机dpi还是hdpi,删除drawable-hpdi目录的背景图片,新建了一个drawable-xhpi的文件夹,放置了一个480*800的背景图片,测试结果如下:

我的问题是:
手机dpi是hdpi,去加载hdpi目录大小的图片得到的大小是864000byte,而去加载xhdpi图片得到的大小是432000byte.这个占据 内存大小是如何计算得到的。
------解决方案--------------------
两个图片进行分别比较 没有可比性
看一下 两个图片的大小各是多少;
另外
确定 加载进内存的图片 与 磁盘上的图片 是否一致
------解决方案--------------------
这么吊,。。。。。。。
做了一个测试,我手机的dpi是hdpi,于是我在drawable-xhpi的目录下放置了一个360*600大小的背景图,测试这个背景图片占据内存的大小。
布局文件文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/pg_login_background"
android:id="@+id/root"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/test_hello"
/>
<Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试背景占据内存大小"
android:id="@+id/test_memory"/>
<TextView
android:id="@+id/hint_memory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="测试图片占据多少内存"/>
</LinearLayout>
测试内存大小的代码如下:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.test_memory).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View root=findViewById(R.id.root);
BitmapDrawable background=(BitmapDrawable)root.getBackground();
Bitmap bmp=background.getBitmap();
TextView tv= (TextView) findViewById(R.id.hint_memory);
int size=bmp.getRowBytes()*bmp.getHeight();
tv.setText("背景图片占据内存内存大小 "+size+" byte");
}
});
}
测试结果如下:
于是我又做了令外一个测试,手机dpi还是hdpi,删除drawable-hpdi目录的背景图片,新建了一个drawable-xhpi的文件夹,放置了一个480*800的背景图片,测试结果如下:
我的问题是:
手机dpi是hdpi,去加载hdpi目录大小的图片得到的大小是864000byte,而去加载xhdpi图片得到的大小是432000byte.这个占据 内存大小是如何计算得到的。
------解决方案--------------------
两个图片进行分别比较 没有可比性
看一下 两个图片的大小各是多少;
另外
BitmapDrawable background=(BitmapDrawable)root.getBackground();
Bitmap bmp=background.getBitmap();
确定 加载进内存的图片 与 磁盘上的图片 是否一致
------解决方案--------------------
这么吊,。。。。。。。