Android开发之图片详解(2)
来源:互联网 发布:一人之下知乎 编辑:程序博客网 时间:2024/05/02 02:44
这一篇 主要分析下不同drawable文件夹下的图片在Android手机上的加载效果。
首先在drawble-mdpi和drawble-xhdpi文件夹下分别放入同一张图片,注意是同一张图片,要起不同的名字,如图:
测试代码如下:
package com.example.imagetext;import java.util.ArrayList;import java.util.List;import android.annotation.SuppressLint;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.os.Build;import android.os.Bundle;import android.util.DisplayMetrics;import android.util.Log;import android.view.WindowManager;import android.widget.ImageView;public class MainActivity extends Activity { private ImageView iv1,iv2; private Bitmap bp1, bp2; private Drawable d1,d2; List<Drawable> list = new ArrayList<Drawable>(); List<Bitmap> listb = new ArrayList<Bitmap>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv1 = (ImageView) findViewById(R.id.iv1); iv2 = (ImageView) findViewById(R.id.iv2); d1 = iv1.getDrawable(); d2 = iv2.getDrawable(); bp1 = ((BitmapDrawable)d1).getBitmap(); bp2 = ((BitmapDrawable)d2).getBitmap(); Log.e("tttext","频幕密度:"+getScreenDensityDpi(this)); Log.e("tttext","drawble-mdpi:"+"宽"+bp1.getWidth()+"高"+bp1.getHeight()+"占用内存:"+getBitmapsize(bp1)/1024+"KB"); Log.e("tttext","drawble-xhdpi:"+"宽"+bp2.getWidth()+"高"+bp2.getHeight()+"占用内存:"+getBitmapsize(bp2)/1024+"KB"); } @SuppressLint("NewApi") public long getBitmapsize(Bitmap bitmap) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) { return bitmap.getByteCount(); } // Pre HC-MR1 return bitmap.getRowBytes() * bitmap.getHeight(); } /** * 获取屏幕密度系数 * * 例:0.75 / 1.0 / 1.5 / 2 / 3 * */ public float getScreenDensity(Context context) { DisplayMetrics metric = new DisplayMetrics(); WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); manager.getDefaultDisplay().getMetrics(metric); return metric.density; } /** * 获取屏幕密度 * * 例:120 / 160 / 240 / 320(280-400) / 480(400-560) * */ public int getScreenDensityDpi(Context context) { DisplayMetrics metric = new DisplayMetrics(); WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); manager.getDefaultDisplay().getMetrics(metric); return metric.densityDpi; } @Override protected void onStart() { super.onStart(); // Log.e("tttext", Runtime.getRuntime().totalMemory()/1024/1024+"kk"+Runtime.getRuntime().freeMemory()/1024/1024+"ff"+Runtime.getRuntime().maxMemory()/1024/1024); }}
<LinearLayout 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" android:orientation="vertical" ><ImageView android:id="@+id/iv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon_nongye" /><ImageView android:layout_marginTop="10dp" android:id="@+id/iv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon_zhaoshang" /></LinearLayout>
运行出来的效果图
然后是打印的截图
从这里就可以看出,drawble-mdpi文件夹下的图片被放大成了原来的2倍,而drawble-xhdpi文件夹下的图片分辨率不变。
在上一篇里我们讲到了各个文件夹对应的dpi分别是:
drawable-mdpi 160dpi
drawable-hdpi 240dpi
drawable-xhdpi 320dpi
drawable-xxhdpi 480dpi
drawable-xxxhdpi 640dpi
因为这个测试机频幕密度刚好是320dpi,所以在xhdpi下的图片刚好保持原样,而在mdpi下的图片则变为原来的320/160,即2倍。
同时可以看到,图片占用的内存也变成了原来的2*2=4倍;
同理,如果手机密度比较低的时候,高分辨率的图片也是会被压缩的,所以我们做图片适配的时候,完全可以从最高分辨率往下适配,也就是说在xxhdpi里放一套图就可以了,这样不仅可以减少包的整体大小,也不会在低密度的手机上出现内存占用过大的问题(因为图片被压缩过后,占用的内存也会相应地缩小)。
下一篇 将具体分析下bitmap和drawble的关系,以及iamgeview的几个设置图片的方法的区别。
0 0
- Android开发之图片详解(2)
- Android开发之图片详解(3)
- Android开发之图片详解(4)
- Android开发之图片详解(5)
- Android开发环境搭建(图片详解)
- Android 开之图片详解(1)
- android开发之拖动图片(Gallary )
- Android开发之图片浏览器
- Android开发之图片浏览器
- android 开发之 显示图片
- Android 开发之上传图片
- Android开发之MdiaPlayer详解(api)
- Android异步加载图片详解之方式二(2)
- Android异步加载图片详解之方式一(2)
- Android图片缓存之Bitmap详解(一)
- Android图片缓存之Bitmap详解(一)
- android开发之就本地和网络图片的压缩以及缓存详解
- android开发之就本地和网络图片的压缩以及缓存详解
- Mathematica绘制常见曲线
- ISO7816-3 T=1
- Spring Security 4 Hibernate整合 注解和xml例子(带源码)
- Markdown 使用
- JiaThis 分享插件调用
- Android开发之图片详解(2)
- Java并发编程:如何创建线程?
- python使用mongo数据库
- ListView的置顶操作与滑动监听
- 触发器入门知识
- PowerDesigner15的安装和破解
- SQL 左外连接,右外连接,全连接,内连接
- gap debug is unable to locate the chrome browser executable 问题
- 【第二章】 IoC 之 2.2 IoC 容器基本原理 ——跟我学Spring3