一、ViewUtils
你受够了重复冗长的findViewById了嘛?你受够了各种监听事件的绑定了嘛?在这里,你只需要一句注解,如@ViewInject、@OnClick,就能轻松摆脱小白似的代码,大大的上了一个档次。
二、HttpUtils
支持的HTTP七种请求方式,非常便捷的满足你的接口请求的需要。同时还支持大文件上传下载,以及同步异步请求。
三、BitmapUtils
你的程序因OOM强制关闭过嘛?你在为加在网络图片头疼嘛?有了组件,你将永久摆脱前面的问题。
四、DbUtils
简单易用又出色的ORM框架,真的是谁用谁知道,直接轻松存储各种对象到sqlite数据库中,同时也能非常方便的进行各种条件查询,甚至分页查询,还有对表中数据的更新删除等操作,真正的实现。一行代码就可以进行增删改查。并且可通过注解自定义表名,列名,外键,唯一性约束,NOT NULL约束,CHECK约束等,支持事务。
先来看一看ViewUtils的代码:
[java] view plain
copy
print ?
- //绑定布局文件
- @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;
- }
- }
- }
//绑定布局文件
@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
支持的HTTP七种请求方式,非常便捷的满足你的接口请求的需要。同时还支持大文件上传下载,以及同步异步请求 [java] view plain copy print ?
- @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() {
- @Override
- public void onSuccess(ResponseInfo 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
httpHandler = httpUtils.download(url, Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "master.zip", - new RequestCallBack
() { - @Override
- public void onSuccess(ResponseInfo
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() {
- @Override
- public void onSuccess(ResponseInfo responseInfo) {
- System.out.println(responseInfo.result);
- }
- @Override
- public void onFailure(HttpException error, String msg) {
- System.out.println(msg);
- }
- });
- }
- /**
- * 获取提交的参数
- *
- * @Title: getParams
- * @说 明:
- * @参 数: @return
- * @return List
返回类型 - * @throws
- */
- private List
getParams() { - List
list = new ArrayList (); - NameValuePair userName = new BasicNameValuePair("name", "test");
- list.add(userName);
- NameValuePair pswNameValuePair = new BasicNameValuePair("psw", "123456");
- list.add(pswNameValuePair);
- return list;
- }
- }
@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() {@Override
public void onSuccess(ResponseInfo 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 httpHandler = httpUtils.download(url, Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "master.zip",
new RequestCallBack() {@Override
public void onSuccess(ResponseInfo 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() {@Override
public void onSuccess(ResponseInfo responseInfo) {
System.out.println(responseInfo.result);
}@Override
public void onFailure(HttpException error, String msg) {
System.out.println(msg);
}
});
}/**
* 获取提交的参数
*
* @Title: getParams
* @说 明:
* @参 数: @return
* @return List 返回类型
* @throws
*/
private List getParams() {
List list = new ArrayList();
NameValuePair userName = new BasicNameValuePair("name", "test");
list.add(userName);
NameValuePair pswNameValuePair = new BasicNameValuePair("psw", "123456");
list.add(pswNameValuePair);
return list;
}}
三、BitmapUtils
[java] view plain copy print ?
- 第一:首先自定义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() {
- @Override
- public void onSuccess(ResponseInfo 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
{ - private BitmapUtils bitmapUtils;
- public MainAdapter(List
list, Context context) { - super(list, context);
- }
- public MainAdapter(List
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;
- }
- }
第一:首先自定义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() {@Override
public void onSuccess(ResponseInfo 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 {private BitmapUtils bitmapUtils;
public MainAdapter(List list, Context context) {
super(list, context);
}public MainAdapter(List 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.** { *; }
xUtils2.6.14.jar 和 source以及RecycleView-v7-24.1.1.jar 和source下载
【android xUtils 2.6.14 jar 下载 使用 和混淆】
推荐阅读
- android|快速开发之xUtils(三)DbUtils详细介绍
- xUtils|xUtils3框架解析一(概述: ViewInject + ImageLoader + Http + SQLite 功能封装和使用)
- Androidstudio|Android studio利用xutils进行文件下载
- xUtils系列之ViewUtils-ViewInject注解