android 加载图片圆角等功能的处理

以Glide为例:
Glide.with(getContext()).load(item.getSoftLogo()).transform(this.glideRoundTransform).into(vh.ivSoftIcon);
Glide 提供了transform()方法。
/**
* Transform {@link GlideDrawable}s using the given
* {@link com.bumptech.glide.load.resource.bitmap.BitmapTransformation}s.
*
* <p>
* Note - Bitmap transformations will apply individually to each frame of animated GIF images and also to
* individual {@link Bitmap}s.
* </p>
*
* @see #centerCrop()
* @see #fitCenter()
* @see #bitmapTransform(com.bumptech.glide.load.Transformation[])
* @see #transform(com.bumptech.glide.load.Transformation[])
*
* @param transformations The transformations to apply in order.
* @return This request builder.
*/
public DrawableRequestBuilder<ModelType> transform(BitmapTransformation... transformations) {
return bitmapTransform(transformations);
}
此方法接受 BitmapTransformation 类形参数。
import android.content.Context;
import android.graphics.Bitmap; import com.bumptech.glide.Glide;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.util.Util; /**
* A simple {@link com.bumptech.glide.load.Transformation} for transforming {@link android.graphics.Bitmap}s that
* abstracts away dealing with {@link com.bumptech.glide.load.engine.Resource} objects for subclasses.
*
* Use cases will look something like this:
* <pre>
* <code>
* public class FillSpace extends BaseBitmapTransformation {
* {@literal @Override}
* public Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
* if (toTransform.getWidth() == outWidth && toTransform.getHeight() == outHeight) {
* return toTransform;
* }
*
* return Bitmap.createScaledBitmap(toTransform, outWidth, outHeight, true);
* }
* }
* </code>
* </pre>
*/
public abstract class BitmapTransformation implements Transformation<Bitmap> { private BitmapPool bitmapPool; public BitmapTransformation(Context context) {
this(Glide.get(context).getBitmapPool());
} public BitmapTransformation(BitmapPool bitmapPool) {
this.bitmapPool = bitmapPool;
} @Override
public final Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
if (!Util.isValidDimensions(outWidth, outHeight)) {
throw new IllegalArgumentException("Cannot apply transformation on width: " + outWidth + " or height: "
+ outHeight + " less than or equal to zero and not Target.SIZE_ORIGINAL");
}
Bitmap toTransform = resource.get();
int targetWidth = outWidth == Target.SIZE_ORIGINAL ? toTransform.getWidth() : outWidth;
int targetHeight = outHeight == Target.SIZE_ORIGINAL ? toTransform.getHeight() : outHeight;
Bitmap transformed = transform(bitmapPool, toTransform, targetWidth, targetHeight); final Resource<Bitmap> result;
if (toTransform.equals(transformed)) {
result = resource;
} else {
result = BitmapResource.obtain(transformed, bitmapPool);
} return result;
} /**
* Transforms the given {@link android.graphics.Bitmap} based on the given dimensions and returns the transformed
* result.
*
* <p>
* The provided Bitmap, toTransform, should not be recycled or returned to the pool. Glide will automatically
* recycle and/or reuse toTransform if the transformation returns a different Bitmap. Similarly implementations
* should never recycle or return Bitmaps that are returned as the result of this method. Recycling or returning
* the provided and/or the returned Bitmap to the pool will lead to a variety of runtime exceptions and drawing
* errors. See #408 for an example. If the implementation obtains and discards intermediate Bitmaps, they may
* safely be returned to the BitmapPool and/or recycled.
* </p>
*
* <p>
* outWidth and outHeight will never be {@link com.bumptech.glide.request.target.Target#SIZE_ORIGINAL}, this
* class converts them to be the size of the Bitmap we're going to transform before calling this method.
* </p>
*
* @param pool A {@link com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool} that can be used to obtain and
* return intermediate {@link Bitmap}s used in this transformation. For every
* {@link android.graphics.Bitmap} obtained from the pool during this transformation, a
* {@link android.graphics.Bitmap} must also be returned.
* @param toTransform The {@link android.graphics.Bitmap} to transform.
* @param outWidth The ideal width of the transformed bitmap (the transformed width does not need to match exactly).
* @param outHeight The ideal height of the transformed bitmap (the transformed heightdoes not need to match
* exactly).
*/
protected abstract Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight);
}
BitmapTransformation 虚拟方法
protected abstract Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight); 意思通过此方法转换和处理图片,并返回给Glide一个bitmap.此bitmap Glide也将替你管理。
剩下的只要你去做处理了。 下面做一个图角的处理
通过重写paint绘制叠加的效果来处理。
关键代码如下:
/**
* @param pool
* @param source
* @return
*/
private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null; //获取一张位图用来往上面绘制
Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
} //使用获取的位图新建一张画布
Canvas canvas = new Canvas(result);
//新建一个画笔
Paint paint = new Paint();
paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
canvas.drawRoundRect(rectF, radius, radius, paint);
//先画图片,再画圆角矩形,获取交集
return result;
}
BitmapShader
public BitmapShader(Bitmap bitmap,Shader.TileMode tileX,Shader.TileMode tileY)
调用这个方法来产生一个画有一个位图的渲染器(Shader)。
bitmap 在渲染器内使用的位图
tileX The tiling mode for x to draw the bitmap in. 在位图上X方向花砖模式
tileY The tiling mode for y to draw the bitmap in. 在位图上Y方向花砖模式
TileMode:(一共有三种)
CLAMP :如果渲染器超出原始边界范围,会复制范围内边缘染色。
REPEAT :横向和纵向的重复渲染器图片,平铺。
MIRROR :横向和纵向的重复渲染器图片,这个和REPEAT 重复方式不一样,他是以镜像方式平铺。

