Android之使用Android-query框架进行开发
开发Android使用Android-query框架能够快速的,比传统开发android所要编写的代码要少得很多,容易阅读等优势。
下载文档及其例子和包的地址:http://code.google.com/p/android-query/
以下内容是我学习的一些心得分享:
第一节:
// 必须实现AQuery这个类
AQuery aq = new AQuery(view);
// 按顺序分析:取得xml对应控件id,设置图片,设置可以显示,点击事件(方法someMethod必须是public修饰)
aq.id(R.id.icon).image(R.drawable.icon).visible().clicked(this, "someMethod");
// 设置文字内容
aq.id(R.id.name).text(content.getPname());
aq.id(R.id.time).text(FormatUtility.relativeTime(System.currentTimeMillis(), content.getCreate())).visible();
aq.id(R.id.desc).text(content.getDesc()).visible();
AQuery也支持Fragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(getContainerView(), container, false);
aq = new AQuery(getActivity(), view);
return view;
}
第二节: 使用AQuery异步加载图片
2.1 从网上读取图片
aq.id(R.id.image1).image(“图片URL”);
2.2 缓存控制: 图片过大的话,避免记忆缓存
boolean memCache =false;
     boolean fileCache = true;
     aq.id(R.id.image1).image("http://www.vikispot.com/z/images/vikispot/android-w.png", memCache, fileCache);
