当前位置: 代码迷 >> Android >> android之bit地图详解
  详细解决方案

android之bit地图详解

热度:31   发布时间:2016-05-01 13:31:01.0
android之bitmap详解

主要介绍加载位图的5中方式:


package com.ghg.BitmapLoad;import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.BitmapFactory.Options;import android.os.Bundle;import android.view.View;import android.widget.ImageView;public class Day0702_BitmapLoadActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        initView();    }    private ImageView imageView;	private void initView() {		// TODO Auto-generated method stub		imageView=(ImageView) findViewById(R.id.iv_image);	}		public void doClick(View view){		/**		 * 1 .【加载位图】通过文件路径加载位图,显示原图,大小比例不变		 */				/*  Bitmap bmp=BitmapFactory.decodeFile("/mnt/sdcard/dog.jpg");			imageView.setImageBitmap(bmp);  */				/**		 * 2.【加载位图】通过文件路径加载位图,(若scale 值为n>1) 则图片长,宽变为原来的1/n,相当于把图片压缩到		 * 原来的1/(n*n),加载到手机内存占用的空间小,我们可以再xml文件中设置ImageView的scaleType=fitCenter属性,		 * 进行拉伸自适应操作,拉伸后的图像不是很清楚,但还是可以接受的。		 * 		 *    		 */				/*Options options=new Options();		options.inSampleSize=2; 		Bitmap bm=BitmapFactory.decodeFile("/mnt/sdcard/dog.jpg", options);		imageView.setImageBitmap(bm);*/				/**		 * 3.【加载位图】通过文件路径加载位图,此种方式,可以把原图缩小,或放大。		 *    下面例子是先压缩再放大。		 */		Options opts=new Options();		//设置仅加载位图边界信息(相当于位图的信息,但没有加载位图)		opts.inJustDecodeBounds=true;		//加载指定路径图片的边界信息,保存到opts中		BitmapFactory.decodeFile("/mnt/sdcard/dog.jpg", opts);		//计算收缩比例		int xScale=opts.outWidth/200;		int yScale=opts.outHeight/200;		opts.inSampleSize=xScale>yScale?xScale:yScale;		//设置加载边界信息为false		opts.inJustDecodeBounds=false;		Bitmap bm=BitmapFactory.decodeFile("/mnt/sdcard/dog.jpg", opts);		imageView.setImageBitmap(bm);				/**		 * 4.【加载位图】通过输入流加载位图		 */				/*FileInputStream is;		try {			is = new FileInputStream("/mnt/sdcard/dog.jpg");			Bitmap bm=BitmapFactory.decodeStream(is);			imageView.setImageBitmap(bm);					} catch (FileNotFoundException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}*/				/**		 * 5.【加载位图】通过字节数组加载位图,图片长,宽减半;		 */		//从网络上读取图片的字符串数组(输入流)		//字节数组输出流		/*ByteArrayOutputStream out;		try {			FileInputStream fis=new FileInputStream("/mnt/sdcard/dog.jpg");			BufferedInputStream bis=new BufferedInputStream(fis);			out = new ByteArrayOutputStream();			int hasRead=0;			byte[] buffer=new byte[1024*2];			while((hasRead=bis.read(buffer))>0){				//读出多少数据,向输出流中写入多少				out.write(buffer);				out.flush();			}			out.close();			fis.close();			bis.close();			byte[] data=out.toByteArray();			//长宽减半			Options opts=new Options();			opts.inSampleSize=2;			Bitmap bm=BitmapFactory.decodeByteArray(data, 0, data.length, opts);			imageView.setImageBitmap(bm);					} catch (FileNotFoundException e) {			// TODO Auto-generated catch block			e.printStackTrace();		} catch (IOException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}*/	}																}


  相关解决方案