回忆
-
- xutils:
快速开发型框架
,DbUtils(orm),ViewUtils(ioc),HttpUtils,BitmapUtils -
- 其他的快速开发型框架:andBase,thinkandroid,loonandroid,dhroid
-
- orm:对象关系型映射 就是面向对象操作数据
-
- db:create table ttable(id integer primary key autoincret...);
- insert-->save(obj)
- ioc:控制反转 是javaspring三大框架里面的 通过注解的查找
-
- Obj obj = new Obj();
- 对象的实例化,不用new关键字就可以了吧.
- volley
是一种
通信框架
,和xutils中的HttpUtils,BitmapUtils
- 因为它是google出的,google 在2013 i/o大会上提出来的.
- 而且在几个项目里面已经看到了它的身影
-
- google公司为什么会去搞一个volley框架?
-
- 1. 用户开启一个activity,然后加载网络,这个时候.如果用户点击了finish按钮.activity被销毁了-->网络请求和activity的生命周期是应该联动起来的.
- 2. listview加载图片的情况比较多.如果用户快速的去滑动listview-->getView->快速的加载图片,用户停止操作的时候.其实真正现实的图片
- 最多就几张--->图片应该缓存起来(内存 +本地 )
- 3. 如果用户打开了一个activity,用户旋转了一下屏幕.activity会旋转-->生命周期重走了-->网络请求缓存
- 4. 之前我们的网络请求,httpurlconnection,httpclient,asynctask(api)-->android sdk-->封装性不够好.
- 1000个开发者就有1000种使用方式-->不够统一
- 5. 理念很容易理解,是开源的.
- Request:一个请求
-
- StringRequest:请求的时候直接回来一个String
- JsonObjectRequest:请求的时候直接回来一个JsonObject
- JsonArrayRequest:请求的时候直接回来一个JsonArray
- ImageRequest:请求的时候直接回来一个Bitmap
- 自定义请求:一会我们会结合gson
- ImageLoader:图片的加载器
- NetWorkImageView:继承了imageView,对ImageView进行了拓展
- 是对smartimage的封装 一样的
- RequestQueue:请求队列.
JsonObject取值
- String origin = response.getString("origin");// 方式一
-
- 这个如果没有对应的key会抛异常.需要异常处理
- String origin = response.optString("origin");// 方式二
-
- 这个如果没有对应的key不会抛异常.会返回一个默认值
- optString:默认值""
- optInt:默认值 0
- 比如有的实体bean属性很多.我们不喜欢去建议对应的XXX.class的时候.可以使用JsonObject里面的这个方法;
图片
- 大图片的处理:
-
- 大图片处理的核心
- 核心类:BitmapFactory.Options
- 核心方法:
-
- decodeOptions.inJustDecodeBounds = true;-->得到图片的宽度以及高度,不需要把图片加载到内存 10M
- decodeOptions.inJustDecodeBounds = false;-->真正去加载图片
- decodeOptions.inSampleSize-->采样率-->不同的框架有不同的核心算法
-
- 特点:2 4 8 -->取值最好使用2的指数
- 图片的加载
- 图片的缓存
- 不同框架的算法实现 核心就是
SampleSize
框架找SampleSize的方式
-
volley
static int findBestSampleSize( int actualWidth, int actualHeight, int desiredWidth, int desiredHeight) { double wr = (double) actualWidth / desiredWidth; double hr = (double) actualHeight / desiredHeight; double ratio = Math.min(wr, hr); float n = 1.0f; while ((n * 2) <= ratio) { n *= 2; } return (int) n; }
-
Xutils
public static int calculateInSampleSize(BitmapFactory.Options options, int maxWidth, int maxHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (width > maxWidth || height > maxHeight) { if (width > height) { inSampleSize = Math.round((float) height / (float) maxHeight); } else { inSampleSize = Math.round((float) width / (float) maxWidth); } final float totalPixels = width * height; final float maxTotalPixels = maxWidth * maxHeight * 2; while (totalPixels / (inSampleSize * inSampleSize) > maxTotalPixels) { inSampleSize++; } } return inSampleSize;
}
-
uil_imageLoader
public static int computeMinImageSampleSize(ImageSize srcSize) { final int srcWidth = srcSize.getWidth(); final int srcHeight = srcSize.getHeight(); final int targetWidth = maxBitmapSize.getWidth(); final int targetHeight = maxBitmapSize.getHeight(); final int widthScale = (int) Math.ceil((float) srcWidth / targetWidth); final int heightScale = (int) Math.ceil((float) srcHeight / targetHeight); return Math.max(widthScale, heightScale); // max } public static int computeImageSampleSize(ImageSize srcSize, ImageSize targetSize, ViewScaleType viewScaleType, boolean powerOf2Scale) { final int srcWidth = srcSize.getWidth(); final int srcHeight = srcSize.getHeight(); final int targetWidth = targetSize.getWidth(); final int targetHeight = targetSize.getHeight(); int scale = 1; switch (viewScaleType) { case FIT_INSIDE: if (powerOf2Scale) { final int halfWidth = srcWidth / 2; final int halfHeight = srcHeight / 2; while ((halfWidth / scale) > targetWidth || (halfHeight / scale) > targetHeight) { // || scale *= 2; } } else { scale = Math.max(srcWidth / targetWidth, srcHeight / targetHeight); // max } break; case CROP: if (powerOf2Scale) { final int halfWidth = srcWidth / 2; final int halfHeight = srcHeight / 2; while ((halfWidth / scale) > targetWidth && (halfHeight / scale) > targetHeight) { // && scale *= 2; } } else { scale = Math.min(srcWidth / targetWidth, srcHeight / targetHeight); // min } break; } if (scale < 1) { scale = 1; } scale = considerMaxTextureSize(srcWidth, srcHeight, scale, powerOf2Scale); return scale;
}