MasterofProject

Using Android open source library android-gif-drawable loaded GIF format pictures

Label Android Developmentpictureframe
329 people read comment(1) Collection Report
Classification:

In the Android project, the most used is the PNG format of the picture, or with the JPEG picture. That we want to use the animation type picture GIF format picture should how to do? We can use the android-gif-drawable framework to achieve GIF image loading, the following directly attached to the tools I used in the project:

Class GifLoader public {
Save the picture / * * Map*/ quoted
Static Map<ImageView String>, mImageViewMap public = Collections.synchronizedMap (HashMap<ImageView new, String> ());
ExecutorService executorService private;
The cache size is 10MiB*/ / * *
Static int mMemCacheMaxSize private = 10 * 1024 * 1024;
/**LruCache cache picture * /
Static LruCache<String byte[]>, mMemLruCache private;
* / / * * version number
Static int mAppVersion private = 1;
Disk cache / * * 50M*/
Static int mDiskCacheMaxSize private = 50 * 1024 * 1024;
The hard disk cache object * / / * *
Static DiskLruCache mDiskLruCache private;
Do you want to initialize * / / * *
Static Boolean mCacheInit private = false;
Static final int DISK_CACHE_COUNT private = 1;
The /**GifLoader object.
Static GifLoader loader private;
A picture of the id*/ / * * default
Int default_image_id final = R.drawable.icon_app_normal;


Construct object * / / * *
GifLoader private (context Context) {
ExecutorService = Executors.newFixedThreadPool (2);
InitCaches (context);
}


The singleton / * * * /
Synchronized static GifLoader getInstance (context Context) {public
If (loader = = null) {
Loader = GifLoader new (context);
}
Loader return;
}


Show the pictures on the control / * * * /
Void displayImage public (URL String, imageView GifImageView, isGif Boolean) {
{try
If (File.Exists (URL) new ()) {
ImageView.setImageDrawable (GifDrawable new (URL));
Return;
}
}
Catch (E Exception) {
}


MImageViewMap.put (imageView, URL);
Data byte[] = mMemLruCache.get (URL);
If (data! = null) {
{try
ImageView.setImageDrawable (GifDrawable new (data));
}
Catch (E Exception) {
E.printStackTrace ();
ImageView.setImageResource (default_image_id);
}
}
{else
QueuePhoto (URL, imageView);
ImageView.setImageResource (default_image_id);
}


}


Void queuePhoto private (URL String, imageView GifImageView) {
PhotoToLoad PhotoToLoad = PhotoToLoad new (URL, imageView);
ExecutorService.submit (PhotosLoader new (photoToLoad));
}


This method is to be optimized to prevent / * * memory overflow first read from the file, if not to download.
Byte[] getBitmap private (URL String) {
CacheEntry Snapshot = null;
{try
CacheEntry = mDiskLruCache.get (CacheHelper.UriToDiskLruCacheString (URL));
}
Catch (E Exception) {
E.printStackTrace ();
}


Image byte[] = null;


If (cacheEntry! = null) {
Image = inputStreamToByteArray (cacheEntry.getInputStream (0) (), (int) cacheEntry.getLength (0);
MMemLruCache.put (URL, image);
}
{try
If (image! = null) {


Image return;
}
{else
ImageUrl URL = URL new (URL);
Con HttpURLConnection = (HttpURLConnection) imageUrl.openConnection ();
Con.setConnectTimeout (30000);
Con.setReadTimeout (30000);
Con.setInstanceFollowRedirects (true);
Is InputStream = con.getInputStream ();
Image = inputStreamToByteArray (is, 8096);
If (image! = null) {


{try
Editor Editor = mDiskLruCache.edit (CacheHelper.UriToDiskLruCacheString (URL));
If (editor! = null) {
If (CacheHelper.writeByteArrayToEditor (image, editor)) {
MDiskLruCache.flush ();
Editor.commit ();
}
{else
Editor.abort ();
}
}
}
Catch (E Exception) {
E.printStackTrace ();
}


MMemLruCache.put (URL, image);
}
}


}
Catch (E FileNotFoundException) {
E.printStackTrace ();
}
Catch (E MalformedURLException) {
E.printStackTrace ();
}
Catch (E IOException) {
E.printStackTrace ();
}


Image return;
}


photosloader民营类实现Runnable {
私人phototoload phototoload;


公共photosloader(phototoload phototoload){
super();
this.phototoload = phototoload;
}


“重写”
run() { public void
/ * * /下载前检查ImageView是否被复用
如果(imageviewreused(phototoload)){ return;}
字节[] BM = getbitmap(phototoload。URL);


/ * * /下载完毕后再次检查ImageView是否被复用
如果(imageviewreused(phototoload)){ return;}
displayimagerunnable displayimagerunnable =新displayimagerunnable(BM,phototoload);
活动=(活动)phototoload。getcontext() ImageView;
a.runonuithread(displayimagerunnable);


}


}


布尔imageviewreused(phototoload phototoload){
字符串标签= mimageviewmap。得到(phototoload。ImageView);
/ * *代表imageviews地图中存放的ImageView对应的价值值已经被覆盖掉,也就是重用了*/
如果(= = null | |标签!标签。等于(phototoload。URL)){
返回真;
}
否则{
返回假;
}


}


displayimagerunnable民营类实现Runnable {
私人字节数据;
私人phototoload phototoload;


公共displayimagerunnable(byte []数据,phototoload phototoload){
super();
this.data =数据;
this.phototoload = phototoload;
}


“重写”
run() { public void
如果(imageviewreused(phototoload)){ return;}
如果(数据!=空){
尝试{
phototoload。ImageView。setimagedrawable(新gifdrawable(数据));
}
(例外){
E. printstacktrace();
phototoload。ImageView。setimageresource(default_image_id);
}
}
否则{
phototoload。ImageView。setimageresource(default_image_id);
}


}
}


私有类phototoload {
公共字符串网址;
公共gifimageview ImageView;


公共phototoload(字符串的URL,gifimageview ImageView){
super();
this.url =网址;
this.imageview = ImageView;
}


}


private void initcaches(语境){
如果(!mcacheinit){
mmemlrucache =新LruCache <字符串,字节[ ] >(mmemcachemaxsize){
保护国际(关键字符串的字节大小,[ ]值){
返回value.length;
}
};
文件diskcachedir = cachehelper。getdiskcachedir(上下文,“ImageCache”);
尝试{
mdisklrucache = disklrucache。开放(diskcachedir,mappversion,disk_cache_count,mdiskcachemaxsize);
}
赶上(IOException忽略){
}
mcacheinit =真;
}
}


专用字节[] inputstreamtobytearray(InputStream,int的大小){
=新bytearrayoutputstream()缓冲区写入字节数组的输出流;
字节缓冲=新的字节大小;


int len = 0;
尝试{
而((len =是。读(缓冲区))!= - 1){
写缓冲区(缓冲区。,0,Len);
}
}
赶上(IOException e){
E. printstacktrace();
}


tobytearray()缓冲=字节缓冲区;
返回缓冲区;
}
}

猜你在找
查看评论
*以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:42986次
    • 积分:二千一百一十五
    • 等级:
    • 排名:10800名第
    • 原创:174篇
    • 转载:37篇
    • 译文:1篇
    • 评论:9条
    文章分类
    文章存档
    Latest comments