添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
  1. 将View转成Bitmap,并且记录列表的滑动距离,生成bitmap时上移画布(这样是为了实现滑到那,模糊那的效果)

    上移画布:一般绘制View,canvas的起始点在屏幕的左上角,也就是(0,0)。当canvas上移距离y后,绘制的起点会变成(0,-y)。这样canvas竖直方向-y到0这一部分会在屏幕外,那么在绘制的时候view的top到top+y是在屏幕外面,我们看不到;我们能看到的部分是从view的top+y开始的,这样就实现了模糊滑动后的列表了。

  2. 使用高斯模糊,对上一步生成的bitmap进行模糊处理

使用RenderScript,下列代码可以直接用。sdk>17可以正常使用。需要兼容sdk<17的话,要增加配置。文末会给参考文章

//使用RenderScript
private static Bitmap rsBlur(Context context, Bitmap source, float radius, float scale){
  int scaleWidth = (int) (source.getWidth() * scale);
  int scaleHeight = (int) (source.getHeight() * scale);
  Bitmap scaledBitmap = Bitmap.createScaledBitmap(source, scaleWidth,
                                                  scaleHeight,false);
  Bitmap inputBitmap = scaledBitmap;
  Log.i("RenderScriptActivity","size:"+inputBitmap.getWidth()+","+inputBitmap.getHeight());
  //创建RenderScript
  RenderScript renderScript = RenderScript.create(context);
  //创建Allocation
  Allocation input = Allocation.createFromBitmap(renderScript, inputBitmap,  Allocation.MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
  Allocation output = Allocation.createTyped(renderScript, input.getType());
  //创建ScriptIntrinsic
  ScriptIntrinsicBlur intrinsicBlur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
  intrinsicBlur.setInput(input);
  intrinsicBlur.setRadius(radius);
  intrinsicBlur.forEach(output);
  output.copyTo(inputBitmap);
  renderScript.destroy();
  return inputBitmap;

View转Bitmap

代码很简单,需要注意的是将canvas上移scrollY。scrollY是gif中看到的列表相对起始位置滑动的距离。

还有生成的Bitmap要使用ARGB_8888格式,RGB_565效果不理想。

Bitmap bitmap = Bitmap.createBitmap(llListWidth, llListHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Log.e("ListViewBlurActivity","useCanvas,scrollY:"+scrollY);
canvas.translate(0,-scrollY);
llList.draw(canvas);
  1. Android 图片高斯模糊解决方案

    关于高斯模糊,很全面的文章,会告诉你如何兼容sdk<17的设备。

  2. Android Canvas translate() 平移

    关于Canvas的translate()。

Android使用高斯模糊实现模糊背景引言最近的开发中实现了一个模糊背景的效果。大概效果是这样的:实现思路将View转成Bitmap,并且记录列表的滑动距离,生成bitmap时上移画布(这样是为了实现滑到那,模糊那的效果)上移画布:一般绘制View,canvas的起始点在屏幕的左上角,也就是(0,0)。当canvas上移距离y后,绘制的起点会变成(0,-y)。这样canvas竖直方向-y到0这一部分会在屏幕外,那么在绘制的时候view的top到top+y是在屏幕外面,我们看不到;我们能看到的
高斯模糊是什么? 高斯模糊(英语:Gaussian Blur),也叫高斯平滑,是在Adobe Photoshop、GIMP以及Paint.NET等图像处理软件中广泛使用的处理效果,通常用它来减少图像噪声以及降低细节层次。这种模糊技术生成的图像,其视觉效果就像是经过一个半透明屏幕在观察图像,这与镜头焦外成像效果散景以及普通照明阴影中的效果都明显不同。 什么?看不明白?没关系,我也看不明白,维基百科复制回来的嘛。我们直接放一些图片来了解以下这个高斯模糊是怎么样的。因为高斯模糊在iOS中最常见,这里抓了几张iOS网易云的图片: 可以看到这个界面中的背景,其实就是通过图1中间那个小图片模糊得到的,
最近做项目有这样的需求: 在activity中启动一个dialog时, 启动的dialog的背景设为启动acitivity的模糊化图片.实现思路: 1. 截屏, 获取当前activity的界面 2. 将获取的照片进行模糊化 3. 将模糊化的图片设为dialog的背景1.截屏, 获取当前activity的界面 private Bitmap takeScreenShot(Activity a
Android 中,Dialog 的高斯模糊指的是将 Dialog 界面进行模糊处理,使得 Dialog 呈现出一种模糊、朦胧的效果。 高斯模糊是一种常见的图像处理技术,通过对图像中的像素进行加权平均来减少图像的细节和噪点,从而达到模糊效果。在 Dialog 中使用高斯模糊可以用来实现一些特殊的视觉效果,或者提供一种更加柔和的界面体验。 在 Android 中,实现 Dialog 的高斯模糊通常有两种方式: 使用系统提供的 DialogFragment:可以在 DialogFragment 中使用
在现在好多背景图片都流行用高斯模糊图片做背景,比如音乐播放器页面,SystemUI下拉状态栏背景 好多都流行高斯模糊 高斯模糊的几种实现方式: (1)RenderScript RenderScript是Google在Android 3.0(API 11)中引入的一个高性能图片处理框架。 使用RenderScriprt实现高斯模糊: (2) 使用Glide实现高斯模糊 第一种RenderScipt实现高斯模糊 public Bitmap blurBitmap(Context context, Bitmap b
.error(R.drawable.error_img) // 设置高斯模糊 .bitmapTransform(new BlurTransformation(this, 14, 3)) .into(imageview); 适用场景:动态配置的背景图片 2、对图片高斯模糊,需要先将图片转成bitmap对象 mport android.annotation.TargetApi; import a 1.方法的实现: public static void updateBgToBlur(Activity a, Bitmap bmpToBlur, View view, int resId) { BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inJustDecodeBounds = true; opt.inSampleSize = 8; opt.inJustDecodeBounds = false; Bitmap bmp = BitmapFa
实现高斯模糊背景的对话框,可以使用 Android 的 DialogFragment 和 RenderScript 实现。以下是大致的步骤: 1. 在布局文件中定义对话框的 UI,注意要为对话框添加一个半透明的背景。 2. 创建一个继承自 DialogFragment 的类,并在其中实现 onCreateDialog() 方法。 3. 在 onCreateDialog() 方法中,使用 RenderScript 创建一个高斯模糊的 Bitmap,并将其设置为对话框背景。 4. 在对话框显示时,使用 DialogFragment.show() 方法显示对话框。 以下是示例代码: ```java public class BlurDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // 创建一个对话框 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.dialog_layout, null); builder.setView(view); // 创建一个 RenderScript 对象 RenderScript rs = RenderScript.create(getActivity()); // 加载图片资源 Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.background); // 创建一个高斯模糊的 Bitmap Bitmap blurredBitmap = image.copy(Bitmap.Config.ARGB_8888, true); Allocation input = Allocation.createFromBitmap(rs, image); Allocation output = Allocation.createFromBitmap(rs, blurredBitmap); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setInput(input); script.setRadius(25.f); script.forEach(output); output.copyTo(blurredBitmap); // 将高斯模糊的 Bitmap 设置为对话框背景 view.setBackground(new BitmapDrawable(getResources(), blurredBitmap)); return builder.create(); 要实现毛玻璃效果的对话框,可以使用 Android 的 DialogFragment 和自定义控件实现。以下是大致的步骤: 1. 在布局文件中定义对话框的 UI,包括一个用于显示毛玻璃效果的自定义控件。 2. 创建一个继承自 DialogFragment 的类,并在其中实现 onCreateDialog() 方法。 3. 在 onCreateDialog() 方法中,创建一个自定义控件的实例,并将其添加到对话框的 UI 中。 4. 在对话框显示时,使用 DialogFragment.show() 方法显示对话框。 以下是示例代码: ```java public class GlassDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // 创建一个对话框 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.dialog_layout, null); builder.setView(view); // 创建一个自定义控件 GlassView glassView = new GlassView(getActivity()); ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); glassView.setLayoutParams(layoutParams); // 将自定义控件添加到对话框的 UI 中 FrameLayout frameLayout = view.findViewById(R.id.frame_layout); frameLayout.addView(glassView); return builder.create(); 其中 GlassView 是一个自定义控件,用于显示毛玻璃效果。以下是示例代码: ```java public class GlassView extends View { private Paint paint = new Paint(); private Bitmap bitmap; public GlassView(Context context) { super(context); init(); public GlassView(Context context, AttributeSet attrs) { super(context, attrs); init(); public GlassView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); private void init() { // 加载图片资源 bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background); // 设置画笔透明度 paint.setAlpha(100); @Override protected void onDraw(Canvas canvas) { // 创建一个缩小的 Bitmap,用于实现毛玻璃效果 Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth() / 10, bitmap.getHeight() / 10, false); // 将缩小的 Bitmap 放大,实现模糊效果 Bitmap blurredBitmap = Bitmap.createScaledBitmap(scaledBitmap, getWidth(), getHeight(), false); // 绘制模糊的 Bitmap canvas.drawBitmap(blurredBitmap, 0, 0, paint); 注意:以上代码只是示例代码,实际上还需要根据具体情况进行调整和优化。