尝试模糊android中的relativeLayout时出错 [英] Error while trying to blur relativeLayout in android

查看:55
本文介绍了尝试模糊android中的relativeLayout时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我正在以程序方式使用 制作的 relativeLayout ,该代码已附加到 WindowManager .我试图模糊 relativeLayout ,以便该 relativeLayout 后面的所有内容都变得模糊.我在下面的代码中尝试过此操作:

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:

/***由Adarsh在2016年12月8日创建.*/

/** * 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

当我在 MainActivity 中尝试相同的程序时,它可以完美工作并模糊 relativeLayout 中的所有内容.请帮我.

When I try the same program in my MainActivity it perfectly works and blur all the contents inside the relativeLayout. Please help me.

推荐答案

您获得的异常是要使用您的 getViewScreenshot()方法拍摄屏幕截图,您需要有一个视图.这意味着它只能从活动中使用,而不能从服务中使用.

To take a screenshot using your getViewScreenshot() method, you need to have a view. Meaning it will only work from an activity and not from a service.

由于服务无法查看,因此您无法获取屏幕截图,并且模糊不起作用.另外,将视图附加到服务也没有意义.文档

Since a service can't have a view, you can't get a screenshot and blurring doesn't work. Also there's no point in attaching a view to a service. Docs

这篇关于尝试模糊android中的relativeLayout时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