亮度屏幕过滤器

17

我曾经遇到过同样的问题。我也在https://dev59.com/-0vSa4cB1Zd3GeqPhtZF上发布了这个问题。我最终解决了这个问题。你可以在帖子中看到正确的方法。 - Naveen Murthy
试试这个链接,它可能会对你有所帮助:http://android-er.blogspot.com/2011/02/change-android-screen-brightness.html - user762749
4个回答

8

创建一个透明的全屏Activity并让触摸事件穿透。在设置contentView之前,使用以下Window flags使触摸事件穿透:

@Override
public void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  Window window = getWindow();

  // Let touches go through to apps/activities underneath.
  window.addFlags(FLAG_NOT_TOUCHABLE);

  // Now set up content view
  setContentView(R.layout.main);
}

对于您的main.xml布局文件,只需使用一个全屏幕LinearLayout,并设置透明背景:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/background"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#33000000">
</LinearLayout>

然后要调整“亮度”,只需在代码中某处更改背景颜色的值即可:
findViewById(R.id.background).setBackgroundColor(0x66000000);

谢谢,关于设置透明度它正在工作,但触摸事件似乎有些奇怪,我可以滚动,但当我点击一个程序图标时,它似乎会冻结(不完全冻结,但无法打开程序)。 - Alex
还有其他的想法吗?我认为在顶部放置一个透明的活动并不是解决办法。 - Alex
我并不打算放弃,除了与OpenGL和SurfaceView相关的一些想法外,我没有任何新的想法,但我仍然希望有人能够清楚地知道如何完成这个任务。 - Alex
Alex,我认为我的解决方案应该可行。我做过类似的事情,并且没有触摸事件传递到后面的应用程序的问题。尝试调整窗口标志以查看是否可以使其工作。 - pheelicks
2
我已经独立实现了几乎完全相同的解决方案,并且它运行得非常完美,即使涉及到像捏合缩放这样复杂的触摸事件。触摸事件问题可能是另一个问题。 - OverclockedTim
2016年 window.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); - Andy the android

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

注意:您需要SYSTEM_ALERT_WINDOW 权限才能在屏幕上放置覆盖层。
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

我已经测试过了,它可以运行。如果你遇到问题,请告诉我。

请问您能否发布一个完整的答案来回答这个问题。使用示例将会非常感激。 - noobProgrammer

1

当然,您不能将这个用作生产代码,但如果您在玩耍...可以尝试这个未经记录的黑客技巧

它使用:

private void setBrightness(int brightness) {
try {
  IHardwareService hardware = IHardwareService.Stub.asInterface(
   ServiceManager.getService("hardware"));
  if (hardware != null) {
    hardware.setScreenBacklight(brightness);
  }
 } catch (RemoteException doe) {          
  }        
 }

请记得使用此权限:

 <uses-permission android:name="android.permission.HARDWARE_TEST"/>

1
我能在哪里找到hardware09.jar文件?用于这个教程:http://www.tutorialforandroid.com/2009/01/changing-screen-brightness.html - noobProgrammer

0

你也可以尝试这个:

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.max_bright);



        WindowManager.LayoutParams lp = getWindow().getAttributes();

        lp.screenBrightness = 100 / 100.0f;

        getWindow().setAttributes(lp);

    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接