2016年4月19日 星期二

Android - Universal-Image-Loader 加載網路圖片Lib

提供一個常用的第三方加載網路圖片的Lib 目前主流的好像是Universal-Image-Loader、Picasso、fresco,這幾個Lib都還蠻多人用的 這篇主要是介紹Universal-Image-Loader 簡單來說最簡單、支援度也很廣、又容易客製化,而且可以做Local端暫存 在開始前記得先把Jar檔import到專案裡 接著非常的簡單 基本上你什麼都不用設定就可以直接開始用了 因為套件本身有預設值
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//圖片網址
String mStrPicUrl="http://aaa.bbb.ccc/1234.jpg";

//ImageView呈現的元件
ImageView mImgView=findViewById(R.id.imageview); 

//使用方式,一般加載圖片
ImageLoader.getInstance().displayImage(mStrPicUrl,mImgView);
//使用方式,加載圖片,並加入顯示設定,在最下面會補充說明
ImageLoader.getInstance().displayImage(pic,mImgView,displayImageOptions );



//使用方式,加載圖片,並加入顯示設定及CallBack事件
ImageLoader.getInstance().displayImage(pic,mImgView,getImageLoaderOptions(), new ImageLoadingListener() {

                @Override
                public void onLoadingStarted(String s, View view) {
                    //開始loading時
                }

                @Override
                public void onLoadingFailed(String s, View view, FailReason failReason) {
                    //loading失敗時

}

                @Override
                public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                   //loading完成時

}

                @Override
                public void onLoadingCancelled(String s, View view) {
                   //loading取消時
                }

            });
當然除了以上方法,還有其他呈現方式的組合,可以自行多嘗試 那如果想要在背景抓圖,不呈現在畫面 可以使用 //直接抓圖,但不顯示在畫面上,getImageLoaderOptionsWithLoading請參考最下面
ImageLoader.getInstance().loadImage(mStrPicUrl, getImageLoaderOptions(), callBackListenrs);
如果要進行客製化,其實步驟也很簡單 首先要有個Class,去繼承Application(相關用法及設定請自行google) 然後再該Class去Override繼承至Application的onCreate事件 並且在這邊去做一些Image-Loader的客製化設定
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class MyApplication extends Application {
    private static Context mContext;
    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
        initImageLoader();
  }

    private void initImageLoader() {
        ImageLoaderConfiguration config = new ImageLoaderConfiguration
                .Builder(mContext)
                .threadPoolSize(5) //執行序數,通常用預設就好
                .threadPriority(Thread.NORM_PRIORITY - 2) //執行續優先權
                .memoryCache(new LruMemoryCache(1 * 1024 * 1024)) 
                .memoryCacheSize(1 * 1024 * 1024) //記憶體暫存大小
                .discCacheSize(10 * 1024 * 1024) //儲存空間暫存大小
                .discCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO) //佇列執行方式,預設是後進先出
//                .discCacheFileCount(100) //暫存檔案數量,如果需要設定檔案數量的話再設定
                .diskCache(new UnlimitedDiskCache(mContext.getCacheDir())) //設定暫存數量無上限
                .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) //預設的顯示圖片選項,可以再額外做設定,下面會補充
                .imageDownloader(new BaseImageDownloader(mContext, 5 * 1000, 30 * 1000)) //從網路抓取圖片時,timeout相關設定
                .build();
        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config);

    }

}
詳細設定可以參考這篇文章 大陸人寫的,有更詳細的設定 http://www.eoeandroid.com/thread-318344-1-1.html 另外補充加載圖片時的顯示選項
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public static DisplayImageOptions getImageLoaderOptionsWithLoading() {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.icon_loading) //開始Loading時要呈現的圖片
                .showImageForEmptyUri(R.mipmap.icon_is_empty) //圖片來源是null時要呈現的圖片
                .showImageOnFail(R.mipmap.icon_loading_fail)//load圖失敗時要呈現的圖片
//.displayer(new FadeInBitmapDisplayer(300)) //顯示時加入動畫效果,範例是指0.3秒淡入
                .cacheOnDisc(true) //是否暫存到本機端
                .cacheInMemory(true) //是否暫存到快取記憶體
                .imageScaleType(ImageScaleType.EXACTLY) //圖片呈現方式(會影響效能)
                .bitmapConfig(Bitmap.Config.RGB_565) //圖片品質 (會影響效能)
                .considerExifParams(true) //自動判斷圖片方向,並修正方向有問題的圖片
//                .resetViewBeforeLoading(true)  //是否再載入圖片前,把imageview元件重設(通常是listview在Reuse時會用到)
                .build();

        return options;
    }
以上大概是這樣 寫得很冗長,有問題可以再詢問哦!

2016年4月15日 星期五

線上表格產生器

線上表格產生器:http://www.tablesgenerator.com/

PIVOT Table 統計每月生日人數

SQL:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
SELECT *
FROM (
 SELECT 
 DATEPART(MONTH,[Birthday]) AS [月份],
 count(DATEPART(MONTH,[Birthday])) AS 人數
 FROM [Employees]
 GROUP BY DATEPART(MONTH,[Birthday])
 ) AS Layer1
PIVOT(
 MAX(人數) 
 FOR 月份 
 IN ([01], [02], [03], [04], [05], [06], [07], [08], [09], [10], [11], [12])
) AS PivotTable


執行結果:

01 02 03 04 05 06 07 08 09 10 11 12
11 3 11 8 10 10 9 18 10 11 16 10