安卓圖片資源筆記
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};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.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