0%

1
2
3
4
5
6
7
8
9
10
11
12
13
git reset --soft HEAD~   撤销当前本地提交的
git status 查看当前修改的状态

git config --global http.proxy http://127.0.0.1:7890 让git使用代理
git config --global https.proxy http://127.0.0.1:7890
git config --get https.proxy 测试是否走代理

git config --global --unset http.proxy 关掉代理
git config --global --unset https.proxy

git config --global --get http.proxy 确认,没有输出就说明清掉了
git config --global --get https.proxy

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 按下状态 -->
<item android:state_pressed="true"
android:drawable="@color/color_FF5800" />

<!-- 选中状态(可选) -->
<item android:state_selected="true"
android:drawable="@color/color_FF5800" />

<!-- 默认状态(抬起) -->
<item android:drawable="@color/gray_999999" />
</selector>

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
1. 添加依赖
在你的 App 模块的 build.gradle 文件中添加 AutoSize 依赖:

gradle
dependencies {
implementation 'com.github.JessYanCoding:AndroidAutoSize:v1.2.1'
}
如果无法从 Jcenter 获取(注意 Jcenter 已于 2022 年 2 月后停止维护),可以尝试使用 JitPack 仓库:

gradle
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
dependencies {
implementation 'com.github.JessYanCoding:AndroidAutoSize:v1.2.1'
}
2. 配置全局设计图尺寸
在 AndroidManifest.xml 的 <application> 标签内添加 meta-data 配置你的设计图尺寸(单位通常为 dp):

xml
<manifest>
<application>
<meta-data
android:name="design_width_in_dp"
android:value="360"/> <!-- 根据你的设计图宽度填写,例如 360 dp -->
<meta-data
android:name="design_height_in_dp"
android:value="640"/> <!-- 根据你的设计图高度填写,例如 640 dp -->
</application>
</manifest>
这里的 value 值需要你根据设计师提供的设计图来定。常见做法是:

如果设计图是 1920x1080 px,通常除以 3(因为 1920/3=640, 1080/3=360),可配置为 360 dp (宽) x 640 dp (高)。

如果设计图是 1280x720 px,通常除以 2(1280/2=640, 720/2=360),同样可配置为 360 dp (宽) x 640 dp (高)。

关键在于:design_width_in_dp 和 design_height_in_dp 的单位必须是 dp。如果设计师只给了 px 尺寸,你需要换算:dp = px / (dpi / 160)。如果不知道设备 DPI,粗略地将 px 尺寸除以 2 或 3 也是一种办法。

3. 初始化 SDK
在你的自定义 Application 类的 onCreate() 方法中进行初始化:

kotlin
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
// 初始化 AutoSize
AutoSize.initCompatMultiProcess(this) // 处理多进程:cite[1]

// 可选:进行高级配置
AutoSizeConfig.getInstance()
.setLog(false) // 设置是否打印日志,默认 true
.setBaseOnWidth(true) // 默认 true,表示以宽度为基准适配。false 则以高度为基准:cite[2]
.setUseDeviceSize(false) // 是否使用设备的实际尺寸做适配,默认为 false:cite[1]
.setExcludeFontScale(false) // 是否屏蔽系统字体大小对 AndroidAutoSize 的影响, 默认为 false:cite[1]
}
}
别忘了在 AndroidManifest.xml 中通过 android:name 属性指定你的 Application 类。

🛠 高级用法与自定义配置
选择布局单位
AutoSize 支持两种类型的布局单位:

单位类型 单位 特点 适用场景
主单位 dp, sp 侵入性低,推荐在新项目中使用。会影响三方库页面、控件及系统控件的布局。
副单位 pt, in, mm 侵入性高,不会影响其他使用 dp 布局的三方库或系统控件。


⚠️ 注意事项
初始化时机:AutoSize 的初始化应在 Application 的 onCreate() 中尽早完成。

