kimi
2021-02-18 0ac056bb5b4c567293482286c80a1b83a467cd33
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.lqr.emoji;
 
import android.content.Context;
import android.content.res.AssetManager;
import android.util.DisplayMetrics;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
 
/**
 * CSDN_LQR
 * 表情库Kit
 */
 
public class LQREmotionKit {
 
    private static String STICKER_NAME_IN_ASSETS = "sticker";
    private static Context mContext;
    private static float density;
    private static float scaleDensity;
    private static String STICKER_PATH;//默认路径在 /data/data/包名/files/sticker 下
    private static IImageLoader imageLoader;
 
    private static void getAndSaveParameter(Context context) {
        mContext = context.getApplicationContext();
 
        DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics();
        density = dm.density;
        scaleDensity = dm.scaledDensity;
    }
 
    public static void init(Context context, String stickerPath) {
        getAndSaveParameter(context);
        STICKER_PATH = stickerPath;
 
        //将asset/sticker目录下默认的贴图复制到STICKER_PATH下
        copyStickerToStickerPath(STICKER_NAME_IN_ASSETS);
    }
 
    public static void init(Context context) {
        init(context, new File(context.getFilesDir(), STICKER_NAME_IN_ASSETS).getAbsolutePath());
    }
 
    public static void init(Context context, IImageLoader imageLoader) {
        init(context);
        setImageLoader(imageLoader);
    }
 
    public static void init(Context context, String stickerPath, IImageLoader imageLoader) {
        init(context, stickerPath);
        setImageLoader(imageLoader);
    }
 
    private static void copyStickerToStickerPath(String assetsFolderPath) {
        AssetManager assetManager = mContext.getResources().getAssets();
        List<String> srcFile = new ArrayList<>();
        try {
            String[] stickers = assetManager.list(assetsFolderPath);
            for (String fileName : stickers) {
                if (!new File(LQREmotionKit.getStickerPath(), fileName).exists()) {
                    srcFile.add(fileName);
                }
            }
            if (srcFile.size() > 0) {
                copyToStickerPath(assetsFolderPath, srcFile);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    private static void copyToStickerPath(final String assetsFolderPath, final List<String> srcFile) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                AssetManager assetManager = mContext.getResources().getAssets();
                for (String fileName : srcFile) {
 
                    if (fileName.contains(".")) {//文件
                        InputStream is = null;
                        FileOutputStream fos = null;
                        try {
                            is = assetManager.open(assetsFolderPath + File.separator + fileName);
                            File destinationFile;
                            if (assetsFolderPath.startsWith(STICKER_NAME_IN_ASSETS + File.separator)) {//递归回来的时候assetsFolderPath可能变为"sticker/tsj"
                                destinationFile = new File(getStickerPath(), assetsFolderPath.substring(assetsFolderPath.indexOf(File.separator) + 1) + File.separator + fileName);
                            } else {
                                destinationFile = new File(getStickerPath(), fileName);
                            }
                            fos = new FileOutputStream(destinationFile);
                            byte[] buffer = new byte[1024];
                            int len;
                            while ((len = is.read(buffer)) != -1) {
                                fos.write(buffer, 0, len);
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            if (is != null) {
                                try {
                                    is.close();
                                    is = null;
                                } catch (IOException e) {
                                    e.printStackTrace();
                                    is = null;
                                }
                            }
                            if (fos != null) {
                                try {
                                    fos.close();
                                    fos = null;
                                } catch (IOException e) {
                                    e.printStackTrace();
                                    fos = null;
                                }
                            }
                        }
                    } else {//文件夹
 
                        File dir = new File(getStickerPath(), fileName);
                        if (!dir.exists()) {
                            dir.mkdirs();
                        }
 
                        copyStickerToStickerPath(assetsFolderPath + File.separator + fileName);
                    }
                }
            }
        }).start();
    }
 
    public static Context getContext() {
        return mContext;
    }
 
    public static String getStickerPath() {
        return STICKER_PATH;
    }
 
    public static IImageLoader getImageLoader() {
        if (imageLoader == null) {
            throw new RuntimeException("you should use setImageLoader() in your App onCreate()");
        }
        return imageLoader;
    }
 
    public static void setImageLoader(IImageLoader imageLoader) {
        LQREmotionKit.imageLoader = imageLoader;
    }
 
//    public static void setStickerPath(String stickerPath) {
//        STICKER_PATH = stickerPath;
//    }
 
    public static int dip2px(float dipValue) {
        return (int) (dipValue * density + 0.5f);
    }
 
    public static int px2dip(float pxValue) {
        return (int) (pxValue / density + 0.5f);
    }
 
    public static int sp2px(float spValue) {
        return (int) (spValue * scaleDensity + 0.5f);
    }
}