您的位置:首页 > 其它

深入学习xUtils 2015-06-14 10:50 23人阅读 评论(0) 收藏

2015-06-14 10:50 246 查看
xUtils简介
xUtils 包含了很多实用的android工具。
xUtils 最初源于Afinal框架,进行了大量重构,使得xUtils支持大文件上传,更全面的http请求协议支持(10种谓词),拥有更加灵活的ORM,更多的事件注解支持且不受混淆影响...
xUitls最低兼容android 2.2 (api level 8)

目前xUtils主要有四大模块:
ViewUtils模块:
android中的ioc框架,完全注解方式就可以进行UI,资源和事件绑定;
新的事件绑定方式,使用混淆工具混淆后仍可正常工作;
目前支持常用的20种事件绑定,参见ViewCommonEventListener类和包com.lidroid.xutils.view.annotation.event。

HttpUtils模块:
支持同步,异步方式的请求;
支持大文件上传,上传大文件不会oom;
支持GET,POST,PUT,MOVE,COPY,DELETE,HEAD,OPTIONS,TRACE,CONNECT 请求;
下载支持301/302重定向,支持设置是否根据Content-Disposition重命名下载的文件;
返回文本内容的请求(默认只启用了GET请求)支持缓存,可设置默认过期时间和针对当前请求的过期时间。

BitmapUtils模块:
加载bitmap的时候无需考虑bitmap加载过程中出现的oom和android容器快速滑动时候出现的图片错位等现象;
支持加载网络图片和本地图片;
内存管理使用lru算法,更好的管理bitmap内存;
可配置线程加载线程数量,缓存大小,缓存路径,加载显示动画等...

DbUtils模块:
android中的orm框架,一行代码就可以进行增删改查;
支持事务,默认关闭;
可通过注解自定义表名,列名,外键,唯一性约束,NOT NULL约束,CHECK约束等(需要混淆的时候请注解表名和列名);
支持绑定外键,保存实体时外键关联实体自动保存或更新;
自动加载外键关联实体,支持延时加载;
支持链式表达查询,更直观的查询语义,参考下面的介绍或sample中的例子。

XUtil使用方法:

使用xUtils快速开发框架需要有以下权限:
<uses-permissionandroid:name="android.permission.INTERNET" />
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />
public class MainActivity extends Activity {
@ViewInject(R.id.btn_get)
private Button btn_get;
@ViewInject(R.id.btn_post)
private Button btn_post;
@ViewInject(R.id.btn_bitmapUtils)
private Button btn_bitmapUtils;
@ViewInject(R.id.btn_sendsync)
private Button btn_sendsync;
private ImageView imgv;
@ViewInject(R.id.lv)
private ListView lv;
private String url = "http://api.yi18.net/news/list?page=1&limit=10";
private String url1 = "http://api.yi18.net/news/list?";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewUtils.inject(this);

}

@OnClick(R.id.btn_get)
public void btnget(View view) {
HttpUtils httpUtils = new HttpUtils();
httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() {

@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
Log.e("", "错误信息===" + arg0.getMessage());

}

@Override
public void onSuccess(ResponseInfo<String> result) {
// TODO Auto-generated method stub
Log.e("", "请求到的信息" + result.result);
try {
// JSONObject obj1 = new JSONObject(result.result);
// JSONArray jsonArray = obj1.getJSONArray("yi18");
// List<Info> infos = new ArrayList<Info>();
// for (int i = 0; i < jsonArray.length(); i++) {
// Info info = new Info();
// JSONObject obj2 = jsonArray.getJSONObject(i);
// String title = obj2.getString("title");
// info.setTitle(title);
// if (obj2.has("img")) {
// String img = obj2.getString("img");
// info.setImg(img);
// }
// infos.add(info);
// lv.setAdapter(new InfoAdapter(getApplicationContext(),
// infos));
// }
getData(result);

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
});

}

@OnClick(R.id.btn_post)
public void btnpost(View view) {
HttpUtils httpUtils = new HttpUtils();
RequestParams params = new RequestParams();
params.addBodyParameter("page", "1");
params.addBodyParameter("limit", "20");
httpUtils.send(HttpMethod.POST, url1, params,
new RequestCallBack<String>() {

@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
Log.e("===", arg1.toString());
}

@Override
public void onSuccess(ResponseInfo<String> arg0) {
// TODO Auto-generated method stub
Log.e("请求成功", arg0.result);
try {
getData(arg0);

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

});
}

private void getData(ResponseInfo<String> arg0) throws JSONException {
JSONObject obj1 = new JSONObject(arg0.result);
JSONArray jsonArray = obj1.getJSONArray("yi18");
List<Info> infos = new ArrayList<Info>();
for (int i = 0; i < jsonArray.length(); i++) {
Info info = new Info();
JSONObject obj2 = jsonArray.getJSONObject(i);
String title = obj2.getString("title");
info.setTitle(title);
if (obj2.has("img")) {
String img = obj2.getString("img");
info.setImg(img);
}
infos.add(info);
lv.setAdapter(new InfoAdapter(getApplicationContext(), infos));
}
}

@OnClick(R.id.btn_sendsync)
public void btnsendsync(View view) {
HttpUtils httpUtils =new HttpUtils();
try {
ResponseStream sendSync = httpUtils.sendSync(HttpMethod.GET, url);
Log.e("sync", "======================="+sendSync);

} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@OnClick(R.id.btn_bitmapUtils)
public void btnbmputils(View view) {
imgv = (ImageView) findViewById(R.id.imgv);
BitmapUtils bitmapUtils = new BitmapUtils(getApplicationContext());
bitmapUtils.configDefaultLoadingImage(R.drawable.ic_launcher);
bitmapUtils
.configDefaultLoadFailedImage(android.R.drawable.alert_light_frame);
bitmapUtils.display(imgv, "http://avatar.csdn.net/1/9/0/3_merbn.jpg",
new BitmapLoadCallBack<ImageView>() {

@Override
public void onLoadCompleted(ImageView arg0, String arg1,
Bitmap arg2, BitmapDisplayConfig arg3,
BitmapLoadFrom arg4) {
// TODO Auto-generated method stub
imgv.setImageBitmap(toRoundBitmap(arg2));
}

@Override
public void onLoadFailed(ImageView arg0, String arg1,
Drawable arg2) {
// TODO Auto-generated method stub

}
});

}

public static Bitmap toRoundBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
if (width <= height) {
roundPx = width / 2;
top = 0;
left = 0;
bottom = width;
right = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}

Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(output);

final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int) left, (int) top, (int) right,
(int) bottom);
final Rect dst = new Rect((int) dst_left, (int) dst_top,
(int) dst_right, (int) dst_bottom);
final RectF rectF = new RectF(dst);

