一、ViewUtils
你受够了重复冗长的findViewById了嘛?你受够了各种监听事件的绑定了嘛?在这里,你只需要一句注解,如@ViewInject、@OnClick,就能轻松摆脱小白似的代码,大大的上了一个档次。
二、HttpUtils
支持的HTTP七种请求方式,非常便捷的满足你的接口请求的需要。同时还支持大文件上传下载,以及同步异步请求。
三、BitmapUtils
你的程序因OOM强制关闭过嘛?你在为加在网络图片头疼嘛?有了组件,你将永久摆脱前面的问题。
四、DbUtils
简单易用又出色的ORM框架,真的是谁用谁知道,直接轻松存储各种对象到sqlite数据库中,同时也能非常方便的进行各种条件查询,甚至分页查询,还有对表中数据的更新删除等操作,真正的实现。一行代码就可以进行增删改查。并且可通过注解自定义表名,列名,外键,唯一性约束,NOT NULL约束,CHECK约束等,支持事务。
由于xUtils是基于aFinal的,这个开源框架是国内的某位大神写的,所以了解了aFinal之后再回头看xUtils,才会更有收获。同时,也要向这位大神以及众多的开源贡献者致敬,有了他们的奉献和开源的精神,才涌现出一个个耳熟能详的更加优秀的更加稳定的框架。我们众所周知的Linux就是这么诞生的。
aFinal学习地址:http://www.afinal.org
先来看一看ViewUtils的代码:
- //绑定布局文件
- @ContentView(R.layout.activity_main)
- public class MainActivity extends Activity {
- //绑定btn1的id
- @(R.id.Button01 )
- private Button btn1 ;
- @ViewInject(R.id.Button02 )
- private Button btn2 ;
- @ViewInject(R.id.button3 )
- private Button btn3 ;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //绑定对象
- ViewUtils. inject(this);
- }
- //绑定按钮的监听器
- @OnChildClick({ R.id.Button01, R.id.Button02, R.id. button3 })
- private void onClick(View v) {
- switch (v.getId()) {
- case R.id.Button01 :
- Toast. makeText(MainActivity.this, "Button1",Toast.LENGTH_SHORT).show();
- break;
- case R.id.Button02 :
- Toast. makeText(MainActivity.this, "Button2",Toast.LENGTH_SHORT).show();
- break;
- case R.id.button3 :
- startActivity( new Intent(MainActivity.this ,HttpActivity.class));
- break;
- default:
- break;
- }
- }
- }
二、HttpUtils
- @ContentView(R.layout.activity_http)
- public class HttpActivity extends ActionBarActivity {
- @ViewInject(R.id.pregress)
- private TextView pregress;
- @ViewInject(R.id.post)
- private Button post;
- @ViewInject(R.id.download)
- private Button downLoad;
- @ViewInject(R.id.upload)
- private Button upload;
- HttpUtils httpUtils;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- ViewUtils.inject(this);
- httpUtils = new HttpUtils();
- loadData();
- }
- private void loadData() {
- // 请求数据
- httpUtils.send(HttpMethod.GET, "", new RequestCallBack<String>() {
- @Override
- public void onSuccess(ResponseInfo<String> responseInfo) {
- String result = responseInfo.result;
- System.out.println(result.toString());
- }
- @Override
- public void onFailure(HttpException error, String msg) {
- }
- });
- }
- /**
- * 下载数据
- *
- * @Title: downLoad
- * @说 明:
- * @参 数:
- * @return void 返回类型
- * @throws
- */
- public void downLoad() {
- String url = "https://github.com/wyouflf/xUtils/archive/master.zip";
- HttpHandler<File> httpHandler = httpUtils.download(url, Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "master.zip",
- new RequestCallBack<File>() {
- @Override
- public void onSuccess(ResponseInfo<File> responseInfo) {
- System.out.println("");
- }
- @Override
- public void onFailure(HttpException error, String msg) {
- }
- /**
- * 第一个参数文件的长度 , 第二 当前下载的进度 第三 判断是否是上传还是下载
- */
- @Override
- public void onLoading(long total, long current, boolean isUploading) {
- super.onLoading(total, current, isUploading);
- pregress.setText(String.valueOf(current / total * 100));
- }
- });
- httpHandler.cancel();
- }
- @OnClick({ R.id.upload })
- public void onClickListener(View view) {
- switch (view.getId()) {
- case R.id.download:
- downLoad();
- break;
- case R.id.post:
- downLoad();
- break;
- case R.id.upload:
- uploadFile();
- break;
- default:
- break;
- }
- }
- //上传图片到服务器(也可以上传对象、String都可以)
- public void uploadFile() {
- String url = "http://10.2.108.35:8080/app/uplaodFile?method=upload";
- RequestParams params = new RequestParams();
- String path =FileUtils.getImagePath();
- File file = new File(path);
- File[] files = file.listFiles();
- for (int i = 0; i < files.length; i++) {
- params.addBodyParameter("" + i, files[i]);
- }
- httpUtils.send(HttpMethod.POST, url, params, new RequestCallBack<String>() {
- @Override
- public void onSuccess(ResponseInfo<String> responseInfo) {
- System.out.println(responseInfo.result);
- }
- @Override
- public void onFailure(HttpException error, String msg) {
- System.out.println(msg);
- }
- });
- }
- /**
- * 获取提交的参数
- *
- * @Title: getParams
- * @说 明:
- * @参 数: @return
- * @return List<NameValuePair> 返回类型
- * @throws
- */
- private List<NameValuePair> getParams() {
- List<NameValuePair> list = new ArrayList<NameValuePair>();
- NameValuePair userName = new BasicNameValuePair("name", "test");
- list.add(userName);
- NameValuePair pswNameValuePair = new BasicNameValuePair("psw", "123456");
- list.add(pswNameValuePair);
- return list;
- }
- }
- 第一:首先自定义application,初始化配置
- public class BaseApplication extends Application {
- ***
- * BitmpUtils BitmapGlobalConfig 配置缓存大小,储存位置 大小
- * BitmapdisplayConfig 配置图片的大小
- * 以及设置默认图片,下载错误的图片
- *
- *
- */
- private BitmapGlobalConfig globalConfig;
- private BitmapDisplayConfig displayConfig;
- private static BaseApplication application;
- @Override
- public void onCreate() {
- super.onCreate();
- application = this;
- configBitmapParams();
- }
- //得到这个类的对象
- public static BaseApplication getApplication() {
- return application;
- }
- public void configBitmapParams() {
- /**
- * 单利模式初始化,并设置缓存的路径
- */
- globalConfig = BitmapGlobalConfig.getInstance(this, FileUtils.getAppImageCache());
- int cacheSize = (int) Runtime.getRuntime().maxMemory() / 8;
- // 设置缓存大小
- globalConfig.setMemoryCacheSize(cacheSize);
- }
- public BitmapGlobalConfig getGlobalConfig() {
- return globalConfig;
- }
- public void setGlobalConfig(BitmapGlobalConfig globalConfig) {
- this.globalConfig = globalConfig;
- }
- public BitmapDisplayConfig getDisplayConfig() {
- return displayConfig;
- }
- public void setDisplayConfig(BitmapDisplayConfig displayConfig) {
- this.displayConfig = displayConfig;
- }
- }
- 第二步:加载数据、通知adapter更新、在adapter里面进行加载图片
- private void loadData() {
- httpUtils.send(HttpMethod.GET, CommonURL.FRONT_IMAGE_TEST, new RequestCallBack<String>() {
- @Override
- public void onSuccess(ResponseInfo<String> responseInfo) {
- String result = responseInfo.result;
- try {
- JSONObject jsonObject = new JSONObject(result);
- JSONArray jsonArray = jsonObject.getJSONArray("items");
- if (jsonArray != null && jsonArray.length() > 0) {
- for (int j = 0; j < jsonArray.length(); j++) {
- Items items = Items.getFromJSON(jsonArray.getJSONObject(j));
- list.add(items);
- }
- adapter.notifyDataSetChanged();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onFailure(HttpException error, String msg) {
- LogUtils.e(msg);
- }
- });
- }
- 第三步
- package com.qianfeng.bitmaputils.adapter;
- import java.util.List;
- import android.content.Context;
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.TextView;
- import com.lidroid.xutils.BitmapUtils;
- import com.lidroid.xutils.ViewUtils;
- import com.lidroid.xutils.view.annotation.ViewInject;
- import com.qianfeng.bitmaputils.R;
- import com.qianfeng.bitmaputils.bean.Items;
- public class MainAdapter extends AppBaseAdapter<Items> {
- private BitmapUtils bitmapUtils;
- public MainAdapter(List<Items> list, Context context) {
- super(list, context);
- }
- public MainAdapter(List<Items> list, Context context, BitmapUtils bitmapUtils) {
- this(list, context);
- this.bitmapUtils = bitmapUtils;
- }
- @Override
- public View createView(int position, View convertView) {
- ViewHolder vh = null;
- if (convertView == null) {
- convertView = inflater.inflate(R.layout.fm_listview_item_layout, null);
- vh = new ViewHolder();
- ViewUtils.inject(vh, convertView);
- convertView.setTag(vh);
- } else {
- vh = (ViewHolder) convertView.getTag();
- }
- vh.contentTv.setText(list.get(position).getContent());
- vh.otherNameTv.setText(list.get(position).getLogin());
- bitmapUtils.display(vh.headIv, list.get(position).getImage());
- // bitmapUtils.display(container, uri, displayConfig, new ),这个方法里面有个回调监听器、可在这里设置进度条相关东西
- return convertView;
- }
- private static class ViewHolder {
- @ViewInject(R.id.headIv)
- ImageView headIv;
- @ViewInject(R.id.otherName)
- TextView otherNameTv;
- @ViewInject(R.id.content)
- private TextView contentTv;
- }
- }
XUtils混淆
第一:使用了ViewUtil的注解的监听事件,访问修饰符必须是public,否则混淆打包之后监听无效。
第二:在proguard-project中加入: -libraryjars libs/xUtils-2.6.14.jar
-keep class com.lidroid.** { *; }