设计图尺寸:务必正确配置 design_width_in_dp 和 design_height_in_dp,这是准确适配的基础。

页面自定义:对于与全局设计图尺寸不一致或不需要适配的页面,记得使用 CustomAdapt 或 CancelAdapt 进行个性化配置。

布局预览:在 Android Studio 中布局时,你可能需要创建对应的模拟设备以获得更准确的预览效果。具体创建方法可参考官方文档或社区分享。

ProGuard 混淆:如果启用了代码混淆,请在混淆规则文件(proguard-rules.pro)中添加以下规则:

bash
-keep class me.jessyan.autosize.** { *; }
-keep interface me.jessyan.autosize.** { *; }




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 public interface WarningListener {
void onWarning(String message);
}


public static void inputData(WarningListener listener) throws IOException {

if(listener != null){
listener.onWarning(secondOutput+"");
}
}



activity或fragment中调用
class.inputData(new class.WarningListener() {
@Override
public void onWarning(@NonNull String message) {
runOnUiThread(() -> txt_msg.append(message + "\n"));
}
});

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
在安卓中,安全使用线程池是保证应用性能、避免 ANR 和内存泄漏的重要手段。

1️⃣ 为什么要用线程池

避免频繁创建和销毁线程,线程创建成本高。

限制线程数量,避免系统过载。

复用线程,提高性能。

安卓开发中常用线程池有两类:

Executors 提供的便捷方法:如 newFixedThreadPool、newCachedThreadPool、newSingleThreadExecutor。

自定义 ThreadPoolExecutor:灵活配置核心线程数、最大线程数、队列大小、拒绝策略。

2️⃣ 常用线程池写法
① 固定线程池(FixedThreadPool)
ExecutorService fixedPool = Executors.newFixedThreadPool(4); // 固定4个线程

fixedPool.execute(() -> {
// 耗时任务
doHeavyTask();
});


适合:任务数量大但线程数可控的情况。

② 缓存线程池(CachedThreadPool)
ExecutorService cachedPool = Executors.newCachedThreadPool();

cachedPool.execute(() -> {
doHeavyTask();
});


特点:有新任务就创建线程,空闲线程 60s 后销毁。

适合:执行短时异步任务,数量不确定。

风险:任务过多会无限开线程 → OOM。

③ 单线程池(SingleThreadExecutor)
ExecutorService singlePool = Executors.newSingleThreadExecutor();
singlePool.execute(() -> {
// 顺序执行任务
});


特点:任务按顺序执行,保证顺序性。

适合:任务必须按顺序执行的场景,比如写文件、写数据库。

④ 自定义线程池(ThreadPoolExecutor)
ThreadPoolExecutor executor = new ThreadPoolExecutor(
4, // 核心线程数
8, // 最大线程数
60, // 空闲线程存活时间
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(100), // 队列大小
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略
);


优点:灵活可控。

拒绝策略:

AbortPolicy → 抛异常

CallerRunsPolicy → 在调用者线程执行

DiscardPolicy → 丢弃任务

DiscardOldestPolicy → 丢弃最老任务

3️⃣ 安全使用线程池的注意点

避免 UI 更新在子线程

executor.execute(() -> {
// 耗时任务
String result = doHeavyTask();

// UI更新
runOnUiThread(() -> textView.setText(result));
});


避免线程泄漏

Activity/Fragment 销毁时要关闭线程池:

@Override
protected void onDestroy() {
super.onDestroy();
executor.shutdownNow(); // 停止所有任务
}


使用有限队列和核心线程数

防止任务无限堆积,导致 OOM。

结合生命周期感知组件

可以用 ViewModel + LiveData / RxJava / Coroutine 来管理线程和 UI 更新。

4️⃣ 推荐组合方式

IO密集型任务(网络请求、文件读写):

ExecutorService ioPool = Executors.newCachedThreadPool();


CPU密集型任务(计算):

