Adarsh32

Blur the linear layout

Aug 12th, 2016
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.10 KB | None | 0 0
  1. here is my complete code:
  2.  
  3. package com.example.adarsh.test;
  4.  
  5. import android.app.Service;
  6. import android.content.Intent;
  7. import android.graphics.Bitmap;
  8. import android.graphics.PixelFormat;
  9. import android.graphics.SurfaceTexture;
  10. import android.os.IBinder;
  11. import android.renderscript.Allocation;
  12. import android.renderscript.Element;
  13. import android.renderscript.RenderScript;
  14. import android.renderscript.ScriptIntrinsicBlur;
  15. import android.support.annotation.Nullable;
  16. import android.util.Log;
  17. import android.view.Surface;
  18. import android.view.TextureView;
  19. import android.view.View;
  20. import android.view.ViewGroup;
  21. import android.view.WindowManager;
  22. import android.widget.FrameLayout;
  23. import android.widget.ImageView;
  24. import android.widget.LinearLayout;
  25. import android.widget.RelativeLayout;
  26. import android.widget.Toast;
  27.  
  28. /**
  29. * Created by Adarsh on 12-08-2016.
  30. */
  31. public class Exa extends Service {
  32.  
  33.  
  34. WindowManager innerlayout;
  35. WindowManager.LayoutParams params;
  36. LinearLayout inner;
  37. RelativeLayout outer;
  38. RenderScript mRS;
  39. ScriptIntrinsicBlur script;
  40. Allocation allocOriginalScreenshot,allocBlurred;
  41. TextureView textureViewBlurred;
  42. private static final String TAG="Exa";
  43.  
  44. @Override
  45. public void onCreate(){
  46. inner=new LinearLayout(this);
  47. outer=new RelativeLayout(this);
  48.  
  49. //param
  50. Toast.makeText(Exa.this,"started",Toast.LENGTH_SHORT).show();
  51. LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
  52. inner.setLayoutParams(layoutParams);
  53.  
  54. RelativeLayout.LayoutParams rparam=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
  55. outer.setLayoutParams(rparam);
  56. outer.addView(inner);
  57.  
  58.  
  59. innerlayout=(WindowManager)getSystemService(WINDOW_SERVICE);
  60. params=new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSPARENT);
  61.  
  62.  
  63. //Ima
  64.  
  65.  
  66. innerlayout.addView(outer,params);
  67. example();
  68.  
  69. blurView(inner);
  70.  
  71. }
  72.  
  73.  
  74. private void example() {
  75. mRS= RenderScript.create(this);
  76. script= ScriptIntrinsicBlur.create(mRS, Element.RGBA_8888(mRS));
  77. script.setRadius(5);
  78.  
  79. }
  80.  
  81. Bitmap getViewScreenshot(View v){
  82. v.setDrawingCacheEnabled(true);
  83. Bitmap b=Bitmap.createBitmap(v.getDrawingCache());
  84. v.setDrawingCacheEnabled(false);
  85.  
  86. return b;
  87. }
  88.  
  89. void replaceView(View originalView,View newView){
  90. originalView.setTag(newView);
  91. newView.setLayoutParams(new FrameLayout.LayoutParams(originalView.getLayoutParams()));
  92. ViewGroup parent=(ViewGroup)originalView.getParent();
  93. int index=parent.indexOfChild(originalView);
  94. parent.removeView(originalView);
  95. parent.addView(newView,index);
  96. }
  97.  
  98. void restoreView(View v){
  99. View otherView=(View)v.getTag();
  100.  
  101. if (otherView!=null&&otherView.getParent()!=null){
  102. replaceView(otherView,v);
  103. }
  104. else if (v!=null&&v.getParent()!=null){
  105. replaceView(v,otherView);
  106. }
  107. }
  108.  
  109. void blurView(View v){
  110. Bitmap viewScreenshot=getViewScreenshot(v);
  111.  
  112. if(allocOriginalScreenshot!=null&&(allocOriginalScreenshot.getType().getX()!=viewScreenshot.getWidth()||allocOriginalScreenshot.getType().getY()!=viewScreenshot.getHeight())){
  113. allocOriginalScreenshot.destroy();
  114. allocBlurred.destroy();
  115.  
  116. textureViewBlurred=null;
  117. allocOriginalScreenshot=null;
  118. allocBlurred=null;
  119. }
  120. if(allocOriginalScreenshot==null){
  121. allocOriginalScreenshot=Allocation.createFromBitmap(mRS,viewScreenshot);
  122. allocBlurred=Allocation.createTyped(mRS,allocOriginalScreenshot.getType(),Allocation.USAGE_SCRIPT|Allocation.USAGE_IO_OUTPUT);
  123. textureViewBlurred=new TextureView(this);
  124. textureViewBlurred.setOpaque(false);
  125. textureViewBlurred.setSurfaceTextureListener(surfaceTextureListener);
  126. }
  127. else {
  128. allocOriginalScreenshot.copyFrom(viewScreenshot);
  129. }
  130. replaceView(v,textureViewBlurred);
  131. }
  132.  
  133. void unblurView(View v){
  134. restoreView(v);
  135. }
  136.  
  137. void executeBlur(){
  138. Log.d(TAG,"Executing blur");
  139. script.setInput(allocOriginalScreenshot);
  140. script.forEach(allocBlurred);
  141.  
  142. allocBlurred.ioSend();
  143. }
  144.  
  145.  
  146. TextureView.SurfaceTextureListener surfaceTextureListener=new TextureView.SurfaceTextureListener() {
  147. @Override
  148. public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
  149. allocBlurred.setSurface(new Surface(surface));
  150. executeBlur();
  151. }
  152.  
  153. @Override
  154. public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
  155.  
  156. }
  157.  
  158. @Override
  159. public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
  160. return false;
  161. }
  162.  
  163. @Override
  164. public void onSurfaceTextureUpdated(SurfaceTexture surface) {
  165.  
  166. }
  167. };
  168.  
  169.  
  170.  
  171.  
  172. @Nullable
  173. @Override
  174. public IBinder onBind(Intent intent) {
  175. return null;
  176. }
  177. }
  178.  
  179.  
  180.  
  181.  
  182.  
  183. Output error:
  184. FATAL EXCEPTION: main
  185. Process: com.example.adarsh.test, PID: 9490
  186. java.lang.RuntimeException: Unable to create service com.example.adarsh.test.Exa: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
  187. at android.app.ActivityThread.handleCreateService(ActivityThread.java:2801)
  188. at android.app.ActivityThread.access$1800(ActivityThread.java:155)
  189. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1400)
  190. at android.os.Handler.dispatchMessage(Handler.java:102)
  191. at android.os.Looper.loop(Looper.java:135)
  192. at android.app.ActivityThread.main(ActivityThread.java:5343)
  193. at java.lang.reflect.Method.invoke(Native Method)
  194. at java.lang.reflect.Method.invoke(Method.java:372)
  195. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
  196. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
  197. Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
  198. at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
  199. at com.example.adarsh.test.Exa.getViewScreenshot(Exa.java:77)
  200. at com.example.adarsh.test.Exa.blurView(Exa.java:104)
  201. at com.example.adarsh.test.Exa.onCreate(Exa.java:63)
  202. at android.app.ActivityThread.handleCreateService(ActivityThread.java:2791)
  203. at android.app.ActivityThread.access$1800(ActivityThread.java:155) 
  204. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1400) 
  205. at android.os.Handler.dispatchMessage(Handler.java:102) 
  206. at android.os.Looper.loop(Looper.java:135) 
  207.  
  208.  
  209. and when i add the ImageView in innerlayout
  210.  
  211. it gives me error like this:
  212.  
  213.  
  214.  
  215. FATAL EXCEPTION: main
  216. E/AndroidRuntime: FATAL EXCEPTION: main
  217. Process: com.example.adarsh.test, PID: 7418
  218. java.lang.ClassCastException: android.view.ViewRootImpl cannot be cast to android.view.ViewGroup
  219. at com.example.adarsh.test.Exa.replaceView(Exa.java:95)
  220. at com.example.adarsh.test.Exa.blurView(Exa.java:133)
  221. at com.example.adarsh.test.Exa$1.onClick(Exa.java:70)
  222. at android.view.View.performClick(View.java:4785)
  223. at android.view.View$PerformClick.run(View.java:19884)
  224. at android.os.Handler.handleCallback(Handler.java:739)
  225. at android.os.Handler.dispatchMessage(Handler.java:95)
  226. at android.os.Looper.loop(Looper.java:135)
  227. at android.app.ActivityThread.main(ActivityThread.java:5343)
  228. at java.lang.reflect.Method.invoke(Native Method)
  229. at java.lang.reflect.Method.invoke(Method.java:372)
  230. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
  231. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Add Comment
Please, Sign In to add comment