2014-02-12 72 views
3

我想要做的是在我的應用程序頂部添加一個視圖,它將是過濾器視圖(我想操縱屏幕的顏色),我也希望能夠同時更改屏幕的亮度。這兩件事似乎都是分開的,但不是在一起。Android上的自定義視圖和窗口屬性

這裏是我的代碼:

添加視圖:

colourView = new Layer(cordova.getActivity()); 

WindowManager localWindowManager = (WindowManager) cordova.getActivity().getWindowManager(); 
LayoutParams layoutParams = cordova.getActivity().getWindow().getAttributes(); 
layoutParams.format = PixelFormat.TRANSLUCENT; 

layoutParams.type=LayoutParams.TYPE_SYSTEM_ALERT; 
layoutParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCHABLE; 
layoutParams.gravity=Gravity.LEFT|Gravity.TOP; 

localWindowManager.addView(colourView, layoutParams); 

Layer類:

class Layer extends View 
{ 
     private int a = 0; 
     private int b = 0; 
     private int g = 0; 
     private int r = 0; 

     public Layer(Context context){ 
     super(context); 
     } 

     @Override 
     protected void onDraw(Canvas canvas){ 
     super.onDraw(canvas); 
     canvas.drawARGB(this.a, this.r, this.g, this.b); 
     Log.d("display", "rendering.."); 
     } 

     @Override 
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
      int parentWidth = MeasureSpec.getSize(widthMeasureSpec); 
      int parentHeight = MeasureSpec.getSize(heightMeasureSpec); 
      this.setMeasuredDimension(parentWidth/2, parentHeight); 
      //Since you are attatching it to the window use window layout params. 
      WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(parentWidth/2, 
        parentHeight); 
      this.setLayoutParams(layoutParams); 

      super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
      Log.d("display", "filling..."); 
     } 

     public void setColor(int a, int r, int g, int b){ 
     this.a = a; 
     this.r = r; 
     this.g = g; 
     this.b = b; 
     invalidate(); 
     } 
} 

改變亮度:

WindowManager.LayoutParams layout = cordova.getActivity().getWindow().getAttributes(); 
try { 
    layout.screenBrightness = (float) arg_object.getDouble("brightness"); 
    //^When I comment this line, it doesn't work either. 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 
cordova.getActivity().getWindow().setAttributes(layout); 

當我將視圖添加到應用程序中,然後,我想要更改屏幕亮度 - 亮度發生變化,但我無法點擊屏幕上的任何內容。幾秒鐘後,我得到一個'應用程序沒有響應的消息。

是什麼導致我的應用程序凍結?

在此先感謝。

回答

2
LayoutParams layoutParams = cordova.getActivity().getWindow().getAttributes(); 

在這一行,你得到ActivityLayoutparams對象。這只是對Activity中的LayoutParams對象的引用。

現在你正在改變這個對象:

layoutParams.type=LayoutParams.TYPE_SYSTEM_ALERT; 
layoutParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE |  LayoutParams.FLAG_NOT_TOUCHABLE; 
layoutParams.gravity=Gravity.LEFT|Gravity.TOP; 

wehereby你正在改變Activity和除其他事物的屬性,設置FLAG_NOT_FOCUSABLE標誌爲Activity,導致其出現不響應,因爲它已經變得不可重點。

更改此行

LayoutParams layoutParams = cordova.getActivity().getWindow().getAttributes(); 

LayoutParams layoutParams = new LayoutParams(); 

應該解決您的問題。

+0

謝謝!我還有一個問題:當我點擊'Home按鈕'時,拋出異常:Activity泄漏了窗口......從這行開始:localWindowManager.addView(colourView,layoutParams);.你知道什麼會導致它嗎? – tomwesolowski

+1

請創建一個單獨的問題。 – ntv1000

+0

http://stackoverflow.com/questions/21817888/activity-has-leaked-window-android – tomwesolowski