drawable 按照分辨率压缩或者拉伸

把一张高清图,放在drawable-h 文件夹中,原图大小是72x72

代码如下。

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="36dip"
        android:layout_height="36dip"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

MainActivity.java

package com.czq.testdrawable;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ImageView;

public class MainActivity extends Activity {

	private ImageView img;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		img = (ImageView) findViewById(R.id.img);
		Drawable drawable = img.getDrawable();
		Log.i("MainActivity", "Bounds.getIntrinsicHeight = " + drawable.getIntrinsicHeight());
		Log.i("MainActivity", "Bounds.getIntrinsicWidth = " + drawable.getIntrinsicWidth());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}

运行在分辨率为 h的机器上。

Bounds.getIntrinsicHeight =72

Bounds. getIntrinsicWidth=72 

运行在分辨率为 m的机器上。

01-19 12:06:40.772: INFO/MainActivity(358):Bounds.getIntrinsicHeight = 48

01-19 12:06:40.772: INFO/MainActivity(358):Bounds.getIntrinsicWidth = 48

48=72/1.5

运行在分辨率为 l的机器上。

01-19 12:11:23.442: INFO/MainActivity(325):Bounds.getIntrinsicHeight = 36

01-19 12:11:23.442: INFO/MainActivity(325):Bounds.getIntrinsicWidth = 36

 

36=72/2

运行在分辨率为 xh的机器上。

01-19 12:15:59.937: DEBUG/SntpClient(70):Bounds.getIntrinsicHeight = 96

01-19 12:15:59.937: DEBUG/SntpClient(70):Bounds.getIntrinsicWidth = 96 

总结:当图片只有一份,只放在一个文件夹中,例如放在drawwable-hdpi中。当程序运行在其他分辨率的机器上时,图片会按比例拉伸或者压缩来适应当前机器。 

把高清图,放在了-m下,然后又运行在高清的机器上。

结果是:

例如一张480x800的图片。会变成480*1.5 x 800 *1.5

所以高清图一定要放在-h 或者-xh文件夹下,否者会因为图片拉伸的过大,程序内存占用的较多。

建议9path 的图,直接放在drawable目录下,不需要按照分辨率去拉伸或者压缩。



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值