自定义视图和窗口,在Android属性 [英] Custom views and window attributes on Android

查看:113
本文介绍了自定义视图和窗口,在Android属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的就是添加在我的应用程序之上的观点,这将是排序的过滤器视图(我想操纵屏幕的颜色),我也希望能够改变屏幕的亮度与此同时。这两种东西似乎分头工作,却不能在一起。

这是我的code:

添加观点:

  colourView =新层(cordova.getActivity());

窗口管理localWindowManager =(窗口管理器)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);
 

图层类:

 类层延伸查看
{
      私人诠释一个= 0;
      私人INT B = 0;
      私人诠释G = 0;
      私人诠释R = 0;

      公共层(上下文的背景下){
        超(上下文);
      }

      @覆盖
      保护无效的OnDraw(帆布油画){
        super.onDraw(画布);
        canvas.drawARGB(this.a,this.r,this.g,this.b);
        Log.d(显示,渲染..);
      }

      @覆盖
        保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){
            INT上级宽度= MeasureSpec.getSize(widthMeasureSpec);
            INT上级高度= MeasureSpec.getSize(heightMeasureSpec);
            this.setMeasuredDimension(上级宽度/ 2,上级高度);
            //既然你attatching到窗口使用的窗口布局PARAMS。
            WindowManager.LayoutParams的LayoutParams =新WindowManager.LayoutParams(上级宽度/ 2,
                    上级高度);
            this.setLayoutParams(的LayoutParams);

            super.onMeasure(widthMeasureSpec,heightMeasureSpec);
            Log.d(显示,填......);
        }

      公共无效setColor(INT A,INT R,INT克,int b)在{
        this.a =一个;
        this.r = R;
        this.g =克;
        this.b = B;
        无效();
       }
}
 

更改亮度:

  WindowManager.LayoutParams布局= cordova.getActivity()getWindow()的getAttributes();
尝试 {
    layout.screenBrightness =(浮点)arg_object.getDouble(亮度);
    // ^当我评论这条线,它并不能工作。
}赶上(JSONException E){
    e.printStackTrace();
}
cordova.getActivity()getWindow()setAttributes(布局)。;
 

在我的观点添加到应用程序,并在那之后,我想改变屏幕亮度 - 亮度发生变化,但我不能点击屏幕上的任何东西。后几秒钟,我得到一个应用程序没有响应的消息。

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

在此先感谢。

解决方案

 的LayoutParams的LayoutParams = cordova.getActivity()getWindow()的getAttributes()。
 

在这一行,你得到活动的的LayoutParams 对象。这仅仅是一个参考的活动的的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要更改活动和除其他事物的属性,设置 FLAG_NOT_FOCUSABLE 标记的< STRONG> 活动 ,导致其出现不响应,因为它已经取得了不可作为焦点。

更改该行

 的LayoutParams的LayoutParams = cordova.getActivity()getWindow()的getAttributes()。
 

 的LayoutParams的LayoutParams =新的LayoutParams();
 

应该解决您的问题。

What I want to do is add a view on top of my application which will be sort of a filter view (I want to manipulate the colors of the screen) and I also want to be able to change the brightness of the screen at the same time. Both of these things seem to work separately, but not together.

Here is my code:

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

Layer class:

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

Changing brightness:

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

When I add the view to the application and, after that, I want to change brightness of the screen - brightness is changing, but I can't click on anything on the screen. After few second I get an 'Application not responding message.

What causes my application to freeze?

Thanks in advance.

解决方案

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

In this line you getting the Layoutparams object of the Activity. This is only a reference to the LayoutParams object in the Activity.

Now you are changing this object:

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 you are changing the attributes of the Activity and amongst other things, setting the FLAG_NOT_FOCUSABLE flag for the Activity, causing it to appear not responding, because it has been made not focusable.

Changing this line

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

to

LayoutParams layoutParams = new LayoutParams();

should solve your problem.

这篇关于自定义视图和窗口,在Android属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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