package com.liulishuo.qiniuimageloader.demo;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
import javax.microedition.khronos.opengles.GL10;
/**
* Copyright (c) 2015 LingoChamp Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Created by Jacksgong on 15/8/3.
*
* @Api: http://developer.qiniu.com/docs/v6/api/reference/fop/image/imageview2.html
*/
public class JockImageLoader<T extends JockImageLoader> {
private final static String TAG = "QiniuImageLoader";
private int mode = MODE_FIT_XY;
private int w = 0;
private int h = 0;
private String oriUrl;
private ImageView imageView;
/**
* 限定缩略图的宽最少为<Width>,高最少为<Height>,进行等比缩放,居中裁剪。
* <p/>
* 转后的缩略图通常恰好是 <Width>x<Height> 的大小(有一个边缩放的时候会因为超出矩形框而被裁剪掉多余部分)。
* <p/>
* 如果只指定 w 参数或只指定 h 参数,代表限定为长宽相等的正方图。
* <p/>
* 强制使用给定w与h
*/
private final static int MODE_CENTER_CROP = 1;
/**
* 限定缩略图的宽最多为<Width>,高最多为<Height>,进行等比缩放,不裁剪。
* <p/>
* 如果只指定 w 参数则表示限定宽(长自适应),只指定 h 参数则表示限定长(宽自适应)。
* <p/>
* 如果给定的w或h大于原图,则采用原图
*/
private final static int MODE_FIT_XY = 2;
/**
* 强制需要原图,通常是图片需要动态缩放才需要
*/
private final static int MODE_FORCE_ORIGIN = -1;
public JockImageLoader(final Context context, final String oriUrl) {
this.context = context;
this.oriUrl = oriUrl;
}
public JockImageLoader(final ImageView imageView, final String oriUrl) {
this.imageView = imageView;
this.oriUrl = oriUrl;
}
/**
* 指定最大宽度 为w
*
* @param w
* @return
*/
public T w(final int w) {
this.w = w;
return (T) this;
}
/**
* 指定最大宽度
*
* @param wResource DimenRes
* @return
*/
public T wR(final int wResource) {
if (getContext() == null) {
return (T) this;
}
this.w = getContext().getResources().getDimensionPixelSize(wResource);
return (T) this;
}
/**
* 指定最大宽高
*
* @param size
* @return
*/
public T size(final int size) {
w(size);
h(size);
return (T) this;
}
/**
* 指定最大宽高
*
* @param sizeResource DimenRes
* @return
*/
public T sizeR(final int sizeResource) {
if (getContext() == null) {
return (T) this;
}
final int size = getContext().getResources().getDimensionPixelSize(sizeResource);
size(size);
return (T) this;
}
/**
* 指定最大高度
*
* @param h
* @return
*/
public T h(final int h) {
this.h = h;
return (T) this;
}
/**
* 指定最大高度
*
* @param hResource DimenRes
* @return
*/
public T hR(final int hResource) {
if (getContext() == null) {
return (T) this;
}
this.h = getContext().getResources().getDimensionPixelSize(hResource);
return (T) this;
}
/**
* 指定模式为mode
*
* @param mode
* @return
*/
public T mode(final int mode) {
this.mode = mode;
return (T) this;
}
/**
* {@link #mode} to {@link #MODE_FIT_XY}
* <p/>
* 指定模式为FitXY
*
* @return
* @see <url></url>https://github.com/lingochamp/QiniuImageLoader</url>
*/
public T fitXY() {
return mode(MODE_FIT_XY);
}
/**
* 指定模式为CenterCrop
*
* @return
* @see <url>https://github.com/lingochamp/QiniuImageLoader</url>
*/
public T centerCrop() {
return mode(MODE_CENTER_CROP);
}
/**
* 请求图片最大宽高为GL10.GL_MAX_TEXTURE_SIZE
*
* @return
* @see #MODE_FORCE_ORIGIN
*/
public T forceOrigin() {
return mode(MODE_FORCE_ORIGIN);
}
/**
* 根据七牛提供的API生成目标URL
*
* @return 目标URL
* @see <url>http://developer.qiniu.com/docs/v6/api/reference/fop/image/imageview2.html</url>
*/
public String createQiniuUrl() {
String u = this.oriUrl;
if (!isUrl(u)) {
return u;
}
int width = this.w;
int height = this.h;
final int maxWidth = getScreenWith();
final int maxHeight = getMaxHeight();
if (this.mode == MODE_FORCE_ORIGIN) {
width = GL10.GL_MAX_TEXTURE_SIZE;
height = GL10.GL_MAX_TEXTURE_SIZE;
} else {
// 其中一个有效
width = (height <= 0 && width > 0 && width > maxWidth) ? maxWidth : width;
height = (width <= 0 && height > 0 && height > maxHeight) ? maxHeight : height;
// 两个都无效
width = (width <= 0 && height <= 0) ? maxWidth : width;
//两个都有效
if (width > 0 && height > 0) {
if (width > maxWidth) {
height = (int) (height * ((float) maxWidth / width));
width = maxWidth;
}
if (height > maxHeight) {
width = (int) (width * ((float) maxHeight / height));
height = maxHeight;
}
}
}
// size /thumbnail/<width>x<height>[>最大宽高/<最小宽高]
// /thumbnail/[!]<width>x<height>[r] 限定短边,生成不小于<width>x<height>的 默认不指定!与r,为限定长边
// %3E = URLEncoder.encoder(">", "utf-8")
final String maxHeightUtf8 = "%3E";
String resizeParams = "";
if (width > 0 || height > 0) {
if (width <= 0) {
// h > 0
resizeParams = this.mode == MODE_FORCE_ORIGIN || this.mode == MODE_FIT_XY ?
// fit xy
String.format("/thumbnail/x%d%s", height, maxHeightUtf8) :
// center crop
String.format("/thumbnail/!%dx%dr/gravity/Center/crop/%dx%d", height, height, height, height);
} else if (height <= 0) {
// w > 0
resizeParams = this.mode == MODE_FORCE_ORIGIN || this.mode == MODE_FIT_XY ?
// fit xy
String.format("/thumbnail/%dx%s", width, maxHeightUtf8) :
// center crop
String.format("/thumbnail/!%dx%dr/gravity/Center/crop/%dx%d", width, width, width, width);
} else {
// h > 0 && w > 0
resizeParams = this.mode == MODE_FORCE_ORIGIN || this.mode == MODE_FIT_XY ?
// fit xy
String.format("/thumbnail/%dx%d%s", width, height, maxHeightUtf8) :
// center crop
String.format("/thumbnail/!%dx%dr/gravity/Center/crop/%dx%d", width, height, width, height);
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
对于Android应用而言,图片展示在绝大多数的应用上已经是必备的功能,我们的应用也是如此。那么图片加载优化层面就会有一堆点需要考虑,它决定一款应用是否足够轻盈: 如何开发一款本地的行之有效,性能卓越的图片加载库(优化资源竞争?封装简洁的接口?强大的图片处理功能?),亦或是如何选择开源优秀的图片加载库呢?(picasso?fresco?glide?bala.bala.) 如何杜绝OOM(565?Activity空壳化?largeHeep?nodpi?独立进程?) 更深次的优化,本地cache高斯模糊、旋转、缩放后的结果,归类?管理?预加载?用base64直接带了一些缩略图在普通的REST请求中,对于一些小缩略图做法还是挺有效的,减少连接数,减轻了各类硬件资源冗余使用? 其实很多时候,我们在客户端做了很多工作,却忽略了我们完全可以将这些操作放到云端处理,很多结果存在客户端,还不如存在最近的节点上,很多操作在多个客户端上操作,还不如在云端一次性操作,不但为用户节流,而且稳定,节约客户端资源,简单。本库尝试从架构层结合七牛的库进行封装。
资源推荐
资源详情
资源评论
收起资源包目录
图片加载处理.rar (58个子文件)
main
.project 848B
project.properties 563B
src
com
liulishuo
qiniuimageloader
demo
PicassoLoader.java 4KB
JockImageLoader.java 14KB
MainActivity.java 2KB
PicassoJockImageLoader.java 5KB
AndroidManifest.xml 798B
res
mipmap-hdpi
ic_launcher.png 3KB
drawable-ldpi
mipmap-xxxhdpi
ic_launcher.png 10KB
mipmap-xhdpi
ic_launcher.png 5KB
drawable-hdpi
menu
menu_main.xml 300B
mipmap-mdpi
ic_launcher.png 2KB
drawable-mdpi
layout
activity_main.xml 2KB
drawable-xhdpi
bs_logo.png 7KB
psb.jpg 151KB
values
dimen.xml 109B
colors.xml 251B
strings.xml 215B
styles.xml 139B
mipmap-xxhdpi
ic_launcher.png 8KB
.settings
org.eclipse.jdt.core.prefs 208B
org.eclipse.core.resources.prefs 301B
assets
gen
com
liulishuo
qiniuimageloader
demo
BuildConfig.java 177B
R.java 2KB
libs
picasso-2.5.2.jar 118KB
.classpath 475B
bin
resources.ap_ 188KB
MainActivity.apk 247KB
classes
com
liulishuo
qiniuimageloader
demo
R$layout.class 454B
R$style.class 446B
R$dimen.class 451B
JockImageLoader$Format.class 1KB
BuildConfig.class 379B
R$drawable.class 481B
R$attr.class 391B
R$mipmap.class 452B
PicassoLoader$TargetProvider.class 352B
JockImageLoader$OpName.class 1KB
R$id.class 595B
JockImageLoader$Op.class 2KB
PicassoJockImageLoader.class 5KB
R.class 975B
R$string.class 521B
JockImageLoader.class 10KB
R$menu.class 444B
PicassoLoader.class 4KB
MainActivity.class 3KB
R$color.class 554B
AndroidManifest.xml 798B
dexedLibs
picasso-2.5.2-b468f6d843e286910f95bfc6f2f37b07.jar 51KB
jarlist.cache 120B
res
crunch
mipmap-hdpi
ic_launcher.png 3KB
mipmap-xxxhdpi
ic_launcher.png 9KB
mipmap-xhdpi
ic_launcher.png 4KB
mipmap-mdpi
ic_launcher.png 2KB
drawable-xhdpi
bs_logo.png 6KB
mipmap-xxhdpi
ic_launcher.png 7KB
classes.dex 146KB
共 58 条
- 1
资源评论
- 拾荒者老大2016-09-21找到资料了,没看这个.谢谢! 评价得积分
- 张路2016-05-18不错 。加油
山寨戴王
- 粉丝: 163
- 资源: 23
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功