安卓图片资源笔记
来源:互联网 发布:好看的网络剧 编辑:程序博客网 时间:2024/05/16 19:17
安卓图片资源笔记
1.资源目录的density
ldpi: density=120
mdpi:density=160
hdpi:density=240
xhdpi:density=320
xxhdpi:density=480
2.获取手机屏幕密度
DisplayMetrics dc = getResources().getDisplayMetrics();textView.setText("屏幕属性:\ndensity=" + dc.density + " ,densityDpi=" + dc.densityDpi + " ,xdpi=" + dc.xdpi + " ,ydpi=" + dc.ydpi + " ,size=" + dc.widthPixels + "*" + dc.heightPixels);
3.解析图片的真实尺寸
final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;//第一次解析将inJustDecodeBounds设置为true,来获取图片大小BitmapFactory.decodeResource(getResources(), res_id, options);int realWidth = options.outWidth;int realHeight = options.outHeight;
4.图片显示或者BitmapFactory.decodeResource解析图片时的缩放比
缩放比例=屏幕的真实密度(densityDpi)/资源文件所在的文件的属性density
5.加载大图片
final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;//第一次解析将inJustDecodeBounds设置为true,来获取图片大小BitmapFactory.decodeResource(getResources(), res_id, options);// 调用上面定义的方法计算inSampleSize值int scale = calculateInSampleSize(options, imageView.getWidth(), imageView.getHeight());if(scale <= 0){//要注意的是,inSampleSize 可能小于0,必须做判断scale = 1;}//inSampleSize = 2,则取出的缩略图的宽和高都是原始图片的1/2,图片大小就为原始大小的1/4。options.inSampleSize = scale;// 使用获取到的inSampleSize值再次解析图片options.inJustDecodeBounds = false;options.inPreferredConfig = Bitmap.Config.RGB_565; //减少内存消耗Bitmap bitmap = BitmapFactory.decodeResource(getResources(), res_id, options);/** * 计算inSampleSize,用于压缩图片 * * @param options * @param reqWidth * @param reqHeight * @return */public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {// 源图片的宽度int width = options.outWidth;int height = options.outHeight;int inSampleSize = 1;// int min = Math.min(width, height);// int maxReq = Math.max(reqWidth, reqHeight);// if(min > maxReq) {// inSampleSize = Math.round((float) min / (float) maxReq);// }Log.e(TAG, "sampleSize w * h=" + width + "*" + height);// 通过之前的计算方法,在加载类似400*4000这种长图时会内存溢出if (width > reqWidth || height > reqHeight){int widthRadio = Math.round(width * 1.0f / reqWidth);int heightRadio = Math.round(height * 1.0f / reqHeight);inSampleSize = Math.max(widthRadio, heightRadio);}Log.e(TAG, "sampleSize =" + inSampleSize);return inSampleSize;}
6.加载不同dpi下的图片不缩放
/*解析图片,让图片在不同密度的手机上不会被缩放*/private Bitmap decodeResourceNoScale(Resources resources, int res_id) { TypedValue value = new TypedValue(); resources.openRawResource(res_id, value); BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inTargetDensity = value.density; return BitmapFactory.decodeResource(resources, res_id, opts);}
7.简单例子
package com.example.imagedensity;import android.app.Activity;import android.content.res.Resources;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.util.DisplayMetrics;import android.util.Log;import android.util.TypedValue;import android.widget.ImageView;import android.widget.TextView;public class MainActivity extends Activity{private final static String TAG = MainActivity.class.getSimpleName();private TextView[] textViews;private ImageView[] imageViews;private int[] resId = new int[]{R.drawable.test_l, R.drawable.test_m, R.drawable.test_h, R.drawable.test_xh, R.drawable.test_xxh};@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private void initView(){textViews = new TextView[5];imageViews = new ImageView[5];textViews[0] = (TextView) findViewById(R.id.textView1);imageViews[0] = (ImageView) findViewById(R.id.imageview1);textViews[1] = (TextView) findViewById(R.id.textView2);imageViews[1] = (ImageView) findViewById(R.id.imageview2);textViews[2] = (TextView) findViewById(R.id.textView3);imageViews[2] = (ImageView) findViewById(R.id.imageview3);textViews[3] = (TextView) findViewById(R.id.textView4);imageViews[3] = (ImageView) findViewById(R.id.imageview4);textViews[4] = (TextView) findViewById(R.id.textView5);imageViews[4] = (ImageView) findViewById(R.id.imageview5);TextView textView = (TextView) findViewById(R.id.textView0);DisplayMetrics dc = getResources().getDisplayMetrics();textView.setText("屏幕属性:\ndensity=" + dc.density + " ,densityDpi=" + dc.densityDpi + " ,xdpi=" + dc.xdpi + " ,ydpi=" + dc.ydpi + " ,size=" + dc.widthPixels + "*" + dc.heightPixels + "\n图片真实尺寸:300 * 160");for(int i = 0; i < textViews.length; i++){ Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId[i]); int w = bitmap.getWidth(); int h = bitmap.getHeight(); bitmap.recycle();TypedValue value = getTargetDensityByResource(getResources(), resId[i]);textViews[i].setText("资源属性:" + value.string + ", density=" + value.density + "\n尺寸:" + w + "*" + h + " ,scale=" + dc.densityDpi + "/" + value.density);}} /*解析图片所在文件夹的属性*/ private TypedValue getTargetDensityByResource(Resources resources, int res_id) { TypedValue value = new TypedValue(); resources.openRawResource(res_id, value); Log.e(TAG, "value.density: " + value.density + "," + value.string); return value; } /*解析图片,让图片在不同密度的手机上不会被缩放*/ private Bitmap decodeResourceNoScale(Resources resources, int res_id) { TypedValue value = new TypedValue(); resources.openRawResource(res_id, value); BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inTargetDensity = value.density; return BitmapFactory.decodeResource(resources, res_id, opts); }}
xml:
<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView0" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp" android:textColor="#ff0000"/> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp" android:textColor="#ff0000" android:text="说明:\n缩放比例=屏幕的真实densityDpi/资源文件所在文件夹的density\n下面测试:"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp"/> <ImageView android:id="@+id/imageview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/test_l"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp"/> <ImageView android:id="@+id/imageview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/test_m"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textView3" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp"/> <ImageView android:id="@+id/imageview3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/test_h"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textView4" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp"/> <ImageView android:id="@+id/imageview4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/test_xh"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textView5" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp"/> <ImageView android:id="@+id/imageview5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/test_xxh"/> </LinearLayout> </LinearLayout></ScrollView>
效果图:
加载大图片,防止溢出:
http://blog.csdn.net/bigconvience/article/details/27054639
http://blog.csdn.net/sjf0115/article/details/7366746
0 0
- 安卓图片资源笔记
- 安卓图片资源更改的问题
- 选择安卓系统中的图片资源
- 安卓 imageview资源图片ID获取方法
- 安卓SimpleAdapter关于设置图片资源问题
- 安卓 imageview资源图片ID获取方法
- 安卓:一张图片资源要占用多大内存
- 安卓对资源图片的保存知识理解
- 【安卓笔记】异步加载大量图片
- 【安卓笔记】异步加载大量图片
- 安卓学习笔记(十一)资源汇总
- 安卓学习资源
- 安卓学习资源
- 【安卓笔记】图片特效之底片效果
- 【安卓笔记】图片特效之黑白效果
- 【安卓笔记】图片特效之灰度效果
- 【安卓笔记】图片特效之浮雕效果
- 【安卓笔记】-----图片特效之怀旧风格
- LeetCode 526. Beautiful Arrangement
- input只可以输入数字或者是最多小数点2位的js代码
- 3 epoll反应堆复杂型
- java并发编程实践学习(14 ) 构建自定义的同步工具
- Ubuntu16.04下配置caffe(仅CPU)
- 安卓图片资源笔记
- 自动生成数据库表设计(二)之Freemarker的基本使用
- 机器学习算法总结--K均值算法
- Firefox 不知道如何打开此地址,因为协议 (javascrpit) 未和任何程序关联
- appium dmg安装环境配置
- 自动生成数据库表设计(三)之制作word模版
- SDWebImage加载Https图片失败的解决方法
- SendMessage()与PostMessage()的区别
- setContentType与setCharacterEncoding的区别