xUtils3,xutils3github
1.簡介
xUtils3 是 xUtils 的升級版,功能和效能都在提高,xUtils3 主要有四大模組:註解模組,連網模組,圖片載入模組,資料庫模組;
註解模組:
用於在 Activity 或者 Fragment 中初始化布局檔案,便於代碼更加簡潔;
連網模組:
xUtils3 支援超大檔案(超過 2G)上傳,更全面的 http 請求協議支援(11 種謂詞),擁有更加靈活的 ORM,更多的事件註解支援且不受混淆影響;
圖片載入模組:
載入圖片很方便並且不用擔心記憶體溢出,還可以圖片綁定支援 gif(受系統相容性影響, 部分 gif 檔案只能靜態顯示), webp; 支援圓角, 圓形, 方形等裁剪,支援自動旋轉.
資料庫模組:
資料庫 api 簡化提高效能, 達到和 greenDao 一致的效能
2.註解模組
①在 Activity 中使用註解
1.在 Application 的 oncreate 方法中加入下面代碼: x.Ext.init(this);
2.在 Activity 的 oncreate 方法中加入下面代碼: x.view().inject(this);
3.載入當前的 Activity 布局需要如下註解: @ContentView 加入到 Activity 的上方
4.給 View 進行初始化需要如下註解: @InjectView
5.處理控制項的各種響應事件需要如下註解: @Envent
@Event(value = R.id.btn_database)private void database(View view){ Toast.makeText(XUtils3Activity.this, "進入資料庫模組單獨的",Toast.LENGTH_SHORT).show();}
②在 Fragment 中使用註解
在onCreateView()中return super.onCreateView(inflater, container, savedInstanceState);改為return x.view().inject(this,inflater,container);
@ContentView(R.layout.fragment_demo)public class DemoFragment extends Fragment {
@ViewInject(R.id.btn_fragment) private Button button; private Context context; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context =getContext(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {// return super.onCreateView(inflater, container, savedInstanceState); return x.view().inject(this,inflater,container); }}
3.連網模組
①xUtils3 的 Get 和 Post 請求文本
//1.Get請求//2.Post請求RequestParams params = new RequestParams("http://api.m.mtime.cn/PageSubArea/TrailerList.api");x.http().post(params, new Callback.CommonCallback<String>() {@Overridepublic void onSuccess(String result) {Log.e("TAG", "xUtis3連網請求成功==" + result);// textView.setText("Get請求的結果——————" + result);textView.setText("Post請求的結果——————" + result);}@Overridepublic void onError(Throwable ex, boolean isOnCallback) {Log.e("TAG", "xUtis3連網請求失敗==" + ex.getMessage());textView.setText("xUtis3連網請求失敗==" + ex.getMessage());}@Overridepublic void onCancelled(CancelledException cex) {Log.e("TAG", "onCancelled==" + cex.getMessage());}@Overridepublic void onFinished() {Log.e("TAG","onFinished==");}});
②xUtils3 檔案下載
RequestParams params = new RequestParams("http://vfx.mtime.cn/Video/2016/09/15/mp4/160915092608935956_480.mp4");//設定儲存資料params.setSaveFilePath(Environment.getExternalStorageDirectory() + "/atguigu/480.mp4");//設定是否可以立即取消下載params.setCancelFast(true);//設定是否自動根據頭資訊命名params.setAutoRename(false);//設定斷點續傳params.setAutoResume(true);params.setExecutor(new PriorityExecutor(3, true));//自訂線程池,有效值範圍[1, 3], 設定為3時, 可能阻塞圖片載入.x.http().get(params, new Callback.ProgressCallback<File>() {/** * 當下載成功的時候回調這個方法,並且把下載到哪個路徑回傳過來 * @param file */@Overridepublic void onSuccess(File file) {Log.e("TAG","onSuccess=="+file.toString());Toast.makeText(XUtils3NetActivity.this, "onSuccess=="+file.toString(), Toast.LENGTH_SHORT).show();}@Overridepublic void onError(Throwable ex, boolean isOnCallback) {Log.e("TAG","onError=="+ex.getMessage());}@Overridepublic void onCancelled(CancelledException cex) {Log.e("TAG","onCancelled=="+cex.getMessage());}@Overridepublic void onFinished() {Log.e("TAG","onFinished==");}@Overridepublic void onWaiting() {Log.e("TAG","onWaiting==");}@Overridepublic void onStarted() {Log.e("TAG","onStarted==");}@Overridepublic void onLoading(long total, long current, boolean isDownloading) {progressbar.setMax((int) total);progressbar.setProgress((int) current);Log.e("TAG", "onLoading=="+current+"/"+total+",isDownloading=="+isDownloading);}});
③xUtils3 大檔案上傳
RequestParams params = new RequestParams("http://192.168.1.16:8080/FileUpload/FileUploadServlet");//以表單方式上傳params.setMultipart(true);//設定上傳檔案的路徑params.addBodyParameter("File",new File(Environment.getExternalStorageDirectory()+"/atguigu/480.mp4"),null,"oppo.mp4");x.http().post(params, new Callback.ProgressCallback<File>() {/** * 當下載成功的時候回調這個方法,並且把下載到哪個路徑回傳過來 * @param file */@Overridepublic void onSuccess(File file) {Log.e("TAG", "onSuccess==" + file.toString());Toast.makeText(XUtils3NetActivity.this, "onSuccess==" + file.toString(), Toast.LENGTH_SHORT).show();}@Overridepublic void onError(Throwable ex, boolean isOnCallback) {Log.e("TAG", "onError==" + ex.getMessage());}@Overridepublic void onCancelled(CancelledException cex) {Log.e("TAG", "onCancelled==" + cex.getMessage());}@Overridepublic void onFinished() {Log.e("TAG", "onFinished==");}@Overridepublic void onWaiting() {Log.e("TAG", "onWaiting==");}@Overridepublic void onStarted() {Log.e("TAG", "onStarted==");}@Overridepublic void onLoading(long total, long current, boolean isDownloading) {progressbar.setMax((int) total);progressbar.setProgress((int) current);Log.e("TAG", "onLoading==" + current + "/" + total + ",isDownloading==" + isDownloading);}});