本篇文章给大家分享的是有关如何提高Android从文件中读取图像的效率,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
方法一
start_time = System.currentTimeMillis();
BitmapFactory.Options options=new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bitmap=BitmapFactory.decodeFile(path,options);
options.inSampleSize=calculateSize(options,width,height);
options.inJustDecodeBounds=false;
//整个图像,下采样
bitmap=BitmapFactory.decodeFile(path,options);
//部分图像
Bitmap patch=Bitmap.createBitmap(bitmap, 10, 10, 100, 100);
end_time = System.currentTimeMillis();
Log.v("BitmapTest", "UI time consume:"+(end_time - start_time));
imageView.setImageBitmap(bitmap);
patchView.setImageBitmap(patch);
操作很简单,先将图片文件的尺寸等信息读取出来, 然后根据其尺寸计算其缩放比例,并将图片中的一部分剪切出来。最后将图片显示在ImageView空间上。大致测了几十次,得到的平均消耗时间为:72.75ms
方法二
启动子线程
start_time = System.currentTimeMillis();
String path=Environment.getExternalStorageDirectory().getPath()+File.separator+"image1.jpg";
ImgThread imgThread=new ImgThread(msgHandler,path,width,height);
imgThread.start();
子线程中的操作,与1基本相同
BitmapFactory.Options options=new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bitmap=BitmapFactory.decodeFile(path,options);
options.inSampleSize=calculateSize(options,width,height);
options.inJustDecodeBounds=false;
//整个图像,下采样
bitmap=BitmapFactory.decodeFile(path,options);
//部分图像
Bitmap patch=Bitmap.createBitmap(bitmap, 10, 10, 100, 100);
array=new ArrayList<Bitmap>(2);
array.add(bitmap);
array.add(patch);
//Serializable传递
Bundle bundle=new Bundle();
bundle.putSerializable("img", array);
//Parcelable传递
/*
MyList l=new MyList(Parcel.obtain());
l.array=array;
bundle.putParcelable("img", l);
*/
Message msg= new Message();
msg.what=1;
msg.setData(bundle);
handler.sendMessage(msg);
将Bitmap传回到UI线程并呈现
Bundle bundle=msg.getData();
//Serializable传递
ArrayList<Bitmap> array=(ArrayList<Bitmap>) bundle.getSerializable("img");
//Parcelable传递
//MyList l=(MyList)bundle.getParcelable("img");
//ArrayList<Bitmap> array=l.array;//=(ArrayList<Bitmap>) bundle.getParcelable("img");
Bitmap bitmap=array.get(0);
Bitmap patch=array.get(1);
end_time = System.currentTimeMillis();
Log.v("BitmapTest", "Th time consume:"+(end_time - start_time));
imageView.setImageBitmap(bitmap);
patchView.setImageBitmap(patch);
方法二的平均消耗时间为:83.93ms
方法三
该方法需要新建一个类用来实现Parcelable接口
package com.example.bitmaptest;
import java.util.ArrayList;
import android.os.Parcel;
import android.os.Parcelable;
public class MyList implements Parcelable{
public ArrayList array;
public MyList(Parcel in)
{
in.readValue(null);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(array);
}
public static final Parcelable.Creator<MyList> CREATOR = new Parcelable.Creator<MyList>() {
@Override
public MyList createFromParcel(Parcel source) {
return new MyList(source);
}
@Override
public MyList[] newArray(int size) {
return new MyList[size];
}
};
}
在子线程中的操作
//Parcelable传递
MyList l=new MyList(Parcel.obtain());
l.array=array;
bundle.putParcelable("img", l);
方法三的平均消耗时间为:87.35ms
结果分析
三种方法都是在魅族MX1型号的手机上测试的,理论上方法三应该比方法二快,但至少根据我的实验结果来看,在传送小数据量时(图像大概是几mB或几百kB),数据的传递耗时并不是关键,两种方法的耗时差不多。方法一由于没有使用线程间的数据传递,因此耗时是最少的。
因此,我总结得到如下结论:
1、如果必须等到图像加载完成才允许用户操作的这种场景,可以直接在UI线程做图像的操作,这时可以添加一个ProgressDialog用来提示正在加载。
2、如果需要一边允许用户操作一边加载图像的话,应该新开一个子线程,但是在数据量不大的情况下,Serializable和Parcelable差距不大。
3、总而言之,图像的尺寸和数量不大时,在UI线程直接做图像读取等操作即可,但比较大时还是最好开个子线程。
以上就是如何提高Android从文件中读取图像的效率,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。