| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
uzsu4968
8年前发布

计算Bitmap内存占用计算

   <p>densityDpi表示一英寸有多少个像素点</p>    <table>     <thead>      <tr>       <th>densityDpi</th>       <th>density</th>       <th>drawable目录</th>       <th>ic_launcher大小</th>      </tr>     </thead>     <tbody>      <tr>       <td>640</td>       <td>4</td>       <td>drawable-xxxhdpi</td>       <td>192x192</td>      </tr>      <tr>       <td>480</td>       <td>3</td>       <td>drawable-xxhdpi</td>       <td>144x144</td>      </tr>      <tr>       <td>320</td>       <td>2</td>       <td>drawable-xhdpi</td>       <td>96x96</td>      </tr>      <tr>       <td>240</td>       <td>1.5</td>       <td>drawable-hdpi</td>       <td>72x72</td>      </tr>      <tr>       <td>160</td>       <td>1</td>       <td>drawable-mdpi</td>       <td>48x48</td>      </tr>      <tr>       <td>160</td>       <td>1</td>       <td>drawable</td>       <td>-</td>      </tr>      <tr>       <td>-</td>       <td>-</td>       <td>drawable-nodpi</td>       <td>-</td>      </tr>     </tbody>    </table>    <p>xml布局使用R.drawable.xx相当于代码</p>    <p>BitmapFactory.decodeResource(getResources(), R.drawable.xx)</p>    <p>假设当前设备的dpi是560,那么系统匹配drawable目录的顺序是:</p>    <p>1) drawable-xxxhdpi</p>    <p>2) drawable-nodpi</p>    <p>3) drawable-xxhdpi</p>    <p>4) drawable-xhdpi</p>    <p>5) drawable-hdpi</p>    <p>6) drawable-mdpi</p>    <p>7) drawable</p>    <p>加载图片会经过缩放,内存占用计算公式如下</p>    <pre>  <code class="language-java">float scale = (float) deviceDensityDpi / drawableDirDensityDpi;  int scaledWidth = (int) (imgWidth * scale + 0.5f);  int scaledHeight = (int) (imgHeight * scale + 0.5f);  // 这里为什么是一个像素点占4字节,下面再说  int byteCount = scaledWidth * scaledHeight * 4;</code></pre>    <p>如果最终选择的是drawable目录,经过测试,其实就和放在drawable-mdpi是一样的,从下上面的公式和表格,可以得出drawableDirDensityDpi越小,最终byteCount越大。所以最坏的情况就是本该放在drawable-xxxhdpi目录的图片放到了drawable目录</p>    <p>如果最终选择的是drawable-nodpi目录,那么是不会经过缩放的,也就是</p>    <pre>  <code class="language-java">int byteCount=imgWidth * imgHeight * 4</code></pre>    <p>再说下,为什么说一个像素点是4个字节</p>    <p>其实加载图片的时候还要根据BitmapFactory.Options,如果options.inPreferredConfig = Bitmap.Config.ARGB_8888那么一个像素就是占4字节,而默认就是ARGB_8888</p>    <p>ALPHA_8就是Alpha由8位组成=1字节</p>    <p>ARGB_4444就是由4个4位组成即16位=2字节</p>    <p>ARGB_8888就是由4个8位组成即32位=4字节</p>    <p>RGB_565就是R为5位,G为6位,B为5位共16位=2字节</p>    <p>ARGB_4444已经不建议使用,质量太差,官方有这样一句注释</p>    <p>@deprecated Because of the poor quality of this configuration, it is advised to use {@link #ARGB_8888} instead.</p>    <h2><strong>关于jpg和png</strong></h2>    <p>对于Android来说并没有区别,比如一张100x100的jpg,大小10k,一张100x100的png,大小15k,如果放在同一个drawable目录下,它们加载进来占用的内存是一摸一样的,只跟分辨率有关。通常jpg图片的文件大小会比png图片小,是因为jpg是有损压缩,而png是无损压缩,而且jpg没有alpha通道</p>    <p>Demo</p>    <p><img src="https://simg.open-open.com/show/2622aca071f34c071583487b01f486f7.png"></p>    <pre>  <code class="language-java">public class MainActivity extends Activity {        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);            BitmapFactory.Options options = new BitmapFactory.Options();          options.inPreferredConfig = Bitmap.Config.ARGB_8888;            // 同一张图片,每个目录都存一份          // drawable-nodpi          Bitmap nodpi = BitmapFactory.decodeResource(getResources(), R.drawable.nodpi);          // drawable          Bitmap _default = BitmapFactory.decodeResource(getResources(), R.drawable._default);          // drawable-mhdpi          Bitmap mhdpi = BitmapFactory.decodeResource(getResources(), R.drawable.mhdpi);          // drawable-hdpi          Bitmap hdpi = BitmapFactory.decodeResource(getResources(), R.drawable.hdpi);          // drawable-xhdpi          Bitmap xhdpi = BitmapFactory.decodeResource(getResources(), R.drawable.xhdpi);          // drawable-xxhdpi          Bitmap xxhdpi = BitmapFactory.decodeResource(getResources(), R.drawable.xxhdpi);          // drawable-xxxhdpi          Bitmap xxxhdpi = BitmapFactory.decodeResource(getResources(), R.drawable.xxxhdpi);            Log.d("TEST", "nodpi=" + nodpi.getByteCount());          Log.d("TEST", "default=" + _default.getByteCount());          Log.d("TEST", "mhdpi=" + mhdpi.getByteCount());          Log.d("TEST", "hdpi=" + hdpi.getByteCount());          Log.d("TEST", "xhdpi=" + xhdpi.getByteCount());          Log.d("TEST", "xxhdpi=" + xxhdpi.getByteCount());          Log.d("TEST", "xxxhdpi=" + xxxhdpi.getByteCount());            DisplayMetrics displayMetrics = new DisplayMetrics();          getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);          float realDensityDpi = displayMetrics.densityDpi;          // 根据densityDpi从不同的目录加载图片          float drawableDirDensityDpi;          if (realDensityDpi < 240) {              drawableDirDensityDpi = 160;          } else if (realDensityDpi < 320) {              drawableDirDensityDpi = 240;          } else if (realDensityDpi < 480) {              drawableDirDensityDpi = 320;          } else if (realDensityDpi < 560) {              drawableDirDensityDpi = 480;          } else {              drawableDirDensityDpi = 640;          }          float scale = realDensityDpi / drawableDirDensityDpi;          // 默认是Bitmap.Config.ARGB_8888,所以是4字节          // 我的设备dpi是560,所以是在xxxhdpi目录下          int byteCount = (int) (192 * scale + 0.5) * (int) (192 * scale + 0.5) * 4;          Bitmap ic_launcher = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);          Log.d("TEST", ic_launcher.getByteCount() + " " + byteCount);      }  }</code></pre>    <p> </p>    <p> </p>    <p> </p>    <p>来自:http://www.jianshu.com/p/e1d42bf88d40</p>    <p> </p>    
 本文由用户 uzsu4968 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1474165203445.html
Bitmap 安卓开发 Android开发 移动开发