|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
CoreAnimation---制作动画很强大很喜欢的框架可以用少量的代码写出漂亮的动画CQuartz2D---强大的2D绘图库COpenGL---不用介绍了超级强大的3D库CCoreImage---在android使用开辟的时分,加载收集图片是一个十分主要的部分,良多图片不成能放在当地,以是就必需要从服务器大概收集读取图片。
软援用是一个如今十分盛行的办法,用户体验对照好,不必每次都必要从收集下载图片,假如下载后就存到当地,下次读取时起首检察当地有无,假如没有再从收集读取。
记得2月份在和爱奇艺公司的项目总监一同弄联通的OTT盒子的时分他就提了一下软援用,奇艺做的手机客户端就是接纳这类办法,以是你会发明奇艺客户端占用很年夜的空间,上面就分享一下异步加载收集图片的办法吧。
FileCache.java
- importjava.io.File;
- importandroid.content.Context;
- publicclassFileCache{
- privateFilecacheDir;
- publicFileCache(Contextcontext){
- //找一个用来缓存图片的路径
- if(android.os.Environment.getExternalStorageState().equals(
- android.os.Environment.MEDIA_MOUNTED))
- cacheDir=newFile(android.os.Environment.getExternalStorageDirectory(),
- "文件夹称号");
- else
- cacheDir=context.getCacheDir();
- if(!cacheDir.exists())
- cacheDir.mkdirs();
- }
- publicFilegetFile(Stringurl){
- Stringfilename=String.valueOf(url.hashCode());
- Filef=newFile(cacheDir,filename);
- returnf;
- }
- publicvoidclear(){
- File[]files=cacheDir.listFiles();
- if(files==null)
- return;
- for(Filef:files)
- f.delete();
- }
- }
HttpUtil.java
- importjava.io.ByteArrayOutputStream;
- importjava.io.File;
- importjava.io.FileNotFoundException;
- importjava.io.FileOutputStream;
- importjava.io.IOException;
- importjava.io.InputStream;
- importjava.io.OutputStream;
- importjava.io.UnsupportedEncodingException;
- importjava.net.HttpURLConnection;
- importjava.net.MalformedURLException;
- importjava.net.ProtocolException;
- importjava.net.URL;
- importjava.net.URLEncoder;
- importjava.util.Map;
- /**
- *Http哀求工具类
- *
- *@authorScorpio.Liu
- *
- */
- publicclassHttpUtil{
- /**
- *猎取呼应字符串
- *
- *@parampath
- *路径
- *@paramparameters
- *参数
- *@return呼应字符串
- */
- publicstaticStringgetResponseStr(Stringpath,Map<String,String>parameters){
- StringBufferbuffer=newStringBuffer();
- URLurl;
- try{
- if(parameters!=null&&!parameters.isEmpty()){
- for(Map.Entry<String,String>entry:parameters.entrySet()){
- //完成转码操纵
- buffer.append(entry.getKey()).append("=")
- .append(URLEncoder.encode(entry.getValue(),"UTF-8")).append("&");
- }
- buffer.deleteCharAt(buffer.length()-1);
- }
- url=newURL(path);
- HttpURLConnectionurlConnection=(HttpURLConnection)url.openConnection();
- urlConnection.setConnectTimeout(3000);
- urlConnection.setRequestMethod("POST");
- urlConnection.setDoInput(true);//暗示从服务器猎取数据
- urlConnection.setDoOutput(true);//暗示向服务器写数据
- //取得上传信息的字节巨细和长度
- byte[]mydata=buffer.toString().getBytes();
- //暗示设置哀求体的范例是文本范例
- urlConnection.setRequestProperty("Content-Type",
- "application/x-www-form-urlencoded");
- urlConnection.setRequestProperty("Content-Length",String.valueOf(mydata.length));
- //取得输入流,向服务器输入数据
- OutputStreamoutputStream=urlConnection.getOutputStream();
- outputStream.write(mydata,0,mydata.length);
- outputStream.close();
- intresponseCode=urlConnection.getResponseCode();
- if(responseCode==200){
- returnchangeInputStream(urlConnection.getInputStream());
- }
- }catch(UnsupportedEncodingExceptione){
- e.printStackTrace();
- }catch(MalformedURLExceptione){
- e.printStackTrace();
- }catch(ProtocolExceptione){
- e.printStackTrace();
- }catch(IOExceptione){
- e.printStackTrace();
- }
- returnnull;
- }
- privatestaticStringchangeInputStream(InputStreaminputStream){
- ByteArrayOutputStreamoutputStream=newByteArrayOutputStream();
- byte[]data=newbyte[1024];
- intlen=0;
- Stringresult="";
- if(inputStream!=null){
- try{
- while((len=inputStream.read(data))!=-1){
- outputStream.write(data,0,len);
- }
- result=newString(outputStream.toByteArray(),"UTF-8");
- }catch(IOExceptione){
- e.printStackTrace();
- }
- }
- returnresult;
- }
- publicstaticInputStreamgetInputStream(Stringpath){
- URLurl;
- try{
- url=newURL(path);
- HttpURLConnectionurlConnection=(HttpURLConnection)url.openConnection();
- urlConnection.setConnectTimeout(3000);
- urlConnection.setRequestMethod("GET");
- urlConnection.setDoInput(true);//暗示从服务器猎取数据
- urlConnection.connect();
- if(urlConnection.getResponseCode()==200)
- returnurlConnection.getInputStream();
- }catch(MalformedURLExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }catch(Exceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- returnnull;
- }
- publicstaticbyte[]readStream(InputStreaminStream)throwsException{
- ByteArrayOutputStreamoutSteam=newByteArrayOutputStream();
- byte[]buffer=newbyte[1024];
- intlen=-1;
- while((len=inStream.read(buffer))!=-1){
- outSteam.write(buffer,0,len);
- }
- outSteam.close();
- inStream.close();
- returnoutSteam.toByteArray();
- }
- publicstaticvoidCopyStream(Stringurl,Filef){
- FileOutputStreamfileOutputStream=null;
- InputStreaminputStream=null;
- try{
- inputStream=getInputStream(url);
- byte[]data=newbyte[1024];
- intlen=0;
- fileOutputStream=newFileOutputStream(f);
- while((len=inputStream.read(data))!=-1){
- fileOutputStream.write(data,0,len);
- }
- }catch(FileNotFoundExceptione){
- e.printStackTrace();
- }catch(IOExceptione){
- e.printStackTrace();
- }finally{
- if(inputStream!=null){
- try{
- inputStream.close();
- }catch(IOExceptione){
- e.printStackTrace();
- }
- }
- if(fileOutputStream!=null){
- try{
- fileOutputStream.close();
- }catch(IOExceptione){
- e.printStackTrace();
- }
- }
- }
- }
- }
MemoryCache.java
- importjava.lang.ref.SoftReference;
- importjava.util.Collections;
- importjava.util.HashMap;
- importjava.util.Map;
- importandroid.graphics.Bitmap;
- publicclassMemoryCache{
- privateMap<String,SoftReference<Bitmap>>cache=Collections
- .synchronizedMap(newHashMap<String,SoftReference<Bitmap>>());//软援用
- publicBitmapget(Stringid){
- if(!cache.containsKey(id))
- returnnull;
- SoftReference<Bitmap>ref=cache.get(id);
- returnref.get();
- }
- publicvoidput(Stringid,Bitmapbitmap){
- cache.put(id,newSoftReference<Bitmap>(bitmap));
- }
- publicvoidclear(){
- cache.clear();
- }
- }
ImageLoader.java
- importjava.io.File;
- importjava.io.FileInputStream;
- importjava.io.FileNotFoundException;
- importjava.io.UnsupportedEncodingException;
- importjava.net.URLEncoder;
- importjava.util.Collections;
- importjava.util.Map;
- importjava.util.WeakHashMap;
- importjava.util.concurrent.ExecutorService;
- importjava.util.concurrent.Executors;
- importandroid.app.Activity;
- importandroid.content.Context;
- importandroid.graphics.Bitmap;
- importandroid.graphics.BitmapFactory;
- importandroid.graphics.drawable.BitmapDrawable;
- importandroid.widget.ImageView;
- publicclassImageLoader{
- privateMemoryCachememoryCache=newMemoryCache();
- privateFileCachefileCache;
- privateMap<ImageView,String>imageViews=Collections
- .synchronizedMap(newWeakHashMap<ImageView,String>());
- privateExecutorServiceexecutorService;
- privatebooleanisSrc;
- /**
- *@paramcontext
- *高低文对象
- *@paramflag
- *true为source资本,false为background资本
- */
- publicImageLoader(Contextcontext,booleanflag){
- fileCache=newFileCache(context);
- executorService=Executors.newFixedThreadPool(5);
- isSrc=flag;
- }
- finalintstub_id=R.drawable.ic_launcher;
- publicvoidDisplayImage(Stringurl,ImageViewimageView){
- Stringu1=url.substring(0,url.lastIndexOf("/")+1);
- Stringu2=url.substring(url.lastIndexOf("/")+1);
- try{
- u2=URLEncoder.encode(u2,"UTF-8");
- }catch(UnsupportedEncodingExceptione){
- e.printStackTrace();
- }
- url=u1+u2;
- imageViews.put(imageView,url);
- Bitmapbitmap=memoryCache.get(url);
- if(bitmap!=null){
- if(isSrc)
- imageView.setImageBitmap(bitmap);
- else
- imageView.setBackgroundDrawable(newBitmapDrawable(bitmap));
- }else{
- queuePhoto(url,imageView);
- if(isSrc)
- imageView.setImageResource(stub_id);
- else
- imageView.setBackgroundResource(stub_id);
- }
- }
- privatevoidqueuePhoto(Stringurl,ImageViewimageView){
- PhotoToLoadp=newPhotoToLoad(url,imageView);
- executorService.submit(newPhotosLoader(p));
- }
- privateBitmapgetBitmap(Stringurl){
- try{
- Filef=fileCache.getFile(url);
- //从sd卡
- Bitmapb=onDecodeFile(f);
- if(b!=null)
- returnb;
- //从收集
- Bitmapbitmap=null;
- System.out.println("ImageLoader-->download");
- HttpUtil.CopyStream(url,f);
- bitmap=onDecodeFile(f);
- returnbitmap;
- }catch(Exceptionex){
- ex.printStackTrace();
- returnnull;
- }
- }
- publicBitmaponDecodeFile(Filef){
- try{
- returnBitmapFactory.decodeStream(newFileInputStream(f));
- }catch(FileNotFoundExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- returnnull;
- }
- /**
- *解码图象用来削减内存损耗
- *
- *@paramf
- *@return
- */
- publicBitmapdecodeFile(Filef){
- try{
- //解码图象巨细
- BitmapFactory.Optionso=newBitmapFactory.Options();
- o.inJustDecodeBounds=true;
- BitmapFactory.decodeStream(newFileInputStream(f),null,o);
- //找到准确的刻度值,它应当是2的幂。
- finalintREQUIRED_SIZE=70;
- intwidth_tmp=o.outWidth,height_tmp=o.outHeight;
- intscale=1;
- while(true){
- if(width_tmp/2<REQUIRED_SIZE||height_tmp/2<REQUIRED_SIZE)
- break;
- width_tmp/=2;
- height_tmp/=2;
- scale*=2;
- }
- BitmapFactory.Optionso2=newBitmapFactory.Options();
- o2.inSampleSize=scale;
- returnBitmapFactory.decodeStream(newFileInputStream(f),null,o2);
- }catch(FileNotFoundExceptione){
- }
- returnnull;
- }
- /**
- *义务行列
- *
- *@authorScorpio.Liu
- *
- */
- privateclassPhotoToLoad{
- publicStringurl;
- publicImageViewimageView;
- publicPhotoToLoad(Stringu,ImageViewi){
- url=u;
- imageView=i;
- }
- }
- classPhotosLoaderimplementsRunnable{
- PhotoToLoadphotoToLoad;
- PhotosLoader(PhotoToLoadphotoToLoad){
- this.photoToLoad=photoToLoad;
- }
- @Override
- publicvoidrun(){
- if(imageViewReused(photoToLoad))
- return;
- Bitmapbmp=getBitmap(photoToLoad.url);
- memoryCache.put(photoToLoad.url,bmp);
- if(imageViewReused(photoToLoad))
- return;
- BitmapDisplayerbd=newBitmapDisplayer(bmp,photoToLoad);
- Activitya=(Activity)photoToLoad.imageView.getContext();
- a.runOnUiThread(bd);
- }
- }
- booleanimageViewReused(PhotoToLoadphotoToLoad){
- Stringtag=imageViews.get(photoToLoad.imageView);
- if(tag==null||!tag.equals(photoToLoad.url))
- returntrue;
- returnfalse;
- }
- /**
- *显现位图在UI线程
- *
- *@authorScorpio.Liu
- *
- */
- classBitmapDisplayerimplementsRunnable{
- Bitmapbitmap;
- PhotoToLoadphotoToLoad;
- publicBitmapDisplayer(Bitmapb,PhotoToLoadp){
- bitmap=b;
- photoToLoad=p;
- }
- publicvoidrun(){
- if(imageViewReused(photoToLoad))
- return;
- if(bitmap!=null){
- if(isSrc)
- photoToLoad.imageView.setImageBitmap(bitmap);
- else
- photoToLoad.imageView.setBackgroundDrawable(newBitmapDrawable(bitmap));
- }else{
- if(isSrc)
- photoToLoad.imageView.setImageResource(stub_id);
- else
- photoToLoad.imageView.setBackgroundResource(stub_id);
- }
- }
- }
- publicvoidclearCache(){
- memoryCache.clear();
- fileCache.clear();
- }
- }
利用的时分用ImageLoader这个类就ok了,很便利
最重要的就是UINavigationController他是一层一层推进view的打开iPhone里的联系人每点一个联系人屏幕就会像右推到下一个界面这就是UINavigationController在做的事UINavigationController |
|