A requirement was encountered in the project:
Intercepts the current screen when a condition is met. and jump to another page, the same time the screenshot image as the next page background image, the same time the background image needs to be blurred
The next step is to solve this problem:
1. Intercept the current screen picture without status bar. Please refer to the Takescreenshot method
2, make the picture Gaussian blur method please refer to Blurbitmap method
Note: Renderscript is an addition to Android after API 11 for efficient image processing, including blur, blending, matrix convolution calculations, etc.
public class Screenshotutil {//Get a screenshot of the specified activity, save to PNG file String filenametemp = "/mnt/sdcard/temp"; /** * Takescreenshot: * TODO screenshot Remove title bar * @param activity */public static Bitmap Takescreenshot (Activ ity activity) {//view is your required view View view = Activity.getwindow (). Getdecorview (); View.setdrawingcacheenabled (TRUE); View.builddrawingcache (); Bitmap B1 = View.getdrawingcache (); Gets the status bar height rect frame = new rect (); Activity.getwindow (). Getdecorview (). Getwindowvisibledisplayframe (frame); int statusbarheight = Frame.top; LOGHELPER.I ("TAG", "" + statusbarheight); Gets the screen length and height int width = activity.getwindowmanager (). Getdefaultdisplay (). GetWidth (); int height = Activity.getwindowmanager (). Getdefaultdisplay (). GetHeight (); Remove title bar//Bitmap B = Bitmap.createbitmap (B1, 0, 25, 320, 455); Bitmap B = Bitmap.createbitmap (b1, 0, Statusbarheight, width, height-sTatusbarheight); View.destroydrawingcache (); return b; }/** * TODO for efficient image processing, including blur, mix, matrix convolution calculations, etc. * @param bitmap * @param context */@SuppressLint ("Newapi") public static Bitmap Blurbitmap (Bitmap Bitmap, Context context) {/-let's create an empty Bitmap with the same Size of the bitmap we want//to blur bitmap Outbitmap = Bitmap.createbitmap (Bitmap.getwidth (), bitmap.gethe Ight (), config.argb_8888); Instantiate a new renderscript renderscript rs = renderscript.create (context);//renderscript is Android added after API 11 Create an intrinsic Blur Script using the renderscript scriptintrinsicblur blurscript = SCRIPTINTRINSICB Lur.create (RS, Element.u8_4 (RS)); Create the Allocations (in/out) with the Renderscript and the in/out//bitmaps Allocation Allin = Alloca Tion.createfrombitmap (rs, bitmap); Allocation Allout = Allocation.createfrombitmap (RS, outbitmap); Set the radius of the Blur Blurscript.setradius (25.F); Perform the Renderscript blurscript.setinput (Allin); Blurscript.foreach (Allout); Copy the final bitmap created by the-Allocation to the Outbitmap Allout.copyto (OUTBITMAP); Recycle the original bitmap bitmap.recycle (); After finishing everything, we destroy the Renderscript. Rs.destroy (); return outbitmap; }}
3, Transmission bitmap
It just started.
Bundle.putparcelable ("Bitmap", Screenshotutil.takescreenshot (Thelayout.getactivity ()));
Proceed as follows: encapsulating the bitmap into the bundle and then encapsulating it into the intent to start the next activity
Activityutil.startactivity (Thelayout.getactivity (), Liveendactivity.class, bundle, false);
/** * Open another activity * * @param activity * @param CLS Additional activity class * @param bundle Objects passed by bundles * @param isfinish True indicates that you want to close activity false to not close activity * /public static void startactivity (activity activity, Class<?> CLS, bundle Bundle, Boolean isfinish) { Intent Intent = new Intent (activity, CLS); if (bundle! = null) { Intent.putextras (bundle); } Activity.startactivity (intent); if (isfinish) { activity.finish (); } Activity.overridependingtransition (r.anim.push_left_in, r.anim.push_left_out); }
And then in Liveendactivity.
Bitmap Bitmap = Intent.getextras (). Getparcelable ("Bitmap");
Result: Unable to achieve expected effect
The key is not to error. When you debug, you can see that we did intercept successfully, but the bitmap object is not passing the past, and not starting the next activity
Then go online and find ways to research.
Conclusion: Images larger than 40k cannot be transmitted directly.
WORKAROUND: Store the bitmap as a byte array, and then continue passing
Bitmap Bitmap = Screenshotutil.takescreenshot (Thelayout.getactivity ()); Bytearrayoutputstream baos=new Bytearrayoutputstream (); Bitmap.compress (Bitmap.CompressFormat.PNG, BAOs); byte [] bitmapbyte =baos.tobytearray (); Bundle.putbytearray ("Bitmap", bitmapbyte); Bundle.putparcelable ("Bitmap", Screenshotutil.takescreenshot (Thelayout.getactivity ())); Activityutil.startactivity (Thelayout.getactivity (), Liveendactivity.class, bundle, false);
And then parse it in the next activity.
byte[] bis =intent.getextras (). Getbytearray ("bitmap");
4, if we need to set this image as our current activity background image. We can do this.
if (bitmap! = null) { bitmap = Screenshotutil.blurbitmap (Bitmap,getapplicationcontext ());//Gaussian obfuscation GetWindow ( ). Getdecorview (). setbackgrounddrawable (New bitmapdrawable (bitmap)); }
The problem is basically conquered. Think the practical can take a look!
Update Supplement:
2015.08.23
* The Blurbitmap method can be easily produced with a version number of 4.2 and above. However, the lower version number will be an exception:
* Java.lang.NoClassDefFoundError:android.renderscript.ScriptIntrinsicBlur.
* Workaround: See http://blog.csdn.net/yangxin_540/article/details/47207727
* But I'm using this workaround easy to appear other so library native method exception problem
So we found the second way.
/** * Blurimageameliorate: Blur effect * http://blog.csdn.net/sjf0115/article/details/7266998 * @param bmp * @retu RN */public static Bitmap blurimageameliorate (Bitmap bmp) {Long start = System.currenttimemillis (); Gaussian matrix int[] Gauss = new int[] {1, 2, 1, 2, 4, 2, 1, 2, 1}; int width = bmp.getwidth (); int height = bmp.getheight (); Bitmap Bitmap = bitmap.createbitmap (width, height, Bitmap.Config.RGB_565); int PIXR = 0; int pixg = 0; int PIXB = 0; int pixcolor = 0; int NEWR = 0; int NEWG = 0; int newb = 0; int delta = 75; The smaller the value, the brighter the picture, and the larger it is, the darker the int idx = 0; int[] pixels = new int[width * height]; Bmp.getpixels (pixels, 0, width, 0, 0, width, height); for (int i = 1, length = height-1; i < length; i++) {for (int k = 1, Len =Width-1; K < Len; k++) {idx = 0; for (int m =-1, m <= 1; m++) {for (int n =-1; n <= 1; n++) {Pixcolor = pixels[(i + m) * width + k + n]; PIXR = Color.Red (Pixcolor); Pixg = Color.green (Pixcolor); PIXB = Color.Blue (Pixcolor); NEWR = newr + (int) (PIXR * gauss[idx]); NEWG = NEWG + (int) (PIXG * gauss[idx]); Newb = newb + (int) (PIXB * gauss[idx]); idx++; }} newr/= delta; NEWG/= Delta; Newb/= Delta; NEWR = Math.min (255, Math.max (0, NEWR)); NEWG = Math.min (255, Math.max (0, NEWG)); Newb = Math.min (255, Math.max (0, newb)); Pixels[i * width + K] = Color.argb (255, NEWR, NEWG, newb); NEWR = 0; NEWG = 0; newb = 0; }} bitmap.setpixels (pixels, 0, width, 0, 0, width, height); Long end = System.currenttimemillis (); return bitmap; }
Here are some assumptions about what's wrong. I will continue to update!
"Android Combat" bitmap image screenshots, fuzzy processing, transmission, use