亮度屏幕滤镜

Ale*_*lex 16 android screen filter brightness

有没有人知道如何实现像这里的亮度屏幕过滤器:

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

我需要一个起点,我无法弄清楚如何做到这一点.

phe*_*cks 8

只需制作透明的全屏活动即可让触摸通过.要进行触摸传递,请在设置contentView之前使用以下Window标志:

@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);
}
Run Code Online (Sandbox Code Playgroud)

对于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>
Run Code Online (Sandbox Code Playgroud)

然后调整"亮度"只需从代码中改变背景颜色的值:

findViewById(R.id.background).setBackgroundColor(0x66000000);
Run Code Online (Sandbox Code Playgroud)

  • 我已经独立实现了几乎这个精确的解决方案,它工作得很好,即使是像捏缩放等复杂的触摸事件.触摸事件问题可能是分开的. (2认同)

小智 5

  • 获取 WindowManager 的实例。

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

  • 创建全屏布局xml(布局参数设置为fill_parent

  • 将您的视图设置为不可点击、不可聚焦、不可长时间点击等,以便触摸传递到您的应用程序并且应用程序可以检测到它。

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

  • 创建类型为 的布局参数android.view.WindowManager.LayoutParamsLayoutParams layoutParams = new LayoutParams();

  • 设置布局参数,如高度、宽度等

    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;
    
    Run Code Online (Sandbox Code Playgroud)
  • 关键步骤:您可以设置您需要的亮度百分比。 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));}
    
    Run Code Online (Sandbox Code Playgroud)
  • 最后将视图添加到您之前创建的 windowManager 中。

    windowManager.addView(view, layoutParams);

注意:您需要获得SYSTEM_ALERT_WINDOW许可才能在屏幕上放置覆盖层。

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Run Code Online (Sandbox Code Playgroud)

已经测试过这个并且它有效。如果您遇到困难请告诉我。