Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- here is my complete code:
- package com.example.adarsh.test;
- import android.app.Service;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.PixelFormat;
- import android.graphics.SurfaceTexture;
- import android.os.IBinder;
- import android.renderscript.Allocation;
- import android.renderscript.Element;
- import android.renderscript.RenderScript;
- import android.renderscript.ScriptIntrinsicBlur;
- import android.support.annotation.Nullable;
- import android.util.Log;
- import android.view.Surface;
- import android.view.TextureView;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.WindowManager;
- import android.widget.FrameLayout;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.RelativeLayout;
- import android.widget.Toast;
- /**
- * Created by Adarsh on 12-08-2016.
- */
- public class Exa extends Service {
- WindowManager innerlayout;
- WindowManager.LayoutParams params;
- LinearLayout inner;
- RelativeLayout outer;
- RenderScript mRS;
- ScriptIntrinsicBlur script;
- Allocation allocOriginalScreenshot,allocBlurred;
- TextureView textureViewBlurred;
- private static final String TAG="Exa";
- @Override
- public void onCreate(){
- inner=new LinearLayout(this);
- outer=new RelativeLayout(this);
- //param
- Toast.makeText(Exa.this,"started",Toast.LENGTH_SHORT).show();
- LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
- inner.setLayoutParams(layoutParams);
- RelativeLayout.LayoutParams rparam=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
- outer.setLayoutParams(rparam);
- outer.addView(inner);
- innerlayout=(WindowManager)getSystemService(WINDOW_SERVICE);
- params=new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSPARENT);
- //Ima
- innerlayout.addView(outer,params);
- example();
- blurView(inner);
- }
- private void example() {
- mRS= RenderScript.create(this);
- script= ScriptIntrinsicBlur.create(mRS, Element.RGBA_8888(mRS));
- script.setRadius(5);
- }
- Bitmap getViewScreenshot(View v){
- v.setDrawingCacheEnabled(true);
- Bitmap b=Bitmap.createBitmap(v.getDrawingCache());
- v.setDrawingCacheEnabled(false);
- return b;
- }
- void replaceView(View originalView,View newView){
- originalView.setTag(newView);
- newView.setLayoutParams(new FrameLayout.LayoutParams(originalView.getLayoutParams()));
- ViewGroup parent=(ViewGroup)originalView.getParent();
- int index=parent.indexOfChild(originalView);
- parent.removeView(originalView);
- parent.addView(newView,index);
- }
- void restoreView(View v){
- View otherView=(View)v.getTag();
- if (otherView!=null&&otherView.getParent()!=null){
- replaceView(otherView,v);
- }
- else if (v!=null&&v.getParent()!=null){
- replaceView(v,otherView);
- }
- }
- void blurView(View v){
- Bitmap viewScreenshot=getViewScreenshot(v);
- if(allocOriginalScreenshot!=null&&(allocOriginalScreenshot.getType().getX()!=viewScreenshot.getWidth()||allocOriginalScreenshot.getType().getY()!=viewScreenshot.getHeight())){
- allocOriginalScreenshot.destroy();
- allocBlurred.destroy();
- textureViewBlurred=null;
- allocOriginalScreenshot=null;
- allocBlurred=null;
- }
- if(allocOriginalScreenshot==null){
- allocOriginalScreenshot=Allocation.createFromBitmap(mRS,viewScreenshot);
- allocBlurred=Allocation.createTyped(mRS,allocOriginalScreenshot.getType(),Allocation.USAGE_SCRIPT|Allocation.USAGE_IO_OUTPUT);
- textureViewBlurred=new TextureView(this);
- textureViewBlurred.setOpaque(false);
- textureViewBlurred.setSurfaceTextureListener(surfaceTextureListener);
- }
- else {
- allocOriginalScreenshot.copyFrom(viewScreenshot);
- }
- replaceView(v,textureViewBlurred);
- }
- void unblurView(View v){
- restoreView(v);
- }
- void executeBlur(){
- Log.d(TAG,"Executing blur");
- script.setInput(allocOriginalScreenshot);
- script.forEach(allocBlurred);
- allocBlurred.ioSend();
- }
- TextureView.SurfaceTextureListener surfaceTextureListener=new TextureView.SurfaceTextureListener() {
- @Override
- public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
- allocBlurred.setSurface(new Surface(surface));
- executeBlur();
- }
- @Override
- public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
- }
- @Override
- public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
- return false;
- }
- @Override
- public void onSurfaceTextureUpdated(SurfaceTexture surface) {
- }
- };
- @Nullable
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- }
- Output error:
- FATAL EXCEPTION: main
- Process: com.example.adarsh.test, PID: 9490
- 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
- at android.app.ActivityThread.handleCreateService(ActivityThread.java:2801)
- at android.app.ActivityThread.access$1800(ActivityThread.java:155)
- at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1400)
- at android.os.Handler.dispatchMessage(Handler.java:102)
- at android.os.Looper.loop(Looper.java:135)
- at android.app.ActivityThread.main(ActivityThread.java:5343)
- at java.lang.reflect.Method.invoke(Native Method)
- at java.lang.reflect.Method.invoke(Method.java:372)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
- Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
- at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
- at com.example.adarsh.test.Exa.getViewScreenshot(Exa.java:77)
- at com.example.adarsh.test.Exa.blurView(Exa.java:104)
- at com.example.adarsh.test.Exa.onCreate(Exa.java:63)
- at android.app.ActivityThread.handleCreateService(ActivityThread.java:2791)
- at android.app.ActivityThread.access$1800(ActivityThread.java:155)
- at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1400)
- at android.os.Handler.dispatchMessage(Handler.java:102)
- at android.os.Looper.loop(Looper.java:135)
- and when i add the ImageView in innerlayout
- it gives me error like this:
- FATAL EXCEPTION: main
- E/AndroidRuntime: FATAL EXCEPTION: main
- Process: com.example.adarsh.test, PID: 7418
- java.lang.ClassCastException: android.view.ViewRootImpl cannot be cast to android.view.ViewGroup
- at com.example.adarsh.test.Exa.replaceView(Exa.java:95)
- at com.example.adarsh.test.Exa.blurView(Exa.java:133)
- at com.example.adarsh.test.Exa$1.onClick(Exa.java:70)
- at android.view.View.performClick(View.java:4785)
- at android.view.View$PerformClick.run(View.java:19884)
- at android.os.Handler.handleCallback(Handler.java:739)
- at android.os.Handler.dispatchMessage(Handler.java:95)
- at android.os.Looper.loop(Looper.java:135)
- at android.app.ActivityThread.main(ActivityThread.java:5343)
- at java.lang.reflect.Method.invoke(Native Method)
- at java.lang.reflect.Method.invoke(Method.java:372)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Add Comment
Please, Sign In to add comment