├── .gitignore ├── LICENSE ├── README.md ├── WorkDaily.md └── doc ├── Asynchronous └── ReactiveX │ └── Rxjava.md ├── CustomView └── Paint │ ├── Shader.md │ ├── Xfermode.md │ ├── code │ └── PorterDuffView.java │ └── img │ └── Xfermode.jpg ├── Http └── OkHttp │ ├── OkHttp.md │ ├── OkHttp添加网络请求缓存.md │ └── img │ └── OkHttpOfficial.png ├── RecyclerView ├── RecyclerView.md └── RecyclerView抢item焦点.md └── TextView ├── SpannableString.md ├── SpannableStringBuilder.md └── TextView.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AndroidTeach 2 | 随笔,还没想好主页面写点啥,主要内容在WorkDaily中记录 3 | 4 | ## Python 5 | 以后移步这边了PythonCourse,Python基础到Web,爬虫,数据分析!!!
6 | 顺路送个羊毛科学上网 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /WorkDaily.md: -------------------------------------------------------------------------------- 1 | # WorkDaily # 2 |

学而时习之,不亦说乎。从事教学工作也有近一年半了,由于教学的限制,一些不是很常用或不容易一下讲明白的知识点长时间不使用,连自己也有些淡忘了,今天突然来感觉,决定写一些干货来记录分享给大家,但是突然又感觉无从写起,就从最近教学的知识点写起吧,毕竟咱是个很随性的人

3 | 4 | 5 | # 2017-2-12 6 | >* DynamicTab 动态管理Tab,带动画效果 7 | 8 | # 2016-12-15 9 | >* 同步Android Development目录,应用开发者使用的所有工具 10 | >* Xfermode图像混合模式 11 | 12 | # 2016-12-14 13 | >* 自定义View中的高级绘制,渲染器Shader 14 | 15 | # 2016-12-13 16 | >* SpannableString,神奇的字符序列CharSequence 17 | 18 | # 2016-12-6 19 | >* 使用RxJava + Retrofit + MVP来撸一个简单的小项目 VMovie 20 | 21 | # 2016-11-28 22 | >* OkHttp 的请求缓存 23 | -------------------------------------------------------------------------------- /doc/Asynchronous/ReactiveX/Rxjava.md: -------------------------------------------------------------------------------- 1 | # RxJava # 2 | - RxJava官方Wiki 3 | -------------------------------------------------------------------------------- /doc/CustomView/Paint/Shader.md: -------------------------------------------------------------------------------- 1 | # Shader # 2 |

3 |   Shader是在View绘制过程中的提供横向颜色变换的对象的一个基类,它通过Paint的setShader方法设置上去,在绘制过程中可以从我们的Shader中获取颜色(对Bitmap无效)
4 |   官网是上面那样介绍的,其实翻译为我们自己话就是,Shader是渲染器的基类,渲染器可以在我们绘制的时候提供颜色变换,在绘制Bitmap的时候无效。 5 |

