Android之使用Android-AQuery异步加载图片(一)
第一节:转载地址(http://www.cnblogs.com/lee0oo0/archive/2012/10/25/2738299.html)
// 必须实现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();
}
}
Android之使用Android-AQuery异步加载图片(一)的更多相关文章
- android Listview 软引用SoftReference异步加载图片
首先说一下,android系统加载大量图片系统内存溢出的3中解决方法: (1)从网络或本地加载图片的时候,只加载缩略图.这个方法的确能够少占用不少内存,可是它的致命的缺点就是,因为加载的是缩略图,所以 ...
- 【Android】ListView、RecyclerView异步加载图片引起错位问题
今天在RecyclerView列表里遇到一个情况,它包含300条数据,每项包含一个图片,发现在首次载入时,由于本地没图,请求网络的时候:快速滑动导致了图片错位.闪烁的问题. 原理的话有一篇已经说的很清 ...
- 实例演示Android异步加载图片
本文给大家演示异步加载图片的分析过程.让大家了解异步加载图片的好处,以及如何更新UI.首先给出main.xml布局文件:简单来说就是 LinearLayout 布局,其下放了2个TextView和5个 ...
- 实例演示Android异步加载图片(转)
本文给大家演示异步加载图片的分析过程.让大家了解异步加载图片的好处,以及如何更新UI.首先给出main.xml布局文件:简单来说就是 LinearLayout 布局,其下放了2个TextView和5个 ...
- [Android]异步加载图片,内存缓存,文件缓存,imageview显示图片时增加淡入淡出动画
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3574131.html 这个可以实现ImageView异步加载 ...
- Android中ListView异步加载图片错位、重复、闪烁问题分析及解决方案
我们在使用ListView异步加载图片的时候,在快速滑动或者网络不好的情况下,会出现图片错位.重复.闪烁等问题,其实这些问题总结起来就是一个问题,我们需要对这些问题进行ListView的优化. 比如L ...
- android异步加载图片并缓存到本地实现方法
图片过多造成内存溢出,这个是最不容易解决的,要想一些好的缓存策略,比如大图片使用LRU缓存策略或懒加载缓存策略.今天首先介绍一下本地缓存图片 在android项目中访问网络图片是非常普遍性的事 ...
- Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅
Android 高手进阶(21) 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处http://blog.csdn.net/xiaanming/article/details ...
- Android 实现ListView异步加载图片
ListView异步加载图片是非常实用的方法,凡是是要通过网络获取图片资源一般使用这种方法比较好,用户体验好,下面就说实现方法,先贴上主方法的代码: package cn.wangmeng.test; ...
随机推荐
- MVC解决方案发布IIS 登录页面需要输入两次帐号问题
IIS项目在本地VS2013 解决方案中正常登录可以进入.发布IIS时出现需要输入两次帐号密码进入主页面最终发现是web.config文件配置问题 web.config 默认配置 <authen ...
- [转]深入理解学习GIT工作流
深入理解学习Git工作流 字数13437 阅读2761 评论3 喜欢70 个人在学习git工作流的过程中,从原有的 SVN 模式很难完全理解git的协作模式,直到有一天我看到了下面的文章,好多遗留在心 ...
- c语言求平面上2个坐标点的直线距离、求俩坐标直线距离作为半径的圆的面积、递归、菲波那次数列、explode
#include <stdio.h> #include <math.h> #include <string.h> char explode( char * str ...
- MATLAB与C/C++混合编程的一些总结
[转载请注明出处]http://www.cnblogs.com/mashiqi 先上总结: 由于C/C++语言的函数输入输出参数的特点,可以将多个参数方便地传入一个函数中,但却不能方便地返回多个参数. ...
- SQLite手工注入方法小结
SQLite 官网下载:www.sqlite.org/download.html sqlite管理工具:http://www.yunqa.de/delphi/products/sqlitespy/in ...
- POJ 1006 中国剩余定理
#include <cstdio> int main() { // freopen("in.txt","r",stdin); ; while(sca ...
- QQ空间漫步者
主要功能(QQ空间) 判断空间权限并跳过无法访问 留下足迹并可选:同时留言(可单独),赞主页(可单独),赞说说(可单独) 其他附加功能,导出QQ,导入群成员,好友,空间访客,说说评论,发表说说 送空间 ...
- python requests库入门[转]
首先,确认一下: Requests 已安装 Requests是 最新的 让我们从一些简单的示例开始吧. 发送请求 使用Requests发送网络请求非常简单. 一开始要导入Requests模块: > ...
- JSBinding / Home
Description JSBinding is a tool enabling you to run actual javascript in Unity3D. It contains Mozill ...
- phpunit4.1的干净测试
一般而言,写测试时需要加载一些文件来进行自动加载 但在phpunit4.1中只要其中一个测试文件加载了,其他测试文件就不需要再加载