Android常用的第三方开源库汇总
生命周期
响应式编程
网络请求框架
RxEasyHttp—基于RxJava+Retrofit
1. 引入
// dependencies
implementation "com.zhouyou:rxeasyhttp:$rootProject.rxeasyhttp"
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
EasyHttp.init(this);//默认初始化
//全局设置请求头
HttpHeaders headers = new HttpHeaders();
headers.put("User-Agent", SystemInfoUtils.getUserAgent(this, AppConstant.APPID));
//全局设置请求参数
HttpParams params = new HttpParams();
params.put("appId", AppConstant.APPID);
//以下设置的所有参数是全局参数,同样的参数可以在请求的时候再设置一遍,那么对于该请求来讲,请求中的参数会覆盖全局参数
EasyHttp.getInstance()
.setBaseUrl(Url)//设置全局URL url只能是域名 或者域名+端口号
// 打开该调试开关并设置TAG,不需要就不要加入该行
// 最后的true表示是否打印内部异常,一般打开方便调试错误
.debug("EasyHttp", true)
//如果使用默认的60秒,以下三行也不需要设置
.setReadTimeOut(60 * 1000)
.setWriteTimeOut(60 * 100)
.setConnectTimeout(60 * 100)
// 可以全局统一设置超时重连次数,默认为3次,那么最差的情况会请求4次(一次原始请求,三次重连请求),不需要可以设置为0
.setRetryCount(3)//网络不好自动重试3次
//可以全局统一设置超时重试间隔时间,默认为500ms,不需要可以设置为0
.setRetryDelay(500)//每次延时500ms重试
//可以全局统一设置超时重试间隔叠加时间,默认为0ms不叠加
.setRetryIncreaseDelay(500)//每次延时叠加500ms
//可以全局统一设置缓存模式,默认是不使用缓存,可以不传,具体请看CacheMode
.setCacheMode(CacheMode.NO_CACHE)
//可以全局统一设置缓存时间,默认永不过期
.setCacheTime(-1)//-1表示永久缓存,单位:秒 ,Okhttp和自定义RxCache缓存都起作用
//全局设置自定义缓存保存转换器,主要针对自定义RxCache缓存
.setCacheDiskConverter(new SerializableDiskConverter())//默认缓存使用序列化转化
//全局设置自定义缓存大小,默认50M
.setCacheMaxSize(100 * 1024 * 1024)//设置缓存大小为100M
//设置缓存版本,如果缓存有变化,修改版本后,缓存就不会被加载。特别是用于版本重大升级时缓存不能使用的情况
.setCacheVersion(1)//缓存版本为1
//.setHttpCache(new Cache())//设置Okhttp缓存,在缓存模式为DEFAULT才起作用
//可以设置https的证书,以下几种方案根据需要自己设置
.setCertificates()//方法一:信任所有证书,不安全有风险
//.setCertificates(new SafeTrustManager()) //方法二:自定义信任规则,校验服务端证书
//配置https的域名匹配规则,不需要就不要加入,使用不当会导致https握手失败
//.setHostnameVerifier(new SafeHostnameVerifier())
//.addConverterFactory(GsonConverterFactory.create(gson))//本框架没有采用Retrofit的Gson转化,所以不用配置
.addCommonHeaders(headers)//设置全局公共头
.addCommonParams(params)//设置全局公共参数
//.addNetworkInterceptor(new NoCacheInterceptor())//设置网络拦截器
//.setCallFactory()//局设置Retrofit对象Factory
//.setCookieStore()//设置cookie
//.setOkproxy()//设置全局代理
//.setOkconnectionPool()//设置请求连接池
//.setCallbackExecutor()//全局设置Retrofit callbackExecutor
//可以添加全局拦截器,不需要就不要加入,错误写法直接导致任何回调不执行
//.addInterceptor(new GzipRequestInterceptor())//开启post数据进行gzip后发送给服务器
.addInterceptor(new CustomSignInterceptor());//添加参数签名拦截器
}
}
// 权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name=".MyApplication" />
缓存
1. 引入
2. 存
put(String key, Bitmap bitmap)
put(String key, byte[] value)
put(String key, String value)
put(String key, JSONObject jsonObject)
put(String key, JSONArray jsonArray)
put(String key, Serializable value)
put(String key, Drawable value)
editor(String key).newOutputStream(0);//原有的方式
3. 取
String getAsString(String key);
JSONObject getAsJson(String key)
JSONArray getAsJSONArray(String key)
<T> T getAsSerializable(String key)
Bitmap getAsBitmap(String key)
byte[] getAsBytes(String key)
Drawable getAsDrawable(String key)
InputStream get(String key);//原有的用法
UI视图
SwitchButton—高仿IOS系统SwitchButton
1. 引入
repositories {
mavenCentral()
jcenter()
}
dependencies {
compile 'com.github.zcweng:switch-button:0.0.3@aar'
}
2. 布局文件使用
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<com.suke.widget.SwitchButton
android:id="@+id/switch_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
3. Java文件使用
SwitchButton switchButton = (SwitchButton) findViewById(R.id.switch_button);
switchButton.setChecked(true);// 设置默认值,true->开启;false->关闭
switchButton.isChecked();
switchButton.toggle(); //switch state
switchButton.toggle(false);//switch without animation
switchButton.setShadowEffect(true);//disable shadow effect
switchButton.setEnabled(false);//disable button
switchButton.setEnableEffect(false);//disable the switch animation
switchButton.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(SwitchButton view, boolean isChecked) {
//TODO do your job
}
});
4. 参数说明
<attr name="sb_shadow_radius" format="reference|dimension"/>阴影半径-->
<attr name="sb_shadow_offset" format="reference|dimension"/><!--阴影偏移-->
<attr name="sb_shadow_color" format="reference|color"/><!--阴影颜色-->
<attr name="sb_uncheck_color" format="reference|color"/><!--关闭颜色-->
<attr name="sb_checked_color" format="reference|color"/><!--开启颜色-->
<attr name="sb_border_width" format="reference|dimension"/><!--边框宽度-->
<attr name="sb_checkline_color" format="reference|color"/><!--开启指示器颜色-->
<attr name="sb_checkline_width" format="reference|dimension"/><!--开启指示器线宽-->
<attr name="sb_uncheckcircle_color" format="reference|color"/><!--关闭指示器颜色-->
<attr name="sb_uncheckcircle_width" format="reference|dimension"/><!--关闭指示器线宽-->
<attr name="sb_uncheckcircle_radius" format="reference|dimension"/><!--关闭指示器半径-->
<attr name="sb_checked" format="reference|boolean"/><!--是否选中-->
<attr name="sb_shadow_effect" format="reference|boolean"/><!--是否启用阴影-->
<attr name="sb_effect_duration" format="reference|integer"/><!--动画时间,默认300ms-->
<attr name="sb_button_color" format="reference|color"/><!--按钮颜色-->
<attr name="sb_show_indicator" format="reference|boolean"/><!--是否显示指示器,默认true:显示-->
<attr name="sb_background" format="reference|color"/><!--背景色,默认白色-->
<attr name="sb_enable_effect" format="reference|boolean"/><!--是否启用特效,默认true-->