使用一:静态控件上使用

  1. 先附上自定义view-BlurringView
public class BlurringView extends View {private int mDownsampleFactor;private int mOverlayColor;private View mBlurredView;private int mBlurredViewWidth, mBlurredViewHeight;private boolean mDownsampleFactorChanged;private Bitmap mBitmapToBlur, mBlurredBitmap;private Canvas mBlurringCanvas;private RenderScript mRenderScript;private ScriptIntrinsicBlur mBlurScript;private Allocation mBlurInput, mBlurOutput;public BlurringView(Context context) {this(context, null);}public BlurringView(Context context, AttributeSet attrs) {super(context, attrs);final Resources res = getResources();final int defaultBlurRadius = res.getInteger(R.integer.default_blur_radius);final int defaultDownsampleFactor = res.getInteger(R.integer.default_downsample_factor);final int defaultOverlayColor = res.getColor(R.color.default_overlay_color);initializeRenderScript(context);TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PxBlurringView);setBlurRadius(a.getInt(R.styleable.PxBlurringView_blurRadius, defaultBlurRadius));setDownsampleFactor(a.getInt(R.styleable.PxBlurringView_downsampleFactor,defaultDownsampleFactor));setOverlayColor(a.getColor(R.styleable.PxBlurringView_overlayColor, defaultOverlayColor));a.recycle();}public void setBlurredView(View blurredView) {mBlurredView = blurredView;}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);if (mBlurredView != null) {if (prepare()) {// If the background of the blurred view is a color drawable, we use it to clear// the blurring canvas, which ensures that edges of the child views are blurred// as well; otherwise we clear the blurring canvas with a transparent color.if (mBlurredView.getBackground() != null && mBlurredView.getBackground() instanceof ColorDrawable) {mBitmapToBlur.eraseColor(((ColorDrawable) mBlurredView.getBackground()).getColor());} else {mBitmapToBlur.eraseColor(Color.TRANSPARENT);}mBlurredView.draw(mBlurringCanvas);blur();canvas.save();canvas.translate(mBlurredView.getX() - getX(), mBlurredView.getY() - getY());canvas.scale(mDownsampleFactor, mDownsampleFactor);canvas.drawBitmap(mBlurredBitmap, 0, 0, null);canvas.restore();}canvas.drawColor(mOverlayColor);}}public void setBlurRadius(int radius) {mBlurScript.setRadius(radius);}public void setDownsampleFactor(int factor) {if (factor <= 0) {throw new IllegalArgumentException("Downsample factor must be greater than 0.");}if (mDownsampleFactor != factor) {mDownsampleFactor = factor;mDownsampleFactorChanged = true;}}public void setOverlayColor(int color) {mOverlayColor = color;}private void initializeRenderScript(Context context) {mRenderScript = RenderScript.create(context);mBlurScript = ScriptIntrinsicBlur.create(mRenderScript, Element.U8_4(mRenderScript));}protected boolean prepare() {final int width = mBlurredView.getWidth();final int height = mBlurredView.getHeight();if (mBlurringCanvas == null || mDownsampleFactorChanged|| mBlurredViewWidth != width || mBlurredViewHeight != height) {mDownsampleFactorChanged = false;mBlurredViewWidth = width;mBlurredViewHeight = height;int scaledWidth = width / mDownsampleFactor;int scaledHeight = height / mDownsampleFactor;// The following manipulation is to avoid some RenderScript artifacts at the edge.scaledWidth = scaledWidth - scaledWidth % 4 + 4;scaledHeight = scaledHeight - scaledHeight % 4 + 4;if (mBlurredBitmap == null|| mBlurredBitmap.getWidth() != scaledWidth|| mBlurredBitmap.getHeight() != scaledHeight) {mBitmapToBlur = Bitmap.createBitmap(scaledWidth, scaledHeight,Bitmap.Config.ARGB_8888);if (mBitmapToBlur == null) {return false;}mBlurredBitmap = Bitmap.createBitmap(scaledWidth, scaledHeight,Bitmap.Config.ARGB_8888);if (mBlurredBitmap == null) {return false;}}mBlurringCanvas = new Canvas(mBitmapToBlur);mBlurringCanvas.scale(1f / mDownsampleFactor, 1f / mDownsampleFactor);mBlurInput = Allocation.createFromBitmap(mRenderScript, mBitmapToBlur,Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);mBlurOutput = Allocation.createTyped(mRenderScript, mBlurInput.getType());}return true;}protected void blur() {mBlurInput.copyFrom(mBitmapToBlur);mBlurScript.setInput(mBlurInput);mBlurScript.forEach(mBlurOutput);mBlurOutput.copyTo(mBlurredBitmap);}@Overrideprotected void onDetachedFromWindow() {super.onDetachedFromWindow();if (mRenderScript != null) {mRenderScript.destroy();}}
}
  1. 调用自定义view
<?xml version="1.0" encoding="utf-8"?>
<FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#FFFFFFFF"tools:context=".SecondActivity"><ImageView android:id="@+id/iv_blur"android:src="@mipmap/image5"android:layout_width="match_parent"android:layout_height="match_parent"android:scaleType="fitXY"/><com.example.maoboli.maobolidemo.BlurringViewandroid:id="@+id/blurring_view"android:layout_width="260dp"android:layout_height="200dp"android:layout_gravity="center"android:layout_marginBottom="80dip"app:blurRadius="20"app:downsampleFactor="6"app:overlayColor="#26FFFFFF"/><Buttonandroid:id="@+id/shuffle_button"android:layout_width="100dp"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginBottom="120dp"android:text="模糊头像"/><Buttonandroid:id="@+id/suibian"android:layout_width="100dp"android:layout_height="wrap_content"android:layout_below="@id/shuffle_button"android:layout_gravity="center"android:layout_marginTop="30dp"android:layout_marginBottom="80dip"android:text="随便点"/></FrameLayout>
  1. 编辑activity
      BlurringView mBlurringView = (BlurringView) findViewById(R.id.blurring_view);View  blurredView = findViewById(R.id.iv_blur);// Give the blurring view a reference to the blurred view.mBlurringView.setBlurredView(blurredView);

使用二:模糊头像效果

