activity_main.xml

```

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="add Filter"
        android:id="@+id/ButtonAddFilter"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Remove Filter"
        android:id="@+id/ButtonRemoveFilter"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
    </RelativeLayout>
    
```

MainActivity.java

```

    public class MainActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        Button buttonAddFilter = (Button) findViewById(R.id.ButtonAddFilter);
        buttonAddFilter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, FilterService.class);
                startService(intent);
            }
        });
    
        Button buttonRemoveFilter = (Button) findViewById(R.id.ButtonRemoveFilter);
        buttonRemoveFilter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, FilterService.class);
                stopService(intent);
            }
        });
    }
    }
    
```

filter.xml

```

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    </LinearLayout>
    
```

FilterService.java

```

    public class FilterService extends Service {
    
    private View mView;
    private WindowManager mWindowManager;
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    
        LayoutInflater layoutInflater = LayoutInflater.from(this);
    
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,    
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT                       
        );
    
        mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    
        mView = layoutInflater.inflate(R.layout.filter, null);
    
        mView.setBackgroundColor(Color.argb(80, 0, 0, 0));
    
        mWindowManager.addView(mView, layoutParams);
    
        return START_NOT_STICKY;
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        mWindowManager.removeView(mView);
    }
    
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    }
    
```

これは画面にフィルターをかける処理です。この場合、ステータスバーが暗くなりません。  
ステータスバーの場所までmWindowManagerを反映させる、またはステータスバーを違和感が無いように暗くする処理はあるのでしょうか。
CC BY-SA 3.0
2015-10-05T06:24:09.403
17261
2016-10-09T04:24:25.840
2015-10-05T07:14:01.380
7290
9850
question
android java android-studio android-layout
ステータスバーにもフィルターをかけたい
これをやってみてください。

```

    WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    getStatusBarHeight(),
                    WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                    PixelFormat.TRANSLUCENT);
    
        layoutParams.gravity =  Gravity.TOP|Gravity.CENTER;      
    
        mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    
        mView = layoutInflater.inflate(R.layout.filter, null);
    
        mView.setBackgroundColor(Color.argb(80, 0, 0, 0));
    
        mWindowManager.addView(mView, layoutParams);
    
    private static int getStatusBarHeight(android.content.res.Resources res) {
        return (int) (24 * res.getDisplayMetrics().density);
    }
    
```

Permissionも忘れないです。

```

    <uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
    
```
CC BY-SA 3.0
2015-10-09T01:27:14.377
17443
2015-10-09T01:27:14.377
2437
17261
answer