2.3 当下载太多图片的时候需要降低图片采样率,第四个参数为了保证图片质量,一般范围时200-399aq.id(R.id.image1).image(imageUrl, true, true, , );
2.4 如果下载图片失败,处理的方法:1. 设置一个预定的图片 2. 使imageview不可见或者是goneaq.id(R.id.image1).image(imageUrl, true, true, 0, R.drawable.default_image);
aq.id(R.id.image1).image(imageUrl, true, true, 0, AQuery.INVISIBLE);
aq.id(R.id.image1).image(imageUrl, true, true, 0, AQuery.GONE); 2.5 图片预加载// 从之前的url取得小图片
String thumbnail = "http://farm6.static.flickr.com/5035/5802797131_a729dac808_s.jpg";
Bitmap preset = aq.getCachedImage(thumbnail);
// 加载大图片前先显示小图片
String imageUrl = "http://farm6.static.flickr.com/5035/5802797131_a729dac808_b.jpg";
aq.id(R.id.image).image(imageUrl, false, false, 0, 0, preset, AQuery.FADE_IN);
2.6 在加载图片的时候显示进度条,progress里面传入idString imageUrl = "http://farm6.static.flickr.com/5035/5802797131_a729dac808_b.jpg";
aq.id(R.id.image).progress(R.id.progress).image(imageUrl, false, false);
2.7 图片圆角显示,不支持大图片ImageOptions options = new ImageOptions();
options.round = 15;
aq.id(R.id.image).image(url, options);
2.8 图片长宽比例 // 保留原图片比例
aq.id(R.id.image).image(imageUrl, true, true, 0, 0, null, AQuery.FADE_IN, AQuery.RATIO_PRESERVE);
// 自定义图片比例
//1:1, a square
aq.id(R.id.image2).image(imageUrl, true, true, 0, 0, null, 0, 1.0f / 1.0f);
aq.id(R.id.image3).image(imageUrl, true, true, 0, 0, null, 0, 1.5f / 1.0f);
//16:9, a video thumbnail
aq.id(R.id.image4).image(imageUrl, true, true, 0, 0, null, 0, 9.0f / 16.0f);
aq.id(R.id.image5).image(imageUrl, true, true, 0, 0, null, 0, 3.0f / 4.0f);
2.9 图片描点,如果图片过高,描点可用来描述图片的哪一部分用于显示
Anchor values:1.0 : Display top of the image
0 : Display the center of the image
-1.0 : Display bottom of the image
AQuery.ANCHOR_DYNAMIC : Display image with a top bias for photos.
=======================================================ImageOptions options =newImageOptions();options.ratio =1;options.anchor =1.0;aq.id(R.id.image1).image(imageUrl, options);
2.10 自定义图片加载后的处理aq.id(R.id.image1).image(imageUrl,true,true,0,0,newBitmapAjaxCallback(){});
2.11 异步从文件加载图片,建议使用降低采样率避免oomFile file =newFile(path);
//load image from file, down sample to target width of 300 pixels
aq.id(R.id.avatar).image(file,300);
//load image from file with callback
aq.id(R.id.avatar).image(file,false,300,newBitmapAjaxCallback(){
@Override
publicvoid callback(String url,ImageView iv,Bitmap bm,AjaxStatus status){
iv.setImageBitmap(bm);
}
});
2.12 如果之前image("url")已经成功,之后的都可以直接使用而不需要重新访问网络,也就是说之后可以离线访问此图像资源
2.13 文件中获取缓冲图片
File file = aq.getCachedFile(url);
2.14 除了imageview,webview也可以用来放图片
aq.id(R.id.web).progress(R.id.progress).webImage(url);
2.15 延迟图片加载,帮助你是否加载正在快速滚动的listview,详情参考文档使用
2.16 图片不使用缓存
aq.id(R.id.image).image(url, false, false);
2.17 缓存配置,缓存一般是保存在内部文件系统,但也可以保存在SDCard里面File ext = Environment.getExternalStorageDirectory();
File cacheDir = new File(ext, "myapp");
AQUtility.setCacheDir(cacheDir);
2.18 共享图片,为了与其他程序共享图片,你需要把文件放在SDCard,makeSharedFile方法创建缓存地址的一个副本File file = aq.makeSharedFile(url, "android.png");
if(file != null){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivityForResult(Intent.createChooser(intent, "Share via:"), SEND_REQUEST);
}
2.19 配置,最好把配置写在application的onCreate方法,详细参考文档
2.20 程序退出时候需要把缓存清除
if(isTaskRoot()){
AQUtility.cleanCacheAsync(this);
}
//clean the file cache with advance option
long triggerSize = 3000000; //大于3M时候开始清除
long targetSize = 2000000; //直到少于2M
AQUtility.cleanCacheAsync(this, triggerSize, targetSize);
}
2.21 低内存处理publicclassMainApplicationextendsApplication{
@Override
         public void onLowMemory(){  
        //clear all memory cached images when system is in low memory
        //note that you can configure the max image cache count, see CONFIGURATION
         BitmapAjaxCallback.clearCache();
}
}
异步网络:
1. 添加权限:<uses-permissionandroid:name="android.permission.INTERNET"/>
2. 支持的类型
JSONObject
JSONArray
String (HTML, XML)
XmlDom (XML parsing)
XmlPullParser (Large XML files)
byte array
User defined custom type (Transformer)
Bitmap
3. 以Json数据为例,注意,红色部分是随你请求的数据类型一起改变
String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0";
aq.ajax(url, JSONObject.class, new AjaxCallback<JSONObject>() { @Override
public void callback(String url, JSONObject json, AjaxStatus status) {
if(json != null){
//successful ajax call, show status code and json content
Toast.makeText(aq.getContext(), status.getCode() + ":" + json.toString(), Toast.LENGTH_LONG).show();
}else{
//ajax error, show error code
Toast.makeText(aq.getContext(), "Error:" + status.getCode(), Toast.LENGTH_LONG).show();
}
}
});
上面的形式也可以写成下面一样,他们是无条件对等
public void asyncJson(){
        
        //perform a Google search in just a few lines of code
        
        String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0";             
        aq.ajax(url, JSONObject.class, this, "jsonCallback");
        
}
public void jsonCallback(String url, JSONObject json, AjaxStatus status){
        
        if(json != null){               
                //successful ajax call          
        }else{          
                //ajax error
        }
}         
再举一个使用AQuery的XmlDom解析xml的例子,如果XML过大,使用XMLPullParser
public void xml_ajax(){         
        String url = "https://picasaweb.google.com/data/feed/base/featured?max-results=8";              
        aq.ajax(url, XmlDom.class, this, "picasaCb");           
}
public void picasaCb(String url, XmlDom xml, AjaxStatus status){
// 返回一系列为entry的结点,并把其add进list
        List<XmlDom> entries = xml.tags("entry");               
        List<String> titles = new ArrayList<String>();
        
        String imageUrl = null;
        
        for(XmlDom entry: entries){
                titles.add(entry.text("title")); //循环把第一个结点为title的文本放进title
                imageUrl = entry.tag("content", "type", "image/jpeg").attr("src");//把第一个结点为content,属性为type,属性值为image/jpeg的src属性值赋予给imageUri
        }
                
        aq.id(R.id.image).image(imageUrl);
}         
4. 如果你想指定保存文件的位置,使用download方法
String url = "https://picasaweb.google.com/data/feed/base/featured?max-results=16"; File ext = Environment.getExternalStorageDirectory();
File target = new File(ext, "aquery/myfolder/photos.xml"); aq.progress(R.id.progress).download(url, target, new AjaxCallback<File>(){
public void callback(String url, File file, AjaxStatus status) {
if(file != null){
showResult("File:" + file.length() + ":" + file, status);
}else{
showResult("Failed", status);
}
}
});
5. 自定义类型(文档例子是gson数据使用对象解析),详细见文档
6. 使用Http Post (Multiple)
privatevoid aync_multipart(){
String url = "https://graph.facebook.com/me/photos";
Map<String, Object> params = new HashMap<String, Object>();
params.put("message", "Message");
//Simply put a byte[] to the params, AQuery will detect it and treat it as a multi-part post
byte[] data = getImageData();
params.put("source", data);
//Alternatively, put a File or InputStream instead of byte[]
//File file = getImageFile();
//params.put("source", file);
AQuery aq = new AQuery(getApplicationContext());
aq.auth(handle).ajax(url, params, JSONObject.class, this, "photoCb");
}
7. 使用ajax是很容易达到缓存的
String url = "http://www.google.com"; // 返回最近15分钟内的缓存副本,如果expire为-1,内容将会立即更新且缓存
long expire = * * ; aq.ajax(url, String.class, expire, new AjaxCallback<String>() { @Override
public void callback(String url, String html, AjaxStatus status) {
showResult(html);
}
});
8. 使缓存无效
public void callback(String url, JSONObject json, AjaxStatus status) {
    
        if(json != null){
                if("1".equals(json.optString("status"))){
                        //do something
                }else{
                        // 不缓存
                        status.invalidate();
                }
        }
}
9. 同步调用:如果ajax调用是在新开的线程,sync方法能够阻塞线程,直到ajax调用完毕,如果sync方法用在主线程将会引起Exception
String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0";
AjaxCallback<JSONObject> cb = new AjaxCallback<JSONObject>();
cb.url(url).type(JSONObject.class);
aq.sync(cb);
JSONObject jo = cb.getResult();
AjaxStatus status = cb.getStatus();
Android之使用Android-query框架进行开发的更多相关文章
- [转载] Android.Hook框架xposed开发篇
		本文转载自: http://www.52pojie.cn/thread-396793-1-1.html 原帖:http://drops.wooyun.org/tips/7488 作者:瘦蛟舞 官方教程 ... 