  1. 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><ImageView android:id="@+id/iv_blur"android:layout_width="match_parent"android:layout_height="200dp" /><ImageView android:id="@+id/iv_avatar"android:layout_width="60dp"android:layout_height="60dp"android:scaleType="fitCenter"android:layout_centerInParent="true"/></RelativeLayout></LinearLayout>
  1. 编辑activity
public class MainActivity extends AppCompatActivity {private ImageView blurImageView;private ImageView avatarImageView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViews();initData();}private void findViews(){blurImageView = (ImageView) findViewById(R.id.iv_blur);avatarImageView = (ImageView) findViewById(R.id.iv_avatar);}private void initData(){Glide.with(this).load(R.mipmap.image).bitmapTransform(new BlurTransformation(this, 30), new CenterCrop(this)).into(blurImageView);Glide.with(this).load(R.mipmap.image).bitmapTransform(new CropCircleTransformation(this)).into(avatarImageView);}
}
  1. 导包
   compile 'com.github.bumptech.glide:glide:3.7.0'compile 'jp.wasabeef:glide-transformations:2.0.1'

使用三:弹出模糊对话框

具体代码参考git上的 Demo

转载于:https://www.cnblogs.com/neo-java/p/10185076.html

【Android开发】毛玻璃效果相关推荐

  1. [deviceone开发]-毛玻璃效果示例

    一.简介 do_Bitmap组件可以把图片加载为内存里的Bitmap对象,能够对这个对象做各种图形化处理.目前只有3种处理,圆角,毛玻璃,灰度.以后会添加更多. 二.效果图 三.相关下载 https: ...

  2. 毛玻璃效果在Android的实现

     本文已授权「玉刚说」微信公众号独家发布 毛玻璃效果实际上是对原图片的严重劣化,突出朦胧感,一般都是通过图片的缩放+模糊算法来实现,从性能角度考虑,模糊半径不能大于25,所以要更高的模糊效果则需要进行 ...

  3. Android开发必备(干货源码放送大)

    Android源码大放送(实战开发必备) 文件夹 PATH 列表 │  javaapk.com文件列表生成工具.bat │  使用说明.txt │  免费下载更多源码.url │  目录列表.txt ...

  4. android 小球效果,Android开发实现跟随手指的小球效果示例

    本文实例讲述了android开发实现跟随手指的小球效果.分享给大家供大家参考,具体如下: 配置drawview类用于绘制小球 public class drawview extends view { ...

  5. android listview下拉动画效果,Android开发中利用ListView实现一个渐变式的下拉刷新动画...

    Android开发中利用ListView实现一个渐变式的下拉刷新动画 发布时间:2020-11-23 16:50:31 来源:亿速云 阅读:80 作者:Leah 本篇文章给大家分享的是有关Androi ...

  6. android 自定义刷新控件,Android开发中MJRefresh自定义刷新动画效果

    有时候我们对自己开发的项目经常不满意,但是我们要达到自定义刷新动画的效果有一定的难度,别着急,下面爱站技术频道和大家分享Android开发中MJRefresh自定义刷新动画效果,一起来学习吧! [一] ...

  7. android 实现磨砂效果_Android 5.0 下毛玻璃(磨砂)效果如何实现?

    刚刚做技术调研,可以给一些优缺点的对比. 目前主流实现毛玻璃效果(高斯模糊)分大致三种方法: 一 利用RenderScript接口 利用现有Android结构,通过RenderScript调用底层接口 ...

  8. android 评论的展开功能,Android开发实现ListView点击展开收起效果示例

    本文实例讲述了Android开发实现ListView点击展开收起效果.分享给大家供大家参考,具体如下: 废话不说先上效果: 实际上这是采用一个ExpandableListView实现的 布局文件很简单 ...

  9. android 开源 高斯模糊_Android实现带毛玻璃效果(高斯模糊)背景的Dialog

    最近换了工作,由于工作中要使用一些自己以前不是很了解的知识,就没有时间更新博客了. 由于最近做了一些很有意思的小demo,不吐不快,再加上还是认为技术需要沉淀和梳理,所以再次把写博客这件事拾起来. 已 ...

  10. Android开发学习之基于ViewPager实现Gallery画廊效果

    通过我们前面的学习,我们知道ViewPager是可以做出近乎完美的滑动体验,回顾整个Android,我们发现Gallery具备同样的特点,于是我们大胆地猜想,Gallery是否和ViewPager之间 ...

最新文章

  1. 翻译 | 摆脱浏览器限制的JavaScript
  2. VTK:IO之ReadPNM
  3. 琥珀ai_琥珀项目:Java的未来暴露
  4. PostgreSQL查看版本信息
  5. qt程序中使用 环境变量_目的:使用CUDA环境变量CUDA_VISIBLE_DEVICES来限定CUDA程序所能使用的GPU...
  6. 【华为云实战开发】2.Docker镜像部署怎么玩才酷炫?
  7. OCA读书笔记(11) - 实现Oracle数据库审计
  8. 多线程跑调度_java多线程中的调度策略
  9. 这种一毛钱值多少钱?
  10. 【C语言】 ASCII码
  11. VMware ESXi 6.7安装过程介绍
  12. C# 实现多种语言切换,通过VS实现
  13. 统计学专业词汇英文翻译中英对照总结汇总(贾俊平 统计学 第七版 )
  14. STM32F1主从定时器设置
  15. Linux上的服务器无法调用新浪邮箱发送邮件
  16. vulnhub Funbox: 1
  17. Unity3d与iOS交互开发—接入平台SDK必备技能
  18. 《地球概论》(第3版)笔记 第三章 地球的运动
  19. c语言函数变量地址符,C语言中取地址符做函数形参?—— 引用的讨论
  20. python爬虫requests的库使用详解

热门文章

  1. BASIC-13 数列排序
  2. 机器人写诗项目——递归神经网络(RNN)
  3. 【Linux】一步一步学Linux——bc命令(233)
  4. 【Linux】一步一步学Linux——pgrep命令(123)
  5. oracle对substr去重,oracle使用笔记
  6. 360全景html插件,jQuery 360度全景图插件 PANORAMA VIEWER
  7. 解决 Cycript 信息显示不全的问题
  8. gcc编译C++程序
  9. centos安装mysql 简书_在centos上安装mysql
  10. C++ vector容器删除操作