6 | 7 | ## Shader的子类 ## 8 | >* LinearGradient 9 | >* RadialGradient 10 | >* SweepGradient 11 | >* BitmapShader 12 | >* ComposeShader 13 | 14 | ## LinearGradient ## 15 | - Linear 线性的,Gradient 倾斜的,有斜度的 16 | - 组合起来就是线性变换的渲染器,实际就是我们常说的线性渐变色 17 | - 有两种构造方式 18 | - 19 | LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1,TileMode tile) 20 | 21 |
22 | 参数挺多,但是不要紧张,我们一个一个来看
23 | - x0 x的起始坐标 24 | - y0 y的起始坐标 25 | - x1 x的结束坐标 26 | - x2 y的结束坐标 27 | - color0 渐变开始的颜色 28 | - color1 渐变结束的颜色 29 | - TileMode 下面统一介绍 30 | 31 | - 32 | LinearGradient(float x0, float y0, float x1, float y1, int colors[], float positions[],TileMode tile) 33 | 34 |
35 | 一样的参数就不介绍了
36 | - colors[] 渐变的颜色数组,按数组顺序渐变 37 | - positions[] 可能为null,null的话颜色均分,如果设置的话,数组长度需要和颜色数组长度一致,数组中的值 0-1的float 38 | - 其实辐射方向是start(x0,y0)--end(x1,y1)连起来的方向 39 | 40 | ## RadialGradient ## 41 | - Radial 放射状的 42 | - 圆环扩散,类似于水波纹 43 | - 有两种构造方式 44 | - 45 | RadialGradient(float centerX, float centerY, float radius,int centerColor, int edgeColor, TileMode tileMode) 46 | 47 | - centerX 扩散圆心X 48 | - centerY 扩散圆心Y 49 | - radius 扩散圆的半径 50 | - centerColor 从圆心向外扩散,中心的颜色 51 | - edgeColor 扩散到边缘的时候颜色 52 | - TileMode 后面统一说 53 | - 54 | RadialGradient(float centerX, float centerY, float radius,int colors[],float stops[], TileMode tileMode) 55 | 56 | - colors 和上面的差不多,辐射的时候颜色数组,按数组的坐标排颜色 57 | - stops 可以设置为null,为null的时候均分,如果设置的话需要和颜色数组的长度保持一致,取值0-1的float 58 | 59 | 60 | 61 | ## SweepGradient ## 62 | - Sweep 扫视,扫射 63 | - 扫射的颜色扩散,类似于雷达扫描的样子,但默认不是圆的 64 | - 同样有两种构造形式 65 | - 66 | SweepGradient(float cx, float cy, int color0, int color1) 67 | 68 | - cx 中心x的坐标 69 | - cy 中心y的坐标 70 | - color0 开始的颜色,以水平向右为0,顺时针为开始 71 | - color1 结束的颜色 72 | - 73 | SweepGradient(float cx, float cy,int colors[], float positions[]) 74 | 75 | - colors 和上面的一样,多个颜色 76 | - positions 可以为null,为null的时候均分,不为null的时候,数组长度需要和颜色数组长度一致,0-1的float取值 77 | 78 | 79 | ## BitmapShader ## 80 | - 使用一张图片作为绘制时的颜色抽取,可以用这个方便的实现圆形,圆角图片,只需使用bitmap构建这个Shader,然后用画笔去画你想要的形状就行了 81 | - 构造 82 | - 83 | BitmapShader(Bitmap bitmap, TileMode tileX, TileMode tileY) 84 | 85 | - bitmap 被用作抽取颜色的位图 86 | - tileX X方向的模式 87 | - tileY Y方向的模式 88 | 89 | 90 | ## ComposeShader ## 91 | - compose 组合的 92 | - 当你的需求比较复杂的时候,一个渲染器已经不能满足你了,你可能需要组合一下特效 93 | - 两种构造 94 | - 95 | ComposeShader(Shader shaderA, Shader shaderB, Xfermode mode) 96 | 97 | - shaderA PorterDuff.Mode中的dst 98 | - shaderB PorterDuff.Mode中的src 99 | - Xfermode 这个这里先简单的说一下,就是绘制两次,绘制的两个图样到底是如何处理的,是取交,取并,A覆盖B还是B覆盖A等等 100 | - 101 | ComposeShader(Shader shaderA, Shader shaderB, PorterDuff.Mode mode) 102 | 103 | - PorterDuff.Mode 和Xfermode用起来一样,Xfermode也是调用的这个 104 | 105 | ## TileMode ## 106 | >* 枚举一个,有三个可选值 107 | - CLAMP 如果我们绘制的容器比咱们声明的大,会复制结尾的边缘进行填充 108 | - REPEAT 单纯的重复123123123...这种 109 | - MIRROR 镜像的 上下左右都是镜像 110 | -------------------------------------------------------------------------------- /doc/CustomView/Paint/Xfermode.md: -------------------------------------------------------------------------------- 1 | # Xfermode # 2 | - Xfer 转送,转移,传递 mode模式 3 | - Xfermode 转换模式,传递模式,混合模式,描述起来感觉很抽象,还是引用官方的一张图来看看到底是个什么吧! 4 | ![](img/Xfermode.jpg) 5 | - 这张图是从最新的ApiDemos中取出来的,和旧版的效果不太一样 6 | - 有的人一看这张图,不禁又说了一句,这是什么鬼?还记得我们Shader中的ComposeShader嘛? 7 | - 组合Shader,有三个参数,前两个参数是两个Shader,最后一个就是Xfermode 8 | - 第一个Shader咱们说它是dst    黄圆Src 9 | - 第二个Shader咱们说它是src    蓝方块Dst 10 | - 第三个参数就是咱们今天要说的这个Xfermode,有的人立马说了不是还有PorterDuff.Mode嘛?实际上两种在内部都是调用的PorterDuff.Mode,都一样,都一样哈 11 | - 我的理解就是两张图绘制完了合并的时候到底留下哪部分,每种模式对应不同的合并方案 12 | 13 | - 先绘制的图dst 14 | - 后绘制的图src 15 | 16 | - 简单的制作了一个模板,只要替换Bitmap的生成,你就可以做出各种不同的混合效果啦 17 | 模板 18 | - 核心代码 19 |
 as follow:   
20 |       int sc = canvas.saveLayer(0, 0, getWidth(), getHeight(), null,Canvas.ALL_SAVE_FLAG);
21 | 
22 |       canvas.drawBitmap(mDstB, 0, 0, mPaint);
23 | 
24 |       // 通过改变数组的索引,可以观察不同的效果
25 |       mPaint.setXfermode(sModes[3]);
26 | 
27 |       canvas.drawBitmap(mSrcB, 0, 0, mPaint);
28 | 
29 |       canvas.restoreToCount(sc);
30 |   
31 | -------------------------------------------------------------------------------- /doc/CustomView/Paint/code/PorterDuffView.java: -------------------------------------------------------------------------------- 1 | package com.rock.xfermode.widget; 2 | 3 | import android.content.Context; 4 | import android.graphics.Bitmap; 5 | import android.graphics.Canvas; 6 | import android.graphics.Paint; 7 | import android.graphics.PorterDuff; 8 | import android.graphics.PorterDuffXfermode; 9 | import android.graphics.RectF; 10 | import android.graphics.Xfermode; 11 | import android.util.AttributeSet; 12 | import android.view.View; 13 | 14 | /** 15 | * 这个里面有一个比较大的坑,就是两个图在画的时候最外层容器应该是一样大的 16 | */ 17 | public class PorterDuffView extends View { 18 | 19 | private static final Xfermode[] sModes = { 20 | new PorterDuffXfermode(PorterDuff.Mode.CLEAR), 21 | new PorterDuffXfermode(PorterDuff.Mode.SRC), 22 | new PorterDuffXfermode(PorterDuff.Mode.DST), 23 | new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER), 24 | new PorterDuffXfermode(PorterDuff.Mode.DST_OVER), 25 | new PorterDuffXfermode(PorterDuff.Mode.SRC_IN), 26 | new PorterDuffXfermode(PorterDuff.Mode.DST_IN), 27 | new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT), 28 | new PorterDuffXfermode(PorterDuff.Mode.DST_OUT), 29 | new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP), 30 | new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP), 31 | new PorterDuffXfermode(PorterDuff.Mode.XOR), 32 | new PorterDuffXfermode(PorterDuff.Mode.DARKEN), 33 | new PorterDuffXfermode(PorterDuff.Mode.LIGHTEN), 34 | new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY), 35 | new PorterDuffXfermode(PorterDuff.Mode.SCREEN) 36 | }; 37 | 38 | private Bitmap makeSrc(int w, int h) { 39 | Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 40 | Canvas c = new Canvas(bm); 41 | Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); 42 | p.setColor(0xFF66AAFF); 43 | c.drawRect(w / 3, h / 3, w * 19 / 20, h * 19 / 20, p); 44 | return bm; 45 | } 46 | 47 | private Bitmap makeDst(int w, int h) { 48 | Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 49 | Canvas c = new Canvas(bm); 50 | Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); 51 | p.setColor(0xFFFFCC44); 52 | c.drawOval(new RectF(0, 0, w * 3 / 4, h * 3 / 4), p); 53 | return bm; 54 | } 55 | 56 | private final Paint mPaint; 57 | 58 | private Bitmap mDstB; 59 | 60 | private Bitmap mSrcB; 61 | 62 | private static final int h = 800; 63 | 64 | private static final int w = 800; 65 | 66 | public PorterDuffView(Context context) { 67 | this(context, null); 68 | } 69 | 70 | public PorterDuffView(Context context, AttributeSet attrs) { 71 | this(context, attrs, 0); 72 | } 73 | 74 | public PorterDuffView(Context context, AttributeSet attrs, int defStyleAttr) { 75 | super(context, attrs, defStyleAttr); 76 | 77 | mDstB = makeDst(w, h); 78 | 79 | mSrcB = makeSrc(w, h); 80 | 81 | mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 82 | } 83 | 84 | 85 | @Override 86 | protected void onDraw(Canvas canvas) { 87 | 88 | int sc = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG); 89 | 90 | canvas.drawBitmap(mDstB, 0, 0, mPaint); 91 | 92 | // 通过改变数组的索引,可以观察不同的效果 93 | mPaint.setXfermode(sModes[6]); 94 | 95 | canvas.drawBitmap(mSrcB, 0, 0, mPaint); 96 | 97 | canvas.restoreToCount(sc); 98 | 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /doc/CustomView/Paint/img/Xfermode.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RockTeach/AndroidTeach/3ba486495c84769b770587f08e8d4e38a8cb6280/doc/CustomView/Paint/img/Xfermode.jpg -------------------------------------------------------------------------------- /doc/Http/OkHttp/OkHttp.md: -------------------------------------------------------------------------------- 1 | # OKHttp # 2 | >* OKHttp是Square公司推出的一个网络请求框架 OkHttp GitHub官网 3 | 4 | # 综述 # 5 | - 照例还是先上一张官方的介绍 6 | ![](img/OkHttpOfficial.png) 7 | -------------------------------------------------------------------------------- /doc/Http/OkHttp/OkHttp添加网络请求缓存.md: -------------------------------------------------------------------------------- 1 | # 为网络请求添加缓存 # 2 |

      在以往的开发中,我们想要为页面添加缓存,通常会使用数据库或者文件的形式进行缓存,缓存做起来相对复杂。这里我们将使用OkHttp的制作一下页面缓存,体验一下什么叫做feel倍爽 3 | 。OkHttp简单的常规用法,这里我们就不进行赘述了,今天我们主要实现OkHttp网络请求的缓存

4 | 5 | - 通常我们在使用OkHttp做请求的时候是这个样子的 6 |
  7 | 
  8 |     OkHttpClient okHttpClient = new OkHttpClient();
  9 | 
 10 |     Request request = new Request.Builder()
 11 |                                     .url("http://xxx.xxx/xxx")
 12 |                                     .build();
 13 | 
 14 |     Call call = okHttpClient.newCall(request);
 15 | 
 16 |     call.enqueue(new Callback() {
 17 |         @Override
 18 |         public void onFailure(Call call, IOException e) {
 19 | 
 20 |         }
 21 | 
 22 |         @Override
 23 |         public void onResponse(Call call, Response response) throws IOException {
 24 | 
 25 |         }
 26 |     });
 27 | 
28 | - 在项目中我们知道这个OkHttpClient不需要重复创建,全局使用一个就Ok的 29 | - 如果我们有一些通用的配置就可以在这个OkHttp上进行配置,但是我们发现OkHttpClient这个对象好像没有提供太多的方法供我们调用,经过翻读源码,我们发现在new OkHttpClient对象的时候,它是使用Builder进行构建的 30 | - 实际上当你学过用过的知识点多了之后,你会发现很多配置型的数据都会使用这种建造者模式进行参数配置 31 | - 实例化可以变成这个样子 32 | OkHttpClient httpClient = new OkHttpClient.Builder().build(); 33 | - 通过这个建造者我们发现这OkHttpClient还是有很多可配置的属性的,其中就有一个叫做 cache 的方法,通过这个方法呢我们就可以实现请求数据的缓存,但是这个方法需要服务器的配合(也就是服务器本身要支持缓存),需要服务器返回的Response中包含 34 | Header:Cache-Control,max-age=xxx,如果服务器支持缓存,那么我们的OkHttp在初始化的时候只需为它指定一个缓存路径就OK了 35 |
 36 | Cache cache = new Cache(getCacheDir(),50 * 1024 * 1024);
 37 | 
 38 |         OkHttpClient okHttpClient = new OkHttpClient.Builder()
 39 |                 // 设置缓存目录
 40 |                 .cache(cache)
 41 | 
 42 |                 .build();
 43 | 