- 王家林的81门一站式云计算分布式大数据&移动互联网解决方案课程第14门课程:Android软硬整合设计与框架揭秘: HAL&Framework &Native Service &App&HTML5架构设计与实战开发
		掌握Android从底层开发到框架整合技术到上层App开发及HTML5的全部技术: 一次彻底的Android架构.思想和实战技术的洗礼: 彻底掌握Andorid HAL.Android Runtime ... 
- 2015最流行的Android组件、工具、框架大全
		Android 是目前最流行的移动操作系统之一. 随着新版本的不断发布, Android的功能也日益强大, 涌现了很多流行的应用程序, 也催生了一大批的优秀的组件. 本文试图将目前流行的组件收集起来以 ... 
- 六款值得推荐的android(安卓)开源框架简介
		1.volley 项目地址 https://github.com/smanikandan14/Volley-demo (1) JSON,图像等的异步下载: (2) 网络请求的排序(scheduli ... 
- Android 中建立一个OpenGL ES的开发环境
		转自: http://wiki.eoe.cn/page/Building_an_OpenGL_ES_Environment.html 负责人:zhangql原文链接:http://docs.eoean ... 
- Android开源项目发现--- 工具类快速开发篇(持续更新)
		1. Guava Google的基于java1.6的类库集合的扩展项目 包括collections, caching, primitives support, concurrency librarie ... 
