网站首页 Android 您当前的位置:首页> 学无止境> Android
Android之使用Android-query框架进行开发(一)
发布时间:2016-05-11编辑:刘伟 (1983)人看过
第一节:
// 必须实现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-399 aq.id(R.id.image1).image(imageUrl, true, true, 200, 0); 2.4 如果下载图片失败,处理的方法:1. 设置一个预定的图片 2. 使imageview不可见或者是gone aq.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里面传入id String 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 image0 : Display the center of the image-1.0 : Display bottom of the imageAQuery.ANCHOR_DYNAMIC : Display image with a top bias for photos. =======================================================ImageOptions options = new ImageOptions();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, new BitmapAjaxCallback(){}); 2.11 异步从文件加载图片,建议使用降低采样率避免oom File file = new File(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, new BitmapAjaxCallback(){ @Override public void 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 ); } 或者: if(isTaskRoot()){ //clean the file cache with advance option long triggerSize = 3000000 ; //大于3M时候开始清除 long targetSize = 2000000 ; //直到少于2M AQUtility . cleanCacheAsync ( this , triggerSize , targetSize ); } 2.21 低内存处理 public class MainApplication extends Application{ @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 属性动画(Property Animation) 完全解析 (上)
- Android,iOS打开手机QQ与指定用户聊天界面
- Android SD卡路径问题以及如何获取SDCard 内存
- Android中 调用图库选择图片的参数详解
- Intent 详解
- dialog以各种动画的形式弹出来
- QQ分享
- Android GridView属性集合
- Android三种播放视频的方式
- 动态增加listview和GridView的高度
- Android开发之Intent跳转到系统应用中的拨号界面、联系人界面、短信界面
- 如何改变Activity在当前任务堆栈中的顺序
- android 打开各种文件(setDataAndType)
- glide的配置
- Android 获取设备各种信息以及其它
- Android代码优化----Application节点的模板写法及UI工具类
- Android滑屏与子控件点击事件处理
- Android 系统广播大全
- Android利用系统广播---监听应用程序安装和卸载
- Android 5.0以后版本打开“有权查看使用情况的应用”
- Android设置Activity背景为透明style
- Android之Activity的四种启动模式
- Android获取Manifest中
元素的值 - android权限
- Android~获取view在屏幕中的位置
- 保持屏幕常亮的两种方法:
- android 与js交互
- HTML meta robots 搜索引擎索引方式 - HTML meta 标签 - HTML教程,XHTML教程
- Android获取Manifest中
元素的值