Android上的自定义视图和窗口属性

时间:2014-02-12 12:58:50

标签: java android cordova view

我想要做的是在我的应用程序顶部添加一个视图,它将是一个过滤视图(我想操纵屏幕的颜色),我也希望能够改变屏幕的亮度同时。这两件事似乎分开工作,但不是一起工作。

这是我的代码:

添加视图:

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

图层类:

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

当我将视图添加到应用程序时,之后,我想要更改屏幕的亮度 - 亮度正在改变,但我无法点击屏幕上的任何内容。几秒钟后,我得到一个'应用程序无响应消息。

导致我的申请冻结的原因是什么?

提前致谢。

1 个答案:

答案 0 :(得分:3)

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

在这一行中,您获得了Layoutparams的{​​{1}}对象。这只是对Activity中的LayoutParams对象的引用。

现在您要更改此对象:

Activity

我们正在更改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; 的属性,以及为 Activity 设置FLAG_NOT_FOCUSABLE标记,导致其显示无响应,因为它已经变得无法集中。

更改此行

Activity

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

应该解决你的问题。