模糊算法参考:

http://blog.csdn.net/markl22222/article/details/10313565

网上很多,这东西是个概念理解,没有什么新鲜的。

轮子有现成的,模糊算法无非是java和jni实现。有兴趣可以自己写一遍。这里直接用现成的了。

compile 'net.qiujuer.genius:blur:2.0.0-beta4'

实现在头部虚化。1.获得bitmap,2.对bitmap处理生成虚化图像,3.应用。

获取图片,可以http请求完成。但现在都是图片框架,没有必重新去写下载。

此处使用Glide下载 。代码如下:

Glide.with(getActivity()).load("http://img4.duitang.com/uploads/item/201509/04/20150904010805_inyuE.jpeg")
.asBitmap().into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
Bitmap bitmap =StackBlur.blurNativelyPixels(resource, 50, false);
//set to imageview
}
});

是不是很轻松~

代码运行起来。

图片处理还是比较慢的。你会发现,背景会慢一会儿出现,也还算正常 。

但后来想想,Glide给了transform()方法,还是有缓存的。

为什么不用。然后代码改为:

ackage com.lechang.utils;

import android.content.Context;
import android.graphics.Bitmap; import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation; import net.qiujuer.genius.blur.StackBlur; public class GlideBlurTransform extends BitmapTransformation {
public GlideBlurTransform(Context context) {
super(context);
} @Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return blurImage(pool, toTransform);
} private static Bitmap blurImage(BitmapPool pool, Bitmap source) {
if (source == null) return null;
return StackBlur.blurNativelyPixels(source, 50, false);
} @Override
public String getId() {
return getClass().getName();
}
}
Glide.with(getActivity())
.load("http://img4.duitang.com/uploads/item/201509/04/20150904010805_inyuE.jpeg")
.transform(new GlideBlurTransform(getActivity()))
.into(ivBg);

这样有了缓存,每次进来也不用等了。

现在来实现

斜着的一块白色背景

简单的方法美术切一块白色多边形往上面一铺。

但我觉得应该有好的方法,还省资源。那就自定义一个bitmap

public static Bitmap getPathBitmap(int w, int h) {

        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
Paint paint = new Paint();
Path path = new Path();
path.moveTo(0, 2 * h / 3); // 此点为多边形的起点
path.lineTo(w, 0);
path.lineTo(w, h);
path.lineTo(0, h);
// 使这些点构成封闭的多边形
path.close();
Canvas canvas = new Canvas(bitmap);
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
// 绘制这个多边形
canvas.drawPath(path, paint);
return bitmap;
}

最后圆形头像,不多说了,看这篇:http://www.cnblogs.com/mamamia/p/7814404.html

切圆算法:

  private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null; int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2; // TODO this could be acquired from the pool too
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size); Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
} Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
return result;
}

最终效果

最新文章

  1. 【分布式】Zookeeper序列化及通信协议
  2. Nop中的Cache浅析
  3. python 2.6 与 2.4 区别
  4. js笔记——js里的null和undefined
  5. MaterialCalendarView使用时遇到的问题
  6. flex中通过sprite在地图上画柱状图主要代码
  7. 写过的HTML标签(一)
  8. Action 操作
  9. $response-&gt;decoded_content; 和$response-&gt;content; 乱码问题
  10. 自定义的插件如何加载到Qt Designer中(详细)
  11. thinkphp 的两种建构模式 第一种一个单入口里面定义两个模块,前台和后台,函数控制模块必须function.php前台加载前台模块的汉书配置文件,后台加载后台模块的汉书配置文件,公共文件共用。第二种架构模式两个单入口文件,分别生成两个应用定义define。。。函数可以定义配置文件。。。。
  12. User already has more than &#39;max_user_connections&#39; active connections
  13. json对象的简单介绍
  14. zabbix监控Elasticsearch集群
  15. storm中的Scheduler
  16. openlayers二:添加矢量图形文字
  17. ajaxJson(常用)
  18. 大规模数据导入和导出(oracle)
  19. error: invalid use of void expression
  20. 移动端设置, mobile , 一张图片作为背景 ,平铺 ,自动拉伸 , 图片 铺满视界 ,窗口. background-image , background-size, background-repeat

热门文章

  1. Caffe之prototxt
  2. Nginx如何配置https证书?
  3. 工作中apache 403的一个小问题
  4. 2019-11-29-asp-dotnet-core-通过图片统计-csdn-用户访问
  5. Hive入门指南
  6. 青风nrf52832跑zephyr——点亮LED
  7. vi 必须要学会的技能
  8. docker 安装 mxnet
  9. mysql语句修改zencart产品原价为特价的倍数
  10. shell中的控制流结构