paint.setAntiAlias(true);

canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(4);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, src, dst, paint);

// 画白色圆圈
paint.reset();
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
paint.setAntiAlias(true);
canvas.drawCircle(width / 2, width / 2, width / 2 - 5 / 2, paint);
return output;
}


public class MainActivity extends Activity {
@ViewInject(R.id.btn_downImage)
private Button btn_downImage;
@ViewInject(R.id.btn_startanim)
private Button btn_start;
@ViewInject(R.id.btn_stopanim)
private Button btn_stop;
@ViewInject(R.id.btn_loadxmlanim)
private Button btn_Load;
@ViewInject(R.id.btn_downImageBack)
private Button btn_loadBack;
AnimationDrawable animationDrawable;
@ViewInject(R.id.imageView1)
private ImageView imgv;
@ViewInject(R.id.imageView2)
private ImageView imgv2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewUtils.inject(this);
}

// 加载xml文件动画
@OnClick(R.id.btn_loadxmlanim)
public void btn_load(View view) {
Animation a = AnimationUtils.loadAnimation(this, R.anim.alpha);
imgv.startAnimation(a);
}

// 下载图片有回调

@OnClick(R.id.btn_downImageBack)
public void btnLoadImageBack(View view) {

final RotateAnimation animation = new RotateAnimation(0, 180, 200, 200);
animation.setRepeatCount(5);
animation.setDuration(5000);
BitmapUtils bitmapUtils = new BitmapUtils(this);
bitmapUtils.configDefaultImageLoadAnimation(animation);
bitmapUtils.display(imgv, "http://avatar.csdn.net/1/9/0/3_merbn.jpg",
new BitmapLoadCallBack<ImageView>() {

@Override
public void onLoadCompleted(ImageView arg0, String arg1,
Bitmap arg2, BitmapDisplayConfig arg3,
BitmapLoadFrom arg4) {
// TODO Auto-generated method stub
imgv.setImageBitmap(arg2);
imgv.startAnimation(animation);

}

@Override
public void onLoadFailed(ImageView arg0, String arg1,
Drawable arg2) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "错误", 0).show();
}
});

}

// 停止动画

@OnClick(R.id.btn_stopanim)
public void btnstop(View view) {
if (animationDrawable != null) {
animationDrawable.stop();
}
}

// 开启动画
@OnClick(R.id.btn_startanim)
public void btnstart(View view) {

if (animationDrawable != null) {
animationDrawable = (AnimationDrawable) imgv2.getBackground();
animationDrawable.start();
}
}

// 下载图片

@OnClick(R.id.btn_downImage)
public void btndownImage(View view) {
BitmapUtils bitmapUtils = new BitmapUtils(this);

AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation anim1 = new AlphaAnimation(0, 1);
anim1.setDuration(5000);

animationSet.addAnimation(anim1);
bitmapUtils.configDefaultImageLoadAnimation(animationSet);
bitmapUtils.display(imgv, "http://avatar.csdn.net/1/9/0/2_merbn.jpg");

}


目前xUtils主要有四大模块:
ViewUtils模块:
android中的ioc框架,完全注解方式就可以进行UI,资源和事件绑定;
新的事件绑定方式,使用混淆工具混淆后仍可正常工作;
目前支持常用的20种事件绑定,参见ViewCommonEventListener类和包com.lidroid.xutils.view.annotation.event。

HttpUtils模块:
支持同步,异步方式的请求;
支持大文件上传,上传大文件不会oom;
支持GET,POST,PUT,MOVE,COPY,DELETE,HEAD,OPTIONS,TRACE,CONNECT 请求;
下载支持301/302重定向,支持设置是否根据Content-Disposition重命名下载的文件;
返回文本内容的请求(默认只启用了GET请求)支持缓存,可设置默认过期时间和针对当前请求的过期时间。

BitmapUtils模块:
加载bitmap的时候无需考虑bitmap加载过程中出现的oom和android容器快速滑动时候出现的图片错位等现象;
支持加载网络图片和本地图片;
内存管理使用lru算法,更好的管理bitmap内存;
可配置线程加载线程数量,缓存大小,缓存路径,加载显示动画等...

DbUtils模块:
android中的orm框架,一行代码就可以进行增删改查;
支持事务,默认关闭;
可通过注解自定义表名,列名,外键,唯一性约束,NOT NULL约束,CHECK约束等(需要混淆的时候请注解表名和列名);
支持绑定外键,保存实体时外键关联实体自动保存或更新;
自动加载外键关联实体,支持延时加载;
支持链式表达查询,更直观的查询语义,参考下面的介绍或sample中的例子。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: