如何提高Android从文件中读取图像的效率

  介绍

本篇文章给大家分享的是有关如何提高Android从文件中读取图像的效率,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

<强>方法一

start_time =, System.currentTimeMillis ();   ,,,,,   ,,,,,BitmapFactory.Options 选项=new  BitmapFactory.Options ();   ,,,,,options.inJustDecodeBounds =,真的;   ,,,,,Bitmap 位图=BitmapFactory.decodeFile(路径,选择);   ,,,,,options.inSampleSize=calculateSize(选项,宽度、高度);   ,,,,,options.inJustDecodeBounds=false;   ,,,,,//整个图像,下采样   ,,,,,位图=BitmapFactory.decodeFile(路径,选择);   ,,,,,//部分图像   ,,,,,Bitmap 补?Bitmap.createBitmap(位图,,,,,,100,,100);   ,,,,,   ,,,,,end_time =, System.currentTimeMillis ();   ,,,,,Log.v (“BitmapTest",,“UI  time 消费:“+ (end_time 作用;start_time));   ,,,,,imageView.setImageBitmap(位图);   ,,,,,patchView.setImageBitmap(补丁);

操作很简单,先将图片文件的尺寸等信息读取出来,然后根据其尺寸计算其缩放比例,并将图片中的一部分剪切出来。最后将图片显示在ImageView空间上。大致测了几十次,得到的平均消耗时间为:72.75毫秒

<强>方法二

启动子线程

start_time =, System.currentTimeMillis ();   .getPath String 路径=Environment.getExternalStorageDirectory () () + File.separator +“image1.jpg";   ImgThread  imgThread=new  imgThread (msgHandler、路径、宽度、高度);   imgThread.start ();

子线程中的操作,与1基本相同

BitmapFactory.Options 选项=new  BitmapFactory.Options ();   ,,,options.inJustDecodeBounds =,真的;   ,,,Bitmap 位图=BitmapFactory.decodeFile(路径,选择);   ,,,options.inSampleSize=calculateSize(选项,宽度、高度);   ,,,options.inJustDecodeBounds=false;   ,,,//整个图像,下采样   ,,,位图=BitmapFactory.decodeFile(路径,选择);   ,,,//部分图像   ,,,Bitmap 补?Bitmap.createBitmap(位图,,,,,100,,100);   数组,,,=new  ArrayList (2);   ,,,array.add(位图);   ,,,array.add(补丁);   ,,,//序列化传递   ,,,Bundle 包=new 包(),,,,,   ,,,bundle.putSerializable (“img",数组);   ,,,//Parcelable传递   ,,/*   ,,,MyList  l=new  MyList (Parcel.obtain ());   ,,,l.array=数组;   ,,,bundle.putParcelable (“img", l);   ,,*/,,,Message 味精=,new 消息();   ,,,msg.what=1;   ,,,msg.setData(包);   ,,,handler.sendMessage(味精);

将位图传回到UI线程并呈现

Bundle 包=msg.getData ();   ,,,,,,,,,//序列化传递   ,,,,,,,,,ArrayList,数组=(ArrayList,数组=l.array;//=(ArrayList

方法二的平均消耗时间为:83.93毫秒

<强>方法三

该方法需要新建一个类用来实现Parcelable接口

package  com.example.bitmaptest;      import  java.util.ArrayList;      import  android.os.Parcel;   import  android.os.Parcelable;      public  class  MyList  implements  Parcelable {      public 才能ArrayList 数组;   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

如何提高Android从文件中读取图像的效率