高斯模糊Android

来源:互联网 发布:ubuntu 关闭swap 编辑:程序博客网 时间:2024/05/17 06:29

高斯模糊Android

前提:当前的需求是要求用户每次下滑状态栏的时候看到的都是目前打开应用的模糊化的背景.下面的介绍主要是围绕实现这个需求来的.

下面记录几使用和见过的方法,以便后续直接使用.

方法1:

获取当前屏幕截图(使用SurfaceControl.screenshot),进行高速模式处理,如果获取结果为null 就使用壁纸来代替(为考虑是宽屏壁纸或者不归宿壁纸会出现压缩的情况),获取之后使用LayerDrawable来将得到的Drawable 作为当前应用程序的背景,这样不仅到达的模糊的效果,有保存了不会看不见当前程序的内容.(防止壁纸和当前程序内容颜色相近的时候容易出现看不清的问题)

方法2:

获取系统壁纸,裁剪,将裁剪之后的壁纸和一张半透明的图片(尺寸要注意)进行合成,这里不是用的LayerDrawable来直接使用的,是使用的Canvas来实现的.再将合成之后的壁纸来进行高斯模糊.

备注:方法2实际在使用的过程中需要裁剪壁纸,这个是不能置裁剪的,需要先将copy 一份Bitmap 对象,才进行操作.而且后续合成和模糊化处理的时候容易出现内存溢出现象.这里可以使用下面的方法来解决,当然及时调用Bitmap的receyle()方法来回收也是必要.
newBitmap = bmp.copy(Bitmap.Config.ARGB_4444, true);//采用ARGB_4444 减少内存使用

由于这2中方法是在不同项目中分别使用到的,各种各的利弊,可以结合实际情况来整合2个方法来实现相关需求.

方法3:

上面的方法主要是使用的java 来实现的,也就是获取bitmap 的方法不一样,实际模糊处理的方法是完全一样的.也就是都使用的RenderScript和ScriptIntrinsicBlur来处理的.后面借鉴别人的成果发现还有使用C来实现模糊处理的部分,这样暂时不对比性能的差异.同时本方法也是使用SurfaceControl.screenshot 来获取bitmap资源.

备注:需要添加 <uses-permission android:name="android.permission.READ_FRAME_BUFFER" /> 同时需要系统签名


