安卓图片资源笔记

来源:互联网 发布:好看的网络剧 编辑:程序博客网 时间: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
原创粉丝点击