【问题标题】:Custom views and window attributes on AndroidAndroid 上的自定义视图和窗口属性
【发布时间】:2014-02-12 12:58:50
【问题描述】:

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

这是我的代码:

添加视图:

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

当我将视图添加到应用程序后,我想更改屏幕的亮度 - 亮度正在更改,但我无法单击屏幕上的任何内容。几秒钟后,我收到一条“应用程序未响应消息”。

是什么导致我的应用程序冻结?

提前致谢。

【问题讨论】:

    标签: java android cordova view


    【解决方案1】:
    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; 
    

    因此,您正在更改 Activity 的属性以及其他内容,为 Activity 设置 FLAG_NOT_FOCUSABLE 标志,导致它看起来没有响应,因为它已被制作无法聚焦。

    改变这一行

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

    LayoutParams layoutParams = new LayoutParams();
    

    应该可以解决你的问题。

    【讨论】:

    • 谢谢!我还有一个问题:当我单击“主页按钮”时,抛出异常:Activity has leaked window... from this line: localWindowManager.addView(colourView, layoutParams);。你知道是什么原因造成的吗?
    • 请创建一个单独的问题。
    猜你喜欢
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 2014-12-28
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多