安卓加載大圖片學習筆記

加載大圖片代碼片斷android

package com.tang.bigpic;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Point;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.os.Build;

public class MainActivity extends Activity {
	private ImageView iv;
	private int screeX;
	private int screeY;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		iv = (ImageView) findViewById(R.id.pic);
		WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
		screeY = wm.getDefaultDisplay().getHeight();
		screeX = wm.getDefaultDisplay().getWidth();
		//Point outSize = new Point();
		//wm.getDefaultDisplay().getSize(outSize);
		//outSize.x;
		//outSize.y;
	}
	
	public void getPic(View view)
	{
		String path = "/data/data/com.look.pic/test.jpg";
		//Bitmap bm = BitmapFactory.decodeFile("/data/data/com.look.pic/test.jpg");
		//iv.setImageBitmap(bm);
		BitmapFactory.Options opts = new Options();
		//並非真正的去解析圖片只是讀取圖片的相關信息;
		opts.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(path, opts);
		int hegiht = opts.outHeight;
		int width = opts.outWidth;
		//計算縮放比例
		int scaleX = width/screeX;
		int scaleY = hegiht/screeY;
		
		int scale = 1;
		if (scaleX > scaleY & scaleX > 1) {
			scale = scaleX;
		}
		if (scaleY > scaleX & scaleY > 1) {
			scale = scaleY;
		}
		opts.inJustDecodeBounds = false;
		opts.inSampleSize = scale;
		Bitmap bm = BitmapFactory.decodeFile(path, opts);
		iv.setImageBitmap(bm);
	}
}
相關文章
相關標籤/搜索