方法1具体源码如下:

  1. private static final int BLUR_RADIUS = 3;private static final int SCALE_RATIO = 8;
  2. public void blur(){
  3. Bitmap mBackBitmap = getScreenshot(context, BLUR_RADIUS);
  4. Drawable backgroundDrawable = null;
  5. if (mBackBitmap != null) {
  6. backgroundDrawable = new BitmapDrawable(mBackBitmap);
  7. } else {
  8. Log.i(TAG, "mBackBitmap is null.");
  9. WallpaperManager wallpaperManager = WallpaperManager.getInstance(mDialogContext);
  10. backgroundDrawable = wallpaperManager.getDrawable();
  11. }
  12. if (backgroundDrawable != null) {
  13. getWindow().getDecorView().setBackground(new LayerDrawable(new Drawable[]{backgroundDrawable,
  14. context.getResources().getDrawable(R.drawable.ali_poweroff_background)}));
  15. }
  16. }
  17. private Bitmap getScreenshot(Context context, int radius) {
  18. long start = System.currentTimeMillis();
  19. Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
  20. DisplayMetrics displayMetrics = new DisplayMetrics();
  21. Matrix displayMatrix = new Matrix();
  22. display.getRealMetrics(displayMetrics);
  23. final int w = displayMetrics.widthPixels;
  24. final int h = displayMetrics.heightPixels;
  25. float[] dims = {w, h};
  26. float degrees = getDegreesForRotation(display.getRotation());
  27. boolean requiresRotation = (degrees > 0);
  28. if (requiresRotation) {
  29. // Get the dimensions of the device in its native orientation
  30. displayMatrix.reset();
  31. displayMatrix.preRotate(-degrees);
  32. displayMatrix.mapPoints(dims);
  33. dims[0] = Math.abs(dims[0]);
  34. dims[1] = Math.abs(dims[1]);
  35. }
  36. Bitmap bp = SurfaceControl.screenshot((int) dims[0], (int) dims[1]);
  37. if (bp == null) return null;
  38. //clip bitmap
  39. Bitmap ss = Bitmap.createBitmap((int) dims[0]/SCALE_RATIO, (int) dims[1]/SCALE_RATIO, Bitmap.Config.ARGB_8888);
  40. if (ss == null) return null;
  41. Canvas c = new Canvas(ss);
  42. c.clipRect(0, 0, dims[0] / SCALE_RATIO, dims[1] / SCALE_RATIO);
  43. Rect srcRect = new Rect(0, 0, bp.getWidth(), bp.getHeight());
  44. Rect dstRect = new Rect(0, 0, ss.getWidth(), ss.getHeight());
  45. c.drawBitmap(bp, srcRect, dstRect, null);
  46. c.setBitmap(null);
  47. bp.recycle();
  48. bp = ss;
  49. //rotate bitmap
  50. if (requiresRotation) {
  51. ss = Bitmap.createBitmap(w / SCALE_RATIO, h / SCALE_RATIO, Bitmap.Config.ARGB_8888);
  52. if (ss == null) return null;
  53. c = new Canvas(ss);
  54. c.translate(ss.getWidth() / 2, ss.getHeight() / 2);
  55. c.rotate(degrees);
  56. c.translate(-dims[0] / (2 * SCALE_RATIO), -dims[1] / (2 * SCALE_RATIO));
  57. c.drawBitmap(bp, 0, 0, null);
  58. c.setBitmap(null);
  59. bp.recycle();
  60. bp = ss;
  61. }
  62. //blur bitmap
  63. Bitmap bitmap = bp.copy(bp.getConfig(), true);
  64. final RenderScript rs = RenderScript.create(context);
  65. final Allocation input = Allocation.createFromBitmap(rs, bp, Allocation.MipmapControl.MIPMAP_NONE,
  66. Allocation.USAGE_SCRIPT);
  67. final Allocation output = Allocation.createTyped(rs, input.getType());
  68. final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
  69. script.setRadius(radius);
  70. script.setInput(input);
  71. script.forEach(output);
  72. output.copyTo(bitmap);
  73. long end = System.currentTimeMillis();
  74. Log.d(TAG, "Take time " + (end - start));
  75. return bitmap;
  76. }
  77. private float getDegreesForRotation(int value) {
  78. switch (value) {
  79. case Surface.ROTATION_90:
  80. return 360f - 90f;
  81. case Surface.ROTATION_180:
  82. return 360f - 180f;
  83. case Surface.ROTATION_270:
  84. return 360f - 270f;
  85. }
  86. return 0f;
  87. }
drawable:ali_poweroff_background

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">
  3. <gradient android:name="poweroff_background"
  4. android:startColor="#e5000000"
  5. android:endColor="#7f000000"
  6. android:angle="90"
  7. />
  8. </shape>

