亮度屏幕滤镜

时间:2010-11-26 14:24:06

标签: android filter screen brightness

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

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

我需要一个起点,我无法弄清楚如何去做。

4 个答案:

答案 0 :(得分: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);
}

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

答案 1 :(得分:4)

  • 获取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"/>

测试了这个并且它有效。如果你遇到困难,请告诉我。

答案 2 :(得分:1)

当然你不能使用这是生产代码,但是如果你正在玩...试试这个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"/>

答案 3 :(得分: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);

    }