- 【转载】Android开源:数据库ORM框架GreenDao学习心得及使用总结
		转载链接:http://www.it165.net/pro/html/201401/9026.html 最近在对开发项目的性能进行优化.由于项目里涉及了大量的缓存处理和数据库运用,需要对数据库进行频繁 ... 
- Android音频系统之音频框架
		1.1 音频框架 转载请注明,From LXS, http://blog.csdn.net/uiop78uiop78/article/details/8796492 Android的音频系统在很长一段 ... 
- android glide图片加载框架
		项目地址: https://github.com/bumptech/glide Glide作为安卓开发常用的图片加载库,有许多实用而且强大的功能,那么,今天就来总结一番,这次把比较常见的都写出来,但并 ... 
- [Android Pro]   终极组件化框架项目方案详解
		cp from : https://blog.csdn.net/pochenpiji159/article/details/78660844 前言 本文所讲的组件化案例是基于自己开源的组件化框架项目g ... 
随机推荐
- Solr删除数据
			步骤: 1.在Solr客户端左下方 Core Selector 中点选想要删除数据的索引库 2.点选Documents 3.右侧Document Type中点选XML 4.Document(s)中输入 ... 
- 一次@value取值失败的原因
			网上down了一份源码.启动后报错,通过报错信息定位到这个地方: 之前对这个@Value的实现方式我也没了解过,所以乘机对springboot关于这一块的源码研究了一下.可以参考当时我的一篇分析记录& ... 
- app  crash率的标准
			手Q定义是: android: 发布目标是低于1% ios: 0.8%以下 
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-人机界面如何快速调整大量控件的位置
			打开元素列表,然后直接从顶部按住Shift批量选中控件即可 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/acetaohai123 我的 ... 
- 微信小程序 - bindcontroltap和control的关系(map)
			听说最近要废弃control,用cover-image和cover-view替代它,层级问题(我们此等萌新们还在想图标怎么显示在地图上(-.-)) 粗略的来说,一个展示(control),一个触发(b ... 
- 读写锁pthread_rwlock_t的使用(转)
			读写锁是用来解决读者写者问题的,读操作可以共享,写操作是排他的,读可以有多个在读,写只有唯一个在写,同时写的时候不允许读. 具有强读者同步和强写者同步两种形式 强读者同步:当写者没有进行写操作,读者就 ... 
- iOS编程 手动忽略clang编译器警告
			在iOS开发过程中, 我们可能会碰到一些系统方法弃用, weak.循环引用.不能运行之类的警告. 有代码洁癖的孩子们非常想消除他们, 今天就让我们来一次Fuck 警告.! 首先学会主要的语句 #pra ... 
- 【Python3 爬虫】14_爬取淘宝上的手机图片
			现在我们想要使用爬虫爬取淘宝上的手机图片,那么该如何爬取呢?该做些什么准备工作呢? 首先,我们需要分析网页,先看看网页有哪些规律 打开淘宝网站http://www.taobao.com/ 我们可以看到 ... 
- WP8控件进度条(ProgressBar)的使用
			--前台代码 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,0,24,0"& ... 
- lua面向对象编程 《lua程序设计》 16章 笔记
			Lua中的table就是一种对象,即它拥有状态.拥有独立于其值的标识(self).table与对象一样具有独立于创建者和创建地的征集周期 什么叫对象拥有独立的生命周期? Account = {bala ... 