方法2源码如下:

  1. private Bitmap blur_wallpaper = null;
  2. public void applyBlur() {
  3. Log.d(TAG,"applyBlur ------- ");
  4. if(null != blur_wallpaper && !blur_wallpaper.isRecycled()){//先回收上一次保存壁纸的变量
  5. Log.d(TAG,"applyBlur blur_wallpaper isRecycled");
  6. setBackgroundResource(R.drawable.backgoud_bulr);
  7. blur_wallpaper.recycle();
  8. blur_wallpaper = null;
  9. }
  10. if(wallpaperManager == null){
  11. wallpaperManager = WallpaperManager.getInstance(this.getContext());
  12. }
  13. Drawable wallpaperDrawable = wallpaperManager.getDrawable();
  14. Bitmap bmp = ((BitmapDrawable) wallpaperDrawable).getBitmap();
  15. Bitmap newBitmap = bmp;
  16. if(null == newBitmap){
  17. Log.d(TAG,"applyBlur newBitmap is null" );
  18. if(blur_wallpaper == null){
  19. setBackgroundResource(R.drawable.backgoud_bulr_default);
  20. blur_wallpaper = ((BitmapDrawable)getResources().getDrawable(R.drawable.backgoud_bulr_default)).getBitmap();
  21. }
  22. return;
  23. }else if(!newBitmap.isMutable()){
  24. int w = bmp.getWidth();
  25. int h = bmp.getHeight();
  26. Log.d(TAG,"applyBlur w=" + w + " h=" + h);
  27. Log.d(TAG,"applyBlur not isMutable");
  28. try {
  29. newBitmap = bmp.copy(Bitmap.Config.ARGB_4444, true);//采用ARGB_4444 减少内存使用
  30. } catch (Exception e) {
  31. Log.d(TAG,"bitmap copy error,out of memory");
  32. if(null != bmp && !bmp.isRecycled()){
  33. //bmp.recycle();
  34. bmp = null;
  35. }
  36. if(null != newBitmap && !newBitmap.isRecycled()){
  37. newBitmap.recycle();
  38. newBitmap = null;
  39. }
  40. setBackgroundResource(R.drawable.backgoud_bulr_default);
  41. blur_wallpaper = ((BitmapDrawable)getResources().getDrawable(R.drawable.backgoud_bulr_default)).getBitmap();
  42. Log.d(TAG,"use default background");
  43. return ;
  44. }
  45. }
  46. newBitmap = mPictureUtil.setAndcropWallpaper(mContext, newBitmap);
  47. if(null == newBitmap){
  48. Toast.makeText(mContext, R.string.update_status_bar_ground_error, Toast.LENGTH_SHORT).show();
  49. setBackgroundResource(R.drawable.backgoud_bulr_default);
  50. blur_wallpaper = ((BitmapDrawable)getResources().getDrawable(R.drawable.backgoud_bulr_default)).getBitmap();
  51. Log.d(TAG,"use default background----");
  52. return;
  53. }
  54. Canvas canvas = new Canvas(newBitmap);
  55. Drawable backgoud_bulr = this.getResources().getDrawable(R.drawable.backgoud_bulr);//半透明的黑色背景
  56. Bitmap bitmap_backgoud_bulr = ((BitmapDrawable)backgoud_bulr).getBitmap();
  57. canvas.drawBitmap(bitmap_backgoud_bulr, 0, 0, null );
  58. canvas.save(Canvas.ALL_SAVE_FLAG);
  59. //存储新合成的图片
  60. canvas.restore();
  61. if(bmp != null && !bmp.isRecycled()){
  62. Log.d(TAG,"applyBlur bmp.isRecycled");
  63. // bmp.recycle();
  64. bmp = null;
  65. }
  66. if(bitmap_backgoud_bulr != null && !bitmap_backgoud_bulr.isRecycled()){
  67. Log.d(TAG,"applyBlur bitmap_backgoud_bulr.isRecycled");
  68. bitmap_backgoud_bulr.recycle();
  69. bitmap_backgoud_bulr = null;
  70. backgoud_bulr = null;
  71. }
  72. blur_wallpaper = mPictureUtil.blur(mContext, newBitmap);
  73. Drawable background = new BitmapDrawable(getResources(), blur_wallpaper);
  74. setBackground(background);
  75. if(null != newBitmap && !newBitmap.isRecycled()){
  76. Log.d(TAG,"blur bkg.isRecycled");
  77. newBitmap.recycle();
  78. newBitmap = null;
  79. }
  80. }

  1. /**
  2. * @Title: PictureUtil.java
  3. * @Package com.doov.systemui.utils
  4. * @Description: TODO(用一句话描述该文件做什么)
  5. * @author
  6. * @date 2015年8月7日 下午3:54:46
  7. * @version V1.0
  8. */
  9. package com.doov.systemui.utils;
  10. import android.annotation.TargetApi;
  11. import android.content.Context;
  12. import android.graphics.Bitmap;
  13. import android.graphics.Matrix;
  14. import android.graphics.drawable.BitmapDrawable;
  15. import android.graphics.drawable.Drawable;
  16. import android.os.Build;
  17. import android.renderscript.Allocation;
  18. import android.renderscript.Element;
  19. import android.renderscript.RenderScript;
  20. import android.renderscript.ScriptIntrinsicBlur;
  21. import android.util.DisplayMetrics;
  22. import android.util.Log;
  23. import android.view.WindowManager;
  24. /**
  25. * @ClassName: PictureUtil
  26. * @Description:图片处理
  27. * @author zhang.le
  28. * @date 2015年8月7日 下午3:54:46
  29. *
  30. */
  31. public class PictureUtil {
  32. private static final String TAG = "PictureUtil";
  33. public final static int mScal = 2;
  34. private final boolean DEBUG = true;
  35. /**
  36. *
  37. *高斯模式处理
  38. * @param context
  39. * @param bkg
  40. * @return Bitmap 返回类型
  41. */
  42. @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
  43. public Bitmap blur(Context context,Bitmap bkg) {
  44. long startMs = System.currentTimeMillis();
  45. float radius = 25;
  46. bkg = small(bkg);
  47. bkg = bkg.copy(bkg.getConfig(), true);
  48. final RenderScript rs = RenderScript.create(context);
  49. final Allocation input = Allocation.createFromBitmap(rs, bkg, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
  50. final Allocation output = Allocation.createTyped(rs, input.getType());
  51. final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs,
  52. Element.U8_4(rs));
  53. script.setRadius(radius);
  54. script.setInput(input);
  55. script.forEach(output);
  56. output.copyTo(bkg);
  57. rs.destroy();
  58. script.destroy();
  59. input.destroy();
  60. output.destroy();
  61. Log.d(TAG, "blur take away:" + (System.currentTimeMillis() - startMs) + "ms");
  62. return bkg;
  63. }
  64. private static Bitmap small(Bitmap bitmap) {
  65. Matrix matrix = new Matrix();
  66. matrix.postScale(1f/mScal, 1f/mScal); // 长和宽放大缩小的比例
  67. Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
  68. bitmap.getHeight(), matrix, true);
  69. return resizeBmp;
  70. }
  71. /**
  72. *
  73. * 裁剪图片
  74. * @param context
  75. * @param wallpaper
  76. * @return Bitmap
  77. */
  78. public Bitmap setAndcropWallpaper(Context context,Bitmap wallpaper) {
  79. Bitmap targetBitmap = null;
  80. if (wallpaper != null) {
  81. try {
  82. int wallpaperWidth = wallpaper.getWidth();
  83. int wallpaperHeight = wallpaper.getHeight();
  84. DisplayMetrics dm = new DisplayMetrics();
  85. WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
  86. wm.getDefaultDisplay().getMetrics(dm);
  87. int screenWidth = dm.widthPixels;// 1080
  88. int screenHeight = dm.heightPixels;// 1920
  89. float defaultScreen = (float)screenHeight / (float)screenWidth;
  90. Log.d(TAG, "defaultScreen=" + defaultScreen);
  91. if (defaultScreen < 1) {
  92. defaultScreen = (float)screenWidth / (float)screenHeight;
  93. }
  94. float bitmapSize = (float)wallpaperHeight /(float)wallpaperWidth;
  95. int disWidth = 0;
  96. int disHeight = 0;
  97. int captureBitmapWidth = wallpaperWidth;
  98. int captureBitmapHeight = wallpaperHeight;
  99. Log.d(TAG, "defaultScreen=" + defaultScreen + " bitmapSize="
  100. + bitmapSize);
  101. if (defaultScreen > bitmapSize) { // bitmapHeight is min , capture bitmapWidth
  102. captureBitmapWidth = (int) ((screenWidth * wallpaperHeight) / screenHeight);
  103. disWidth = wallpaperWidth - captureBitmapWidth;
  104. } else if (defaultScreen < bitmapSize) {
  105. captureBitmapHeight = (int) ((wallpaperWidth * screenHeight) / screenWidth);
  106. disHeight = wallpaperHeight - captureBitmapHeight;
  107. }
  108. if (DEBUG) {
  109. Log.d(TAG, "setAndcropWallpaper ----- disWidth="
  110. + disWidth + ",disHeight=" + disHeight
  111. + ",captureBitmapWidth=" + captureBitmapWidth
  112. + ",captureBitmapHeight=" + captureBitmapHeight
  113. + " screenWidth=" + screenWidth + " screenHeight="
  114. + screenHeight);
  115. }
  116. if (disWidth < 0 || disHeight < 0) {
  117. return null;
  118. }
  119. targetBitmap = Bitmap.createBitmap(wallpaper, disWidth / 2,
  120. disHeight / 2, captureBitmapWidth, captureBitmapHeight);
  121. float scaleWidth = (float) ((float) captureBitmapWidth / screenWidth);
  122. float scaleHeight = (float) ((float) captureBitmapHeight / screenHeight);
  123. Log.d(TAG, "scaleWidth=" + scaleWidth + " scaleHeight="
  124. + scaleHeight);
  125. if ((targetBitmap != null)
  126. && (scaleWidth != 1 || scaleHeight != 1)) {
  127. Matrix matrix = new Matrix();
  128. /**
  129. * 处理之后的图片小于屏幕尺寸 处理原始的图片比例和屏幕比例是一致,但尺寸比屏幕大
  130. */
  131. if (scaleWidth != 0.0f && scaleHeight != 0.0f
  132. && (scaleWidth != 1.0f || scaleHeight != 1.0f)) {//
  133. scaleWidth = 1.0f / scaleWidth;
  134. scaleHeight = 1.0f / scaleHeight;
  135. Log.d(TAG, "---scaleWidth=" + scaleWidth + " scaleHeight=" + scaleHeight);
  136. }
  137. matrix.postScale(scaleWidth, scaleHeight);
  138. int width = targetBitmap.getWidth();
  139. int height = targetBitmap.getHeight();
  140. Log.d(TAG, "---width=" + width + " height=" + height);
  141. targetBitmap = Bitmap.createBitmap(targetBitmap, 0, 0,
  142. screenWidth > width ? width : screenWidth,
  143. screenHeight > height ? height : screenHeight,
  144. matrix, true);
  145. }
  146. } catch (OutOfMemoryError e) {
  147. e.printStackTrace();
  148. }
  149. }
  150. return targetBitmap;
  151. }
  152. }