int cpuCount = Runtime.getRuntime().availableProcessors();
ExecutorService cpuPool = Executors.newFixedThreadPool(cpuCount);

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
在安卓开发中,多线程几乎是随时都会用到的,尤其是涉及 耗时操作 或 异步处理 的场景,否则主线程(UI线程)会被阻塞,导致卡顿甚至 ANR(应用无响应)。
1️⃣ 网络请求

原因:HTTP 请求、WebSocket、Socket 通信等都是耗时操作。

实现方式:

RxJava

OkHttp异步回调

Coroutine(Kotlin)

AsyncTask(过时)

new Thread(() -> {
String result = httpRequest();
runOnUiThread(() -> textView.setText(result));
}).start();

2️⃣ 数据库操作

查询、插入、更新大量数据时,操作可能很慢。

实现方式:

Room + LiveData / RxJava / Coroutine

自己开子线程执行数据库操作

new Thread(() -> {
List<User> users = db.userDao().getAllUsers();
runOnUiThread(() -> adapter.setData(users));
}).start();

3️⃣ 文件 I/O

读写本地文件、图片、视频、大数据文件。

大文件操作会阻塞主线程,必须放子线程。

ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
File file = new File(path);
writeToFile(file, content);
});

4️⃣ 图片加载 / 视频解码

Bitmap解码、视频帧处理、GIF播放。

常用工具:Glide、Fresco、ExoPlayer,本质都是在子线程处理。

5️⃣ 计时 / 定时任务

倒计时、定时上传数据、心跳包。

使用 Handler、ScheduledThreadPoolExecutor 或 TimerTask。

6️⃣ 复杂计算 / 算法

路径规划、图像处理、机器学习推理。

都要在子线程中执行,避免 UI 卡顿。

7️⃣ 消息 / 事件处理

子线程收到消息或数据,需要更新 UI。

用法:

Handler + Looper

runOnUiThread

View.post()

✅ 总结

多线程的本质目的是避免阻塞UI线程,凡是耗时操作、重复任务或异步事件,都会用到多线程。

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
1️⃣ rectangle(矩形)

最常用的形状。

可以通过 cornerRadius 设置圆角。

示例:

<shape android:shape="rectangle">
<solid android:color="#FF0000"/>
<corners android:radius="8dp"/>
</shape>

2️⃣ oval(椭圆 / 圆)

如果宽高相等就是圆形。

可用于圆形按钮或头像背景。

3️⃣ line(直线)

表示一条线,需要设置 size 的 height 或 width 才能显示。

示例:

<shape android:shape="line">
<size android:height="2dp"/>
<solid android:color="#000000"/>
</shape>

4️⃣ ring(环 / 圆环)

用于画圆环或进度条背景。

需要设置 innerRadius(内半径)、thickness(环宽)。

示例:

<shape android:shape="ring">
<size android:width="100dp" android:height="100dp"/>
<solid android:color="#00FF00"/>
<ring
android:innerRadius="30dp"
android:thickness="10dp"/>
</shape>

⚡ 总结
形状 说明 用途示例
rectangle 矩形,可加圆角 按钮、背景、卡片
oval 椭圆或圆 圆形按钮、头像
line 一条直线 分割线
ring 圆环 环形进度条

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
左下角点击分支:
HEAD(Current Branch)
Local
master (鼠标右键,new branch)

Remote
origin
master


修改新建的分支一点代码,然后提交,就会在远程仓库看到新分支

如果要合并新分支代码到主分支:
先local下先切换回主分支(checkout)
再点击新建的分支:merge into master
合并后直接再主分支git push,远程分支就修改了

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
📌 1. 普通 Java 单例 (应用级)
public class MySingleton {
private static MySingleton instance;
private MySingleton() {}
public static MySingleton getInstance() {
if (instance == null) instance = new MySingleton();
return instance;
}
}


instance 静态变量挂在 类加载器 上,只要这个类还在内存里,就不会销毁。

