In my app I am using programmatically
made relativeLayout
which is attached to WindowManager
.I am trying to blur the relativeLayout
so that everything behind that relativeLayout
will look blur.I have tried this below code:
/** * Created by Adarsh on 12-08-2016. */
public class Exa extends Service {
RelativeLayout relativeLayout;
WindowManager windowManager;
RenderScript mRS;
ScriptIntrinsicBlur script;
Allocation allocOriginalScreenshot,allocBlurred;
TextureView textureViewBlurred;
private static final String TAG="MainActivity";
@Override
public void onCreate(){
example();
relativeLayout=new RelativeLayout(this);
RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
relativeLayout.setLayoutParams(layoutParams);
windowManager=(WindowManager)getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams layoutParamsw= new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,PixelFormat.TRANSPARENT);
windowManager.addView(relativeLayout,layoutParamsw);
blurView(relativeLayout); //adding layout for blurring effect
}
////all these code helps to give blurring effect
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){
Log.i("hii","unblurring view");
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 unblur(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;
}
}
When I try to run the program it gives me error like this
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
When I try the same program in my MainActivity
it perfectly works and blur all the contents inside the relativeLayout
.
Please help me.