方法3源码

PhoneStatusBar.java
  1. protected static final int MSG_SCREEN_SHOT = 2031;
  2. public boolean interceptTouchEvent(MotionEvent event) {
  3. //......
  4. //zhangle add start
  5. int action = event.getAction();
  6. if (MotionEvent.ACTION_DOWN == action) {
  7. if (!mExpandedVisible) {
  8. boolean hasScreenShotMsg = myHandler.hasMessages(MSG_SCREEN_SHOT);
  9. LogD.d(TAG, "interceptTouchEvent hasScreenShotMsg = " + hasScreenShotMsg);
  10. if (!hasScreenShotMsg) {
  11. Log.d(TAG, " sendEmptyMessage MSG_SCREEN_SHOT currentTimeMillis = " + System.currentTimeMillis());
  12. myHandler.sendEmptyMessage(MSG_SCREEN_SHOT);
  13. }
  14. }
  15. }
  16. //zhangel add end
  17. return false;
  18. }
以上修改是保证每次点击状态栏的时候可以获取最新的截图.具体原因参考SystemUI 流程相关的笔记.
NotificationPanelView.java
添加以下代码
  1. private Bitmap mScreenShotBg;
  2. protected void setScreenShotBitmap(Bitmap bitmap) {
  3. mScreenShotBg = bitmap;
  4. }
  5. public void recyleScreenShotBg(){
  6. if (mScreenShotBg != null) {
  7. mScreenShotBg.recycle();
  8. mScreenShotBg = null;
  9. }
  10. }
  11. @Override
  12. public void draw(Canvas canvas) {
  13. if (canvas == null) {
  14. return;
  15. }
  16. if (mScreenShotBg != null) {
  17. Bitmap bp = mScreenShotBg;
  18. Rect dstRect = new Rect(0, 0, getWidth(), (int) getFullHeight());
  19. Paint paint = new Paint();
  20. paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
  21. canvas.drawBitmap(bp, new Rect(0, 0, bp.getWidth(), bp.getHeight()), dstRect, paint);
  22. }
  23. super.draw(canvas);
  24. }
