【问题标题】:Brightness Screen Filter亮度屏幕过滤器
【发布时间】:2011-05-16 05:23:10
【问题描述】:

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

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

我需要一个起点,但我不知道该怎么做。

【问题讨论】:

标签: android filter screen brightness


【解决方案1】:

只需制作一个透明的全屏活动,让触摸通过。要使触摸通过,请在设置 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);
}

对于您的 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);

【讨论】:

  • 谢谢,它与设置透明度有关,但触摸事件似乎很奇怪,我可以滚动,但是当我点击一个程序图标时,它似乎冻结了(不是完全冻结,但没有打开程序)
  • 还有其他想法吗?我不认为在顶部有一个透明的活动是解决方案
  • 我没有放弃,除了OpenGL和SurfaceView相关的东西我也没有什么新的想法,但我还是希望有人能清楚地知道这是怎么做的。
  • Alex,我确实认为我的解决方案应该有效。我做过类似的事情,并且触摸传递到后面的应用程序没有任何问题。尝试使用窗口标志,看看你是否可以让它工作
  • 我已经独立实现了几乎这个精确的解决方案,并且它运行良好,即使是像捏缩放这样的复杂触摸事件。触摸事件问题可能是单独的。
【解决方案2】:
  • 获取 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.LayoutParams 类型的布局参数。 LayoutParams 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;
    
  • 关键步骤:您可以设置所需的亮度百分比。 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));}
    
  • 最后将视图添加到您之前创建的 windowManager。

    windowManager.addView(view, layoutParams);

注意:您需要SYSTEM_ALERT_WINDOW 权限才能在屏幕上放置叠加层。

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

已对此进行了测试,并且可以正常工作。如果您遇到困难,请告诉我。

【讨论】:

  • 请您对此question 发表完整的答案,将不胜感激使用示例
【解决方案3】:

当然你不能使用这是生产代码,但如果你在玩......试试这个Undocumented hack

它使用:

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"/>

【讨论】:

【解决方案4】:

你也可以试试这个:

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

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 2013-04-13
    相关资源
    最近更新 更多