在 Android 应用进程退出时(系统杀掉应用进程),单例实例才会被回收。

如果你手动写 instance = null;,垃圾回收器(GC)可能在之后清理。

👉 结论:这种单例和应用进程同生共死。

📌 2. 带 Context 的单例
public class MyManager {
private static MyManager instance;
private Context context;
private MyManager(Context context) {
this.context = context.getApplicationContext(); // 必须用 ApplicationContext!
}
public static synchronized MyManager getInstance(Context context) {
if (instance == null) instance = new MyManager(context);
return instance;
}
}


如果保存了 Activity Context,那么 Activity 销毁时无法释放,可能导致 内存泄漏。

如果保存了 Application Context,那就和进程同生共死,进程退出才销毁。

📌 3. Kotlin object 单例
object MySingleton {
fun doSomething() {}
}


这种实现其实就是 静态类,生命周期 = 应用进程生命周期。

进程被杀时销毁。

📌 4. 依赖注入框架中的单例 (如 Dagger/Hilt)

“单例”受 容器管理,并不一定等于应用级单例。

如果是 @Singleton,一般和 Application 同生命周期。

如果是作用域内的(比如 ActivityScope),在 Activity 销毁时就会释放。

✅ 总结:

如果是自己写的 静态单例,通常 只有在应用进程被杀死时才会销毁。

如果单例里引用了 短生命周期对象(Activity、Service),那会导致泄漏,除非手动释放。

想要提前销毁,可以手动 instance = null;,然后等待 GC 回收。


单例(Singleton)生命周期等于整个进程,如果单例里直接引用了 短生命周期对象(如 Activity、Service、Context 等),在这些对象销毁后单例仍然持有引用,就会造成 内存泄漏。
✅ 预防方法
1. 不要在单例中保存短生命周期对象
2. 使用 WeakReference
如果确实要持有短生命周期对象,用 WeakReference 包装,避免强引用阻止回收。
3. 用回调时解绑
单例常做事件分发或回调管理,这种情况下要记得 在 Activity/Service 销毁时解除注册。

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
1️⃣ EasyPermissions(Google 官方推荐)

GitHub: https://github.com/googlesamples/easypermissions

特点:

基于 Activity/Fragment 封装权限请求

支持一次请求多个权限

自动处理“不再询问”提示

示例:

String[] perms = {Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO};
if (EasyPermissions.hasPermissions(this, perms)) {
// 已有权限
startCamera();
} else {
EasyPermissions.requestPermissions(
this,
"需要相机和麦克风权限",
123,
perms
);
}


回调:

@Override
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) { }

@Override
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) { }

2️⃣ AndPermission

GitHub: https://github.com/yanzhenjie/AndPermission

特点:

API 更现代化,支持链式调用

支持权限分组、申请多个权限

支持“去设置页面引导”

示例:

AndPermission.with(this)
.runtime()
.permission(Permission.CAMERA, Permission.RECORD_AUDIO)
.onGranted(permissions -> {
// 权限允许
startCamera();
})
.onDenied(permissions -> {
// 权限拒绝
Toast.makeText(this, "权限被拒绝", Toast.LENGTH_SHORT).show();
})
.start();

3️⃣ RxPermissions(RxJava 结合)

GitHub: https://github.com/tbruyelle/RxPermissions

特点:

使用 RxJava 响应式方式请求权限

链式写法更优雅,适合 RxJava 项目

示例:

RxPermissions rxPermissions = new RxPermissions(this);
rxPermissions
.request(Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO)
.subscribe(granted -> {
if (granted) {
startCamera();
} else {
Toast.makeText(this, "权限被拒绝", Toast.LENGTH_SHORT).show();
}
});

🔹 总结
库 优点 适合场景
EasyPermissions Google 官方,轻量,回调简单 普通项目、入门推荐
AndPermission API 链式,功能强大 中大型项目,UI 提示丰富
RxPermissions RxJava 响应式 已经用 RxJava 的项目最方便