同时在 onDetachedFromWindow 里面末尾添加recyleScreenShotBg(),来回收内存.

下面主要是使用Handler 发送消息来使用SurfaceControl.screenshot 获取bitmap 资源来供JNI处理.
PhoneStatusBar.java
  1. //zhangle add start
  2. private Handler myHandler = new Handler(){
  3. @Override
  4. public void dispatchMessage(Message msg) {
  5. switch (msg.what) {
  6. case MSG_SCREEN_SHOT:
  7. Xlog.d(TAG, "MSG_SCREEN_SHOT.");
  8. Bitmap screenShotBitmap = screenshotFocusedWindow(mDisplayMetrics.widthPixels,
  9. mDisplayMetrics.heightPixels);
  10. if (screenShotBitmap != null) {
  11. mNotificationPanel.setScreenShotBitmap(screenShotBitmap);
  12. }
  13. break;
  14. default:
  15. break;
  16. }
  17. }
  18. };
  19. private Bitmap screenshotFocusedWindow(int maxWidth, int maxHeight) {
  20. long beginScreenShot = System.currentTimeMillis();
  21. int rot = mDisplay.getRotation();
  22. Rect frame = new Rect();
  23. float degrees = 0f;
  24. switch (rot) {
  25. case Surface.ROTATION_90:
  26. degrees = 360f - 90f;
  27. break;
  28. case Surface.ROTATION_180:
  29. degrees = 360f - 180f;
  30. break;
  31. case Surface.ROTATION_270:
  32. degrees = 360f - 270f;
  33. break;
  34. default:
  35. degrees = 0f;
  36. break;
  37. }
  38. int mNavSize = 0;
  39. try {
  40. boolean showNav = mWindowManagerService.hasNavigationBar();//IWindowManager
  41. if (showNav) {
  42. mNavSize = mContext.getResources().getDimensionPixelSize(
  43. R.dimen.navigation_bar_size);
  44. }
  45. } catch (RemoteException ex) {
  46. // no window manager? good luck with that
  47. Slog.e(TAG, "resolve navigationbar layout fail");
  48. }
  49. if (mNavSize > 0) {
  50. if (maxHeight > maxWidth) {
  51. maxHeight += mNavSize;
  52. } else {
  53. maxWidth += mNavSize;
  54. }
  55. }
  56. Matrix matrix = new Matrix();
  57. int dw = maxWidth;
  58. int dh = maxHeight;
  59. if (DEBUG) Log.d("screenshot", "bitmap dw =" + maxWidth + " dh = " + maxHeight);
  60. frame.set(0, 0/* getStatusBarHeight() */, dw, dh);
  61. float dims[] = {
  62. dw, dh
  63. };
  64. boolean requiresRotation = (degrees > 0);
  65. if (requiresRotation) {
  66. // Get the dimensions of the device in its native orientation
  67. matrix.reset();
  68. matrix.preRotate(-degrees);
  69. matrix.mapPoints(dims);
  70. dims[0] = Math.abs(dims[0]);
  71. dims[1] = Math.abs(dims[1]);
  72. }
  73. if (frame.isEmpty()) {
  74. return null;
  75. }
  76. if (DEBUG) Log.d("screenshot", "bitmap width =" + dims[0] + "height = " + dims[1]);
  77. Bitmap bp = SurfaceControl.screenshot((int) dims[0], (int) dims[1]);
  78. // The screenshot() interface may fail. If it fail, it will return null.
  79. // If the screenshot is null, the statusbar will be transparent.
  80. if (bp == null) {
  81. return null;
  82. }
  83. final int SCALE_RATIO = 8;
  84. if (requiresRotation) {
  85. // Rotate the screenshot to the current orientation
  86. Bitmap ss = Bitmap.createBitmap(dw / SCALE_RATIO, dh / SCALE_RATIO,
  87. Bitmap.Config.ARGB_8888);
  88. if (DEBUG)
  89. Log.d("screenshot", "Canvas width =" + dims[0] + " height = " + dims[1]);
  90. if (ss == null) {
  91. return null;
  92. }
  93. Canvas c = new Canvas(ss);
  94. c.clipRect(0, frame.top / SCALE_RATIO, dw / SCALE_RATIO, dh / SCALE_RATIO);
  95. if (mNavSize > 0) {
  96. c.clipRect(0, 0, (dw - mNavSize) / SCALE_RATIO, dh / SCALE_RATIO);
  97. }
  98. c.translate(ss.getWidth() / 2, ss.getHeight() / 2);
  99. c.rotate(degrees);
  100. c.translate(-dims[0] / (2 * SCALE_RATIO), -dims[1] / (2 * SCALE_RATIO));
  101. // c.drawBitmap(bp, 0, 0, null);
  102. Rect dstRect = new Rect(0, 0, ss.getHeight(), ss.getWidth());
  103. c.drawBitmap(bp, new Rect(0, 0, bp.getWidth(), bp.getHeight()), dstRect, null);
  104. Paint paint = new Paint();
  105. // Reset the screen shot color alpha for text show clearly.
  106. // 80% black
  107. paint.setColor(mContext.getResources().getColor(R.color.notification_background_color));
  108. /* YUNOS END */
  109. c.drawRect(dstRect, paint);
  110. c.setBitmap(null);
  111. bp.recycle();
  112. bp = ss;
  113. } else {
  114. Bitmap ss = Bitmap.createBitmap(dw / SCALE_RATIO, dh / SCALE_RATIO,
  115. Bitmap.Config.ARGB_8888);
  116. if (ss == null) {
  117. return null;
  118. }
  119. Canvas c = new Canvas(ss);
  120. c.clipRect(0, frame.top / SCALE_RATIO, dw / SCALE_RATIO, dh / SCALE_RATIO);
  121. if (mNavSize > 0) {
  122. c.clipRect(0, 0, dw / SCALE_RATIO, (dh - mNavSize) / SCALE_RATIO);
  123. }
  124. // c.drawBitmap(bp, 0, 0, null);
  125. Rect dstRect = new Rect(0, 0, ss.getWidth(), ss.getHeight());
  126. c.drawBitmap(bp, new Rect(0, 0, bp.getWidth(), bp.getHeight()), dstRect, null);
  127. Paint paint = new Paint();
  128. // Reset the screen shot color alpha for text show clearly.
  129. // 80% black
  130. paint.setColor(mContext.getResources().getColor(R.color.notification_background_color));
  131. c.drawRect(dstRect, paint);
  132. c.setBitmap(null);
  133. bp.recycle();
  134. bp = ss;
  135. }
  136. // Invoke jni with thread safe
  137. bp = FrostedGlassUtil.getInstance().convertToBlur(bp, 20);
  138. return bp;
  139. }
  140. //zhangle add end
