Brightness Screen Filter

后端 未结 4 1615
暖寄归人
暖寄归人 2020-12-07 15:54

Does anyone have an idea how to implement an Brightness Screen Filter like the one here:

http://www.appbrain.com/app/screen-filter/com.haxor

I need a startin

4条回答
  •  抹茶落季
    2020-12-07 16:32

    • Get an instance of WindowManager.

      WindowManager windowManager = (WindowManager) Class.forName("android.view.WindowManagerImpl").getMethod("getDefault", new Class[0]).invoke(null, new Object[0]);

    • Create a full screen layout xml(layout parameters set to fill_parent)

    • Set your view as not clickable, not focusable, not long clickable, etc so that touch is passed through to your app and the app can detect it.

      view.setFocusable(false);
      view.setClickable(false);
      view.setKeepScreenOn(false);
      view.setLongClickable(false);
      view.setFocusableInTouchMode(false);

    • Create a layout parameter of type android.view.WindowManager.LayoutParams. LayoutParams layoutParams = new LayoutParams();

    • Set layout parameter like height, width etc

      layoutParams.height = LayoutParams.FILL_PARENT; 
      layoutParams.width = LayoutParams.FILL_PARENT;
      layoutParams.flags = 280; // You can try LayoutParams.FLAG_FULLSCREEN too
      layoutParams.format = PixelFormat.TRANSLUCENT; // You can try different formats
      layoutParams.windowAnimations = android.R.style.Animation_Toast; // You can use only animations that the system to can access
      layoutParams.type = LayoutParams.TYPE_SYSTEM_OVERLAY;
      layoutParams.gravity = Gravity.BOTTOM;
      layoutParams.x = 0;
      layoutParams.y = 0;
      layoutParams.verticalWeight = 1.0F;
      layoutParams.horizontalWeight = 1.0F;
      layoutParams.verticalMargin = 0.0F;
      layoutParams.horizontalMargin = 0.0F;
      
    • Key step: You can set what percentage of brightness you need. layoutParams.setBackgroundDrawable(getBackgroundDrawable(i));

      private Drawable getBackgroundDrawable(int i) {
      int j = 255 - (int) Math.round(255D * Math.exp(4D * ((double) i / 100D) - 4D));
      return new ColorDrawable(Color.argb(j, 0, 0, 0));}
      
    • Finally add view to windowManager that you created earlier.

      windowManager.addView(view, layoutParams);

    Note: You need SYSTEM_ALERT_WINDOW permission to lay an overlay on the screen.

    
    

    Have tested this and it works. Let me know if you get stuck.

提交回复
热议问题