如何完全模糊布局,以便后面的一切看起来都模糊不清

时间:2016-08-15 06:22:01

标签: android

我正在尝试模糊Service类中的LinearLayout。但是当我尝试模糊布局时,它只会模糊布局中的内容。我希望布局完全模糊,以便后面的所有内容都会显示 blurrish < /强>

这是我的代码:

public class Exa extends Service {

LinearLayout LLayout,inner;
WindowManager windowManager;
RenderScript mRS;
Button button;
ImageView imageView;
ScriptIntrinsicBlur script;
Allocation allocOriginalScreenshot,allocBlurred;
TextureView textureViewBlurred;
private static final String TAG="MainActivity";
@Override
public void onCreate(){
   final LayoutInflater inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   //LLayout=(RelativeLayout)inflater.inflate(R.layout.activity_main,null);
    LLayout=(LinearLayout)inflater.inflate(R.layout.blur_layout,null);
    inner=(LinearLayout)LLayout.findViewById(R.id.innerL);
    button=(Button)inner.findViewById(R.id.blurButton);
    imageView=(ImageView)inner.findViewById(R.id.imageView2);
    example();
    windowManager=(WindowManager)getSystemService(WINDOW_SERVICE);
    WindowManager.LayoutParams windowp=new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,PixelFormat.TRANSLUCENT);
    windowManager.addView(LLayout,windowp);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            blurView(inner);
        }
    });
}

////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;
}
}

0 个答案:

没有答案