上面代码中的FrostedGlassUtil.getInstance().convertToBlur(bp,20),就是调用JNI的地方.
FrostedGlassUtil.java:
  1. package com.android.systemui.utils;
  2. import android.graphics.Bitmap;
  3. public class FrostedGlassUtil {
  4. private volatile static FrostedGlassUtil mFrostedGlassUtil;
  5. /*YUNOS BEGIN*/
  6. //##module(SystemUI)
  7. //##date:2013/3/29 ##author:sunchen.sc@alibaba-inc.com##BugID:104943
  8. //Invoke jni with thread safe
  9. public static FrostedGlassUtil getInstance() {
  10. if (mFrostedGlassUtil == null) {
  11. synchronized (FrostedGlassUtil.class) {
  12. if (mFrostedGlassUtil == null) {
  13. mFrostedGlassUtil = new FrostedGlassUtil();
  14. }
  15. }
  16. }
  17. return mFrostedGlassUtil;
  18. }
  19. /*YUNOS END*/
  20. public native void clearColor(Bitmap image, int color);
  21. public native void imageBlur(Bitmap srcBitmap, Bitmap dstBitmap);
  22. public native void boxBlur(Bitmap srcBitmap, int radius);
  23. public native void stackBlur(Bitmap srcBitmap, int radius);
  24. public native void oilPaint(Bitmap srcBitmap, int radius);
  25. public native void colorWaterPaint(Bitmap srcBitmap, int radius);
  26. /*YUNOS BEGIN*/
  27. //##module(SystemUI)
  28. //##date:2013/3/29 ##author:sunchen.sc@alibaba-inc.com##BugID:104943
  29. //Invoke jni with thread safe
  30. public synchronized Bitmap convertToBlur(Bitmap bmp, int radius) {
  31. long beginBlur = System.currentTimeMillis();
  32. stackBlur(bmp, radius);
  33. return bmp;
  34. }
  35. static {
  36. // load frosted glass lib
  37. System.loadLibrary("frostedGlass");
  38. }
  39. /*YUNOS END*/
  40. }
这里将JNI 下面用到的类的目录列出.

为了在编译的时候能够将生成的so库和SystemUI 关联起来,需要在SystemUI的Android.mk 中增加以下2行,其中libfrostedGlass就是上面jni 目录中的Android.mk 里面配置的so库的名称,编译jni 目前之后会生成一个libfrostedGlass.so 库.
LOCAL_JNI_SHARED_LIBRARIES := libfrostedGlass
LOCAL_REQUIRED_MODULES := libfrostedGlass
上面这2行的作用是让编译系统可以将生成的libfrostedGlass.so 拷贝到System/SystemUI/lib/arm64/下面去.
下面继续列出上面jni 部分文件的源码.
Android.mk
  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3. LOCAL_MODULE := libfrostedGlass
  4. LOCAL_SRC_FILES := frostedGlass.c bitmapPort.c imageProcess.c
  5. #LOCAL_LDLIBS := -llog -ljnigraphics
  6. LOCAL_SHARED_LIBRARIES += \
  7. liblog libjnigraphics
  8. include $(BUILD_SHARED_LIBRARY)
其他jni 实现部分的源码看附件.点击打开链接




















0 0
原创粉丝点击