Introducing AQuery: jQuery for Android

简介:
一、内存溢出
现在的智能手机内存已经足够大,但是对于一个应用程序来说智能手机当中稀缺的内存,仍然是应用程序的一大限制。在Android应用程序开发当中,最常见的内存溢出问题(OOM)是在加载图片时出现的,尤其是在不知道图片大小的情况下。
潜在的内存溢出操作主要包括以下几点:
从网络当中加载用户特定的图片。因为直到我们在下载图片的时候我们才知道图片的大小。
向Gallery加载图片。因为现在智能手机的摄像头有很高的分辨率,在加载图片的时候需要最图片进行处理,然后才能正常的使用。
请注意一点,Android系统是从系统全局的观念来分配内存以加载图片的,这就意味着,即使你的应用有足够大的内存可用,内存溢出问题(out of memroy,OOM)仍然可能出现,因为所有的应用共享一个加载图片的内存池(我们使用BitmapFactory进行解析)。
二、解决内存溢出问题
原文(Downsampling为了好理解,解释为,程序A)。程序A通过调整相邻的像素,同时使其均衡化来降低图片的分辨率。因为不管问题图片是因为太大而不能再手机上正常显现,这个图片都会缩短其宽度以在ImageView当中显示,当图片在ImageView当中显示时,我们会因为加载一些没有必要的原始图片而浪费掉内存。
因此,更加有效的加载图片的时机是在其初始化处理的时候。

以下是处理代码

private static Bitmap getResizedImage(String path, byte[] data, int targetWidth){
 
     
 
    BitmapFactory.Options options = new BitmapFactory.Options();
 
     options.inJustDecodeBounds = true;
 
     
 
     BitmapFactory.decodeFile(path, options);
 
     
 
     int width = options.outWidth;
 
     
 
     int ssize = sampleSize(width, targetWidth);
 
    
    
     options = new BitmapFactory.Options();
 
     options.inSampleSize = ssize;
 
     
 
     Bitmap bm = null;
 
     try{
 
         bm = decode(path, data, options);
 
     }catch(OutOfMemoryError e){
 
         AQUtility.report(e);
 
     }
 
     
 
     
 
     return bm;
 
     
 
}
 
  
 
private static int sampleSize(int width, int target){
 
     
 
     int result = 1;
 
     
 
     for(int i = 0; i < 10; i++){
 
         
 
         if(width < target * 2){
 
             break;
 
        }
 
         
 
         width = width / 2;
 
         result = result * 2;
 
         
 
     }
 
     
 
     return result;
 
}
三、AQuery
当在Android应用程序开发当中使用AQuery组件时,处理这个问题会变的更加的简单。
1、当从网络当中下载图片时,我们仅仅需要以下的几句代码:

  1. String url = "http://farm6.static.flickr.com/5035/5802797131_a729dac808_b.jpg";
  2. aq.id(R.id.image1).image(imageUrl, true, true, 200, 0);

2、当加载已有图片时,我们需要的代码如下:

  1. File file = new File(path);
  2. //load image from file, down sample to target width of 300 pixels
  3. aq.id(R.id.avatar).image(file, 300);

相关文章
|
Java Linux Android开发
windows编译FFmpeg for Android 和AndroidStudio使用FFmpeg(二)
FFmpeg的编译是一个大坑,尤其是编译安卓平台的动态库和静态库,应用于APP中。在Linux平台编译是相对简单的,但是我经过尝试在Linux编译静态库没有成功,所以又在windows平台尝试编译了ffempg的动态库,应用成功了,这里分享一下。
519 0
windows编译FFmpeg for Android 和AndroidStudio使用FFmpeg(二)
|
Linux Shell C语言
windows编译FFmpeg for Android 和AndroidStudio使用FFmpeg(一)
FFmpeg的编译是一个大坑,尤其是编译安卓平台的动态库和静态库,应用于APP中。在Linux平台编译是相对简单的,但是我经过尝试在Linux编译静态库没有成功,所以又在windows平台尝试编译了ffempg的动态库,应用成功了,这里分享一下。
902 0
windows编译FFmpeg for Android 和AndroidStudio使用FFmpeg(一)
|
IDE 开发工具 Android开发
解决This Gradle plugin requires a newer IDE able to request IDE model level 3. For Android Studio
解决This Gradle plugin requires a newer IDE able to request IDE model level 3. For Android Studio
240 0
解决This Gradle plugin requires a newer IDE able to request IDE model level 3. For Android Studio
|
存储 缓存 JSON
Code For Better 谷歌开发者之声——Android 中的 Volley 库
Volley是一个HTTP 库,它使 Android 应用程序的网络变得非常简单和快速。它由 Google 开发并在 2013 年 Google I/O 期间推出。它的开发是因为 Android SDK 中缺少能够在不影响用户体验的情况下工作的网络类。尽管 Volley 是 Android 开源项目 (AOSP) 的一部分,但 Google 在 2017 年 1 月宣布 Volley 将迁移到一个独立的库。它管理网络请求的处理和缓存,并节省开发人员一次又一次编写相同的网络调用/缓存代码的宝贵时间。Volley不适合大型下载或流式操作,因为 Volley 在解析期间将所有响应保存在内存中。
281 0
|
存储 人工智能 Java
TensorFlow Lite for Android 初探(附demo)
TensorFlow Lite for Android 初探(附demo)
596 0
TensorFlow Lite for Android 初探(附demo)
|
开发工具 Android开发
License for package Android SDK Build-Tools 28.0.3 not accepted.
License for package Android SDK Build-Tools 28.0.3 not accepted.
414 0
License for package Android SDK Build-Tools 28.0.3 not accepted.
|
开发工具
Could not get unknown property ‘versions‘ for object of type com.android.build.gradle.AppExtension
Could not get unknown property ‘versions‘ for object of type com.android.build.gradle.AppExtension
1803 0
Could not get unknown property ‘versions‘ for object of type com.android.build.gradle.AppExtension
|
开发工具 Android开发
解决Error:Could not determine the class-path for interface com.android.builder.model.AndroidProject.
解决Error:Could not determine the class-path for interface com.android.builder.model.AndroidProject.
274 0
解决Error:Could not determine the class-path for interface com.android.builder.model.AndroidProject.
|
消息中间件 网络协议 物联网
微服务消息队列(MQTT For IoT)Android Demo使用介绍
目前阿里云官方对于微消息队列 MQTT提供了很多语言的参考示例,但是在实际的使用中发现很多用户在使用Android Sample的时候总是会遇到问题,无法正常调试使用。本文主要介绍Android Sample的使用。
微服务消息队列(MQTT For IoT)Android Demo使用介绍
|
消息中间件 物联网 Android开发
微服务消息队列(MQTT For IoT)Android Demo使用介绍
目前阿里云官方对于微消息队列 MQTT提供了很多语言的参考示例,但是在实际的使用中发现很多用户在使用Android Sample的时候总是会遇到问题,无法正常调试使用。本文主要介绍Android Sample的使用。
3251 0

热门文章

最新文章