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