2016-08-14 43 views
0

在我的應用我使用programmatically取得relativeLayout其連接到WindowManager。我正在試圖模糊relativeLayout使relativeLayout背後,一切都會看起來blur.I曾經嘗試這樣做下面的代碼:錯誤,而試圖以模糊的RelativeLayout在android系統

/** *由Adarsh於2016年12月12日創建。 */

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

當我嘗試運行它,它給我的錯誤是這樣

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference 

當我嘗試在我的MainActivity它完美的作品和模糊的所有內部的relativeLayout內容相同的節目。 請幫幫我。

回答

0

你唯一的例外是一個NullPointerException.

要使用您getViewScreenshot()方法採取截圖,你需要有一個觀點。這意味着它只能從一項活動而不是從一項服務中獲益。

由於服務不能有一個視圖,你不能得到一個截圖,模糊不起作用。此外,將視圖附加到服務也沒有意義。 Docs

+0

好吧那麼你有什麼想法我該如何模糊佈局 – Adarsh

+0

服務不能有佈局。如果您正在嘗試錄製外部應用程序,則應查看API 21中添加的[MediaProjection](https://developer.android.com/reference/android/media/projection/package-summary.html)。您應該如果你想擴展對早期API的支持,請檢查[this](http://stackoverflow.com/a/32623147/5980145)。 – Bhav