处理成黑白 代码如下:
int w = source.getWidth();
int h = source.getHeight(); Bitmap resultBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
int color = 0;
int a,r,g,b,r1,g1,b1;
int[] oldPx = new int[w * h];
int[] newPx = new int[w * h]; source.getPixels(oldPx, 0, w, 0, 0, w, h);
for(int i = 0; i < w * h; i++){
color = oldPx[i]; r = Color.red(color);
g = Color.green(color);
b = Color.blue(color);
a = Color.alpha(color); //黑白矩阵
r1 = (int) (0.33 * r + 0.59 * g + 0.11 * b);
g1 = (int) (0.33 * r + 0.59 * g + 0.11 * b);
b1 = (int) (0.33 * r + 0.59 * g + 0.11 * b); //检查各像素值是否超出范围
if(r1 > 255){
r1 = 255;
} if(g1 > 255){
g1 = 255;
} if(b1 > 255){
b1 = 255;
} newPx[i] = Color.argb(a, r1, g1, b1);
}
resultBitmap.setPixels(newPx, 0, w, 0, 0, w, h);
return resultBitmap;
效果如下:

同理可以加其它滤镜、水印等。
android 加载图片圆角等功能的处理的更多相关文章
- android 加载图片框架--Glide使用详解
一.简介 Glide,一个被google所推荐的图片加载库,作者是bumptech.这个库被广泛运用在google的开源项目中,包括2014年的google I/O大会上发布的官方app.(PS:众所 ...
- Android加载图片OOM错误解决方式
前几天做项目的时候,甲方要求是PAD (SAMSUNG P600 10.1寸 2560*1600)的PAD上显示高分辨率的大图片. SQLITE採用BOLD方式存储图片,这个存取过程就不说了哈,网上一 ...
- Android加载图片的策略
实现图片缓存也不难,需要有相应的cache策略.这里我采用 内存-文件-网络 三层cache机制,其中内存缓存包括强引用缓存和软引用缓存(SoftReference),其实网络不算cache,这里姑且 ...
- 图片--Android加载图片导致内存溢出(Out of Memory异常)
Android在加载大背景图或者大量图片时,经常导致内存溢出(Out of Memory Error),本文根据我处理这些问题的经历及其它开发者的经验,整理解决方案如下(部分代码及文字出处无法考证) ...
- android 加载图片oom若干方案小结
本文根据网上提供的一些技术方案加上自己实际开发中遇到的情况小结. 众所周知,每个Android应用程序在运行时都有一定的内存限制,限制大小一般为16MB或24MB(视手机而定).一般我们可以通过获取当 ...
- Android加载图片导致内存溢出(Out of Memory异常)
Android在加载大背景图或者大量图片时,经常导致内存溢出(Out of Memory Error),本文根据我处理这些问题的经历及其它开发者的经验,整理解决方案如下(部分代码及文字出处无法考证) ...
- android 加载图片防止内存溢出
图片资源: private int fore[]; private int back[]; fore = new int[]{R.drawable.a0, R.drawable.a1, R.drawa ...
- 解决android加载图片时内存溢出问题
尽量不要使用setImageBitmap或setImageResource或BitmapFactory.decodeResource来设置一张大图,因为这些函数在完成decode后,最终都是通过jav ...
- Android加载图片小结
应用中用到图片加载需要解决的问题 无网络环境下图片不可用 图片的本地缓存,或者默认预加载的图片 低配置机型,加载图像资源超内存(OutOfMemory, OoM) 需要合理使用内存,尤其是bitmap ...
随机推荐
- 查找算法(I) 顺序查找 二分查找 索引查找
查找 本文为查找算法的第一部分内容,包括了基本概念,顺序查找.二分查找和索引查找.关于散列表和B树查找的内容,待有空更新吧. 基本概念 查找(search)又称检索,在计算机上对数据表进行查找,就是根 ...
- Python练习----多级菜单
多级菜单要求: 1.三级菜单 2.可依次选择进入各子菜单 3.可以返回上一层 4.输入'q'可以退出 脚本: zone = { '北京' : { ' ...
- 【持续更新】.Net 开发中给自己埋下的坑!
1.文件“XXX”正在由另一进程使用,因此该进程无法访问此文件. 原因剖析:文件在主线程操作,在子线程中读写操作文件,刚开始没有意识到程序的问题所在,总是在FileStream中报错,google后常 ...
- 浅谈 var 关键字
提起 var关键子,程序员的第一反应就是JavaScript, 事实上这个关键子在其他语言中也有被采用. 比如说C#, 比如说kotlin, 用法和JavaScript中使用差不多,作为要声明变量的前 ...
- (转)Java正则表达式的语法与示例
转自:http://www.cnblogs.com/lzq198754/p/5780340.html 概要: Java正则表达式的语法与示例 | |目录 1匹配验证-验证Email是否正确 2在字符串 ...
- js math对象总结
1: Math 对象用于执行数学任务. 2:Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math(), Math.sin() 这样的函数只是函数 3:通过把 ...
- LeetCode 111. Minimum Depth of Binary Tree (二叉树最小的深度)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- ajax+分页
<!DOCTYPE html> <html> <head lang="zh-cn"> <meta charset="UTF-8& ...
- enum(枚举类型)
可以使用枚举类型声明代表整数常量的符号名称. 通过enum,创建一个新类型,并指定它可以拥有的值.(就像平常用一个整形变量,我们指定它等于0的时候代表什么,1呢,2呢...而通过枚举,就增加了程序的可 ...
- HDU1792A New Change Problem(GCD规律推导)
A New Change Problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...