HDL_Widget Android和iOS精简版的库,去掉高德SDK,和iOS裁剪方法
JLChen
2021-01-08 07a4e46efd0d180f881a9761c6f737e5d1c47848
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package com.hdl.widget;
 
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.support.annotation.NonNull;
import android.util.Log;
import java.util.List;
 
/**
 * Created by JLChen on 2019/9/23
 */
public final class HDLUtlisXM {
    public static final int DEFAULT_OFFLINE_COLOR = 0xC8E9E9EC;
    public static String MAuthority_NAME = "com.hdl.widgetxm.fileprovider";
    public static final String CROP_TYPE_KEY = "Type";
    public static final String CROP_NAME_KEY = "FileName";
    public static final String CROP_RATIO_X_KEY = "ImageRatioX";
    public static final String CROP_RATIO_Y_KEY = "ImageRatioY";
    public static final String CROP_OUTPUT_Y_KEY = "ImageUotputY";
 
 
    private HDLUtlisXM() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }
 
 
    /**
     * dp转px
     *
     * @param dpValue dp值
     * @return px值
     */
    public static int dp2px(Context context, final float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
 
    /**
     * px转dp
     *
     * @param pxValue px值
     * @return dp值
     */
    public static int px2dp(Context context, final float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
 
    /**
     * Relaunch the application.
     */
    public static void relaunchApp(Context mContext) {
        relaunchApp(mContext, false);
    }
 
    /**
     * Relaunch the application.
     *
     * @param isKillProcess True to kill the process, false otherwise.
     */
    public static void relaunchApp(Context mContext, final boolean isKillProcess) {
        Intent intent = getLaunchAppIntent(mContext, mContext.getPackageName(), true);
        if (intent == null) {
            Log.e("AppUtils", "Didn't exist launcher activity.");
            return;
        }
        intent.addFlags(
                Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK
        );
        mContext.startActivity(intent);
        if (!isKillProcess) return;
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }
 
    private static Intent getLaunchAppIntent(Context mContext, final String packageName) {
        return getLaunchAppIntent(mContext, packageName, false);
    }
 
    private static Intent getLaunchAppIntent(Context mContext, final String packageName, final boolean isNewTask) {
        String launcherActivity = getLauncherActivity(mContext, packageName);
        if (!launcherActivity.isEmpty()) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            ComponentName cn = new ComponentName(packageName, launcherActivity);
            intent.setComponent(cn);
            return isNewTask ? intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) : intent;
        }
        return null;
    }
 
    private static String getLauncherActivity(Context mContext, @NonNull final String pkg) {
        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setPackage(pkg);
        PackageManager pm = mContext.getPackageManager();
        List<ResolveInfo> info = pm.queryIntentActivities(intent, 0);
        int size = info.size();
        if (size == 0) return "";
        for (int i = 0; i < size; i++) {
            ResolveInfo ri = info.get(i);
            if (ri.activityInfo.processName.equals(pkg)) {
                return ri.activityInfo.name;
            }
        }
        return info.get(0).activityInfo.name;
    }
 
 
//    /**
//     * 生成二维码,默认大小为500*500
//     *
//     * @param url  需要生成二维码的网址,也可以是文字
//     * @return bitmap
//     */
//    public static Bitmap createQRCode(String url) {
//        return createQRCode(url, 500);
//    }
//
//    /**
//     * 生成二维码,默认大小为500*500
//     *
//     * @param url  需要生成二维码的网址,也可以是文字
//     * @param size 需要生成二维码的大小()
//     * @return bitmap
//     */
//    public static Bitmap createQRCode(String url, int size) {
//        try {
//            Hashtable<EncodeHintType, String> hints = new Hashtable<>();
//            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//            BitMatrix bitMatrix = new QRCodeWriter().encode(url,
//                    BarcodeFormat.QR_CODE, size, size, hints);
//            int[] pixels = new int[size * size];
//            for (int y = 0; y < size; y++) {
//                for (int x = 0; x < size; x++) {
//                    if (bitMatrix.get(x, y)) {
//                        pixels[y * size + x] = 0xff000000;
//                    } else {
//                        pixels[y * size + x] = 0xffffffff;
//                    }
//
//                }
//            }
//            Bitmap bitmap = Bitmap.createBitmap(size, size,
//                    Bitmap.Config.ARGB_8888);
//            bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
//            return bitmap;
//        } catch (WriterException e) {
//            e.printStackTrace();
//            return null;
//        }
//    }
//
//    /**
//     * @param url 需要生成二维码的文字、网址等
//     * @param size 需要生成二维码的大小()
//     * @param mBitmap logo文件
//     * @return bitmap
//     */
//    private static int IMAGE_HALFWIDTH = 50;//宽度值,影响中间图片大小
//
//    public static Bitmap createQRCodeWithLogo(String url, Bitmap mBitmap) {
//        return createQRCodeWithLogo(url, 500, mBitmap);
//    }
//
//    public static Bitmap createQRCodeWithLogo(String url, int size, Bitmap mBitmap) {
//        try {
//            IMAGE_HALFWIDTH = size / 10;
//            Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
//            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//            /*
//             * 设置容错级别,默认为ErrorCorrectionLevel.L
//             * 因为中间加入logo所以建议你把容错级别调至H,否则可能会出现识别不了
//             */
//            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//            BitMatrix bitMatrix = new QRCodeWriter().encode(url,
//                    BarcodeFormat.QR_CODE, size, size, hints);
//
//            int width = bitMatrix.getWidth();//矩阵高度
//            int height = bitMatrix.getHeight();//矩阵宽度
//            int halfW = width / 2;
//            int halfH = height / 2;
//
//            Matrix m = new Matrix();
//            float sx = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getWidth();
//            float sy = (float) 2 * IMAGE_HALFWIDTH
//                    / mBitmap.getHeight();
//            m.setScale(sx, sy);
//            //设置缩放信息
//            //将logo图片按martix设置的信息缩放
//            mBitmap = Bitmap.createBitmap(mBitmap, 0, 0,
//                    mBitmap.getWidth(), mBitmap.getHeight(), m, false);
//
//            int[] pixels = new int[size * size];
//            for (int y = 0; y < size; y++) {
//                for (int x = 0; x < size; x++) {
//                    if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH
//                            && y > halfH - IMAGE_HALFWIDTH
//                            && y < halfH + IMAGE_HALFWIDTH) {
//                        //该位置用于存放图片信息
//                        //记录图片每个像素信息
//                        pixels[y * width + x] = mBitmap.getPixel(x - halfW
//                                + IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH);
//                    } else {
//                        if (bitMatrix.get(x, y)) {
//                            pixels[y * size + x] = 0xff000000;
//                        } else {
//                            pixels[y * size + x] = 0xffffffff;
//                        }
//                    }
//                }
//            }
//            Bitmap bitmap = Bitmap.createBitmap(size, size,
//                    Bitmap.Config.ARGB_8888);
//            bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
//            return bitmap;
//        } catch (WriterException e) {
//            e.printStackTrace();
//            return null;
//        }
//    }
 
 
}