44 | - 但是在我们开发的过程中,很多时候服务器并没有实现缓存机制,而我们又想使用这个缓存怎么办呢?经研究发现我们可以使用建造OkHttpClient时 45 | 的一个方法,为OkHttpClient添加网络拦截器来实现 46 |
 47 | Cache cache = new Cache(getCacheDir(),50 * 1024 * 1024);
 48 | 
 49 |         Interceptor cacheInterCepter = new Interceptor() {
 50 |             @Override
 51 |             public Response intercept(Chain chain) throws IOException {
 52 | 
 53 |                 Request request = chain.request();
 54 |                 // 在请求前判断网络,没有网络将请求设置为强制从缓存获取
 55 |                 if (!NetWorkUtil.isNetConnected(TeachApp.this)) {
 56 |                     request = request.newBuilder()
 57 |                                     .cacheControl(CacheControl.FORCE_CACHE)
 58 |                                     .build();
 59 |                 }
 60 |                 // 获取请求的结果
 61 |                 Response response = chain.proceed(request);
 62 |                 // 根据网络状况,对结果进行不同转换
 63 |                 if (NetWorkUtil.isNetConnected(TeachApp.this)) {
 64 |                     String cacheControl = request.cacheControl().toString();
 65 |                     response = response.newBuilder()
 66 |                                         .removeHeader("Pragma")
 67 |                                         .addHeader("Cache-Control",cacheControl)
 68 |                                         .build();
 69 |                 }else{
 70 |                     response = response.newBuilder()
 71 |                                         .removeHeader("Pragma")
 72 |                                         .addHeader("Cache-Control","public,only-if-cached,max-stale" + 60 * 60 * 24 * 2)
 73 |                                         .build();
 74 |                 }
 75 |                 return response;
 76 |             }
 77 |         };
 78 | 
 79 | 
 80 | 
 81 |         OkHttpClient okHttpClient = new OkHttpClient.Builder()
 82 |                 // 设置缓存目录
 83 |                 .cache(cache)
 84 |                 // 添加网络拦截器
 85 |                 .addNetworkInterceptor(cacheInterCepter)
 86 |                 .build();
 87 | 
 88 | 
89 | - 在网络请求执行的时候对请求以及响应做包装,使我们的请求和响应支持缓存,那么我们在开发中到底应该怎样调用呢?其实也很简单,在请求的时候添加Header就可以实现了 90 | - 原生OkHttp的写法 91 |
 92 | Request request = new Request.Builder()
 93 |                               .url("http://xxx.xxx/xxx")
 94 |                               // 10 秒内的重复请求走缓存
 95 |                               .addHeader("Cache-Control","max-age=" + 10)
 96 |                               .build();
 97 | 
 98 |         Call call = okHttpClient.newCall(request);
 99 | 
100 |         call.enqueue(new Callback() {
101 |             @Override
102 |             public void onFailure(Call call, IOException e) {
103 | 
104 |             }
105 | 
106 |             @Override
107 |             public void onResponse(Call call, Response response) throws IOException {
108 | 
109 |             }
110 |         });
111 | 
112 | 
113 | - OkHttp-Utils用法 114 |
115 | OkHttpUtils.get()
116 |             .url("http://xxx.xxx")
117 |             // 五秒内的重复请求走缓存
118 |             .addHeader("Cache-Control","max-age=" + 5)
119 |             .build()
120 |             .execute(new StringCallback() {
121 |                 @Override
122 |                 public void onError(Call call, Exception e, int id) {
123 | 
124 |                 }
125 | 
126 |                 @Override
127 |                 public void onResponse(String response, int id) {
128 | 
129 |                 }
130 |               });
131 | 
132 | - Retrofit的用法 133 |
134 | @GET("/")
135 | Call getString(@Header("Cache-Control") String cacheControl);
136 | 
137 |
138 | Retrofit retrofit = new Retrofit.Builder()
139 |                             .addConverterFactory(GsonConverterFactory.create())
140 |                             .baseUrl("http://xxx.xxx")
141 |                             .build();
142 | 
143 |         RockApi rockApi = retrofit.create(RockApi.class);
144 | 
145 |         Call movie = rockApi.getMovie("max-age=" + 10);
146 | 
147 |         movie.enqueue(new Callback() {
148 |             @Override
149 |             public void onResponse(Call call, Response response) {
150 | 
151 |             }
152 | 
153 |             @Override
154 |             public void onFailure(Call call, Throwable t) {
155 | 
156 |             }
157 |         });
158 | 
159 | - 如果我们想做的智能一些,我们可以在对Cache-Control进行赋值的时候,根据网络状态去赋不同的值 160 | 161 | NetWorkUtil.isNetConnected(this) ? "max-age=6" : "only-if-cached, max-stale=" + 60 * 60 * 24 * 2; 162 | 163 | -------------------------------------------------------------------------------- /doc/Http/OkHttp/img/OkHttpOfficial.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RockTeach/AndroidTeach/3ba486495c84769b770587f08e8d4e38a8cb6280/doc/Http/OkHttp/img/OkHttpOfficial.png -------------------------------------------------------------------------------- /doc/RecyclerView/RecyclerView.md: -------------------------------------------------------------------------------- 1 | # RecyclerView # 2 | -------------------------------------------------------------------------------- /doc/RecyclerView/RecyclerView抢item焦点.md: -------------------------------------------------------------------------------- 1 | # 焦点抢占 # 2 | >* 当我们使用RecyclerView加载布局并在item中使用按压的selector的时候,我们会发现selector没有效果 3 | >* 实际上是由于RecyclerView抢占了焦点,焦点并没有传递给item 4 | 5 | # 解决方案 # 6 | >* 在RecyclerView标签上使用 android:descendantFocusability="afterDescendants" 7 | >* 在item布局上添加android:focusable="true",android:clickable="true" 8 | 9 | # 这里顺便说一下共享焦点的属性 # 10 | >* android:duplicateParentState="true" 11 | -------------------------------------------------------------------------------- /doc/TextView/SpannableString.md: -------------------------------------------------------------------------------- 1 | # SpannableString # 2 | >* CharSequence的一个实现类,可以使我们的CharSequence变得更加丰富多彩,比如可以为其中的文字设置不同的颜色,再比如可以为其中的在TextView上实现图文混排,添加URL的超链接等等... 3 | 4 | ## 构造及使用 ## 5 | >* SpannableString的构造直接通过new构造出来,参数是一个CharSequence对象 6 | >* 当构造完成我们的SpannableString后,我们就可以去操作我们的SpannableString了,类中直接拥有的方法并不多setSpan(),removeSpan() 7 | >* setSpan()方法中共有三个参数 8 | - ① Object对象,对应Span效果 9 | - ② int start 开始的位置 10 | - ③ int end 结束的位置 11 | - ④ int flag 12 | 13 | ### Span效果 ### 14 | - ClickableSpan 为CharSequence添加局部点击 一定注意TextView需要设置setMovementMethod(LinkMovementMethod.getInstance()) 15 | - URLSpan 为TextView局部添加超链接,默认会跳外部浏览器,同样需要setMovementMethod(LinkMovementMethod.getInstance()),如果需要自己对连接处理需要自己实现 LinkMovementMethod 16 | - ForegroundColorSpan 为CharSequence指定文字设置前景色(实际就是文字颜色) 17 | - BackgroundColorSpan 为CharSequence指定文字设置背景色 18 | - StrikeThroughSpan 过时线(中划线,删除线)常用来标记过时,打折信息 19 | - UnderlineSpan 下划线,强调 20 | - SubscriptSpan 下标,角标,通常用在数学公式上 21 | - SuperscriptSpan 上标,通常用在引用和数学公式上 22 | - AbsoluteSizeSpan 为CharSequence指定指定字符设置绝对大小 23 | - RelativeSizeSpan 为CharSequence指定指定字符设置相对大小(比绝对大小的适配要好) 24 | - StyleSpan 为文字设置粗体,斜体等样式 25 | - TypefaceSpan 为文字设置字体 26 | - TextAppearanceSpan 设置文字的偏好属性(其中包括大小,颜色,样式,字体) 27 | - ScaleXSpan 基于x轴的缩放 28 | - MaskFilterSpan 为文字设置过滤效果(有点类似滤镜的感觉,但是种类比较少),目前只有两种模糊和浮雕,关联BlurMaskFilter,EmbossMaskFilter 29 | - RasterizerSpan 为文字设置光栅效果 30 | - SuggestionSpan 用在EditText中,常用语输入法 31 | - LocaleSpan 动态改变Local属性的Span 32 | - ImageSpan 图片,加载drawable,实现TextView的图文混排,通常在聊天的表情中比较常用,还有就是社区微博类状态那比较常用 33 | - 其中还有几个抽象类MetricAffectingSpan,使我们上面所描述大多类的父类 34 | - DynamicDrawableSpan,ImageSpan的父类,想要定制一些特殊效果可以自己继承进行定制 35 | - ReplacementSpan,DynamicDrawableSpan的父类 36 | 37 | ### Flag意义 ### 38 | >* INCLUSIVE 包含的,EXCLUSIVE 排外的,不包含的 39 | - SPAN_INCLUSIVE_EXCLUSIVE 40 | - SPAN_INCLUSIVE_INCLUSIVE 41 | - SPAN_EXCLUSIVE_EXCLUSIVE 42 | - SPAN_EXCLUSIVE_INCLUSIVE 43 | >* 然后自己翻译过来就好啦,实际上在TextView上直接设置效果都一样,这些参数在EditText上才有效果 44 | 45 | ### TextPaint ### 46 | >* 我们在定制一些具体效果的时候,经常会看到这个类的身影,其实这个类也没有多神秘,他是Paint的子类,只是携带了一些默认的属性 47 | >* 在使用的时候,我们对画笔做的操作都可以用在上面 48 | -------------------------------------------------------------------------------- /doc/TextView/SpannableStringBuilder.md: -------------------------------------------------------------------------------- 1 | # SpannableStringBuilder # 2 | >* CharSequence的一个实现类 3 | >* 使用起来和StringBuilder有些类似 4 | >* append(CharSequence text, Object what, int flags) 5 | - 追加时的参数 6 | - ① 字符序列 7 | - ② 就是我们的Span对象了 8 | - ③ 还是我们说过的标记 9 | >* 最后有一点就是我们在设置到TextView上前往不要toString,要记得咱们说过TextView的setText参数 10 | -------------------------------------------------------------------------------- /doc/TextView/TextView.md: -------------------------------------------------------------------------------- 1 | # TextView # 2 |

3 | TextView在Android是一个使用非常频繁的控件,但是你真的把它用熟了嘛?有的同学说了,这有啥难的,天天用,都要用烂了。那么我想问你一个问题啊?TextView.setText()这个方法的参数是什么?有同学立马就答上来了,不就是String嘛,然后并不是的,实际上是CharSequence。 4 |

5 | 6 | ## CharSequence ## 7 | >* 字符序列,Java中的一个接口,拥有大量的实现类,比如我们的String就是其中的一个子类 8 | >* 其实在Android中还有一系列的CharSequence子类来使我们的文本更丰富 9 | 10 | ### SpannableString ### 11 | >* CharSequence的一个实现类 12 | 13 | ### SpannableStringBuilder ### 14 | >* CharSequence的一个实现类 15 | >* 和StringBuilder用起来有些类似 16 | --------------------------------------------------------------------------------