问题
Does anyone have an idea how to implement an Brightness Screen Filter like the one here:
http://www.appbrain.com/app/screen-filter/com.haxor
I need a starting point and I can't figure out how to do it.
回答1:
Just make a transparent full screen activity that lets touches pass through. To make touches pass through use the following Window flags before setting the contentView:
@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);
}
For your main.xml layout file just use a full screen LinearLayout with a transparent background:
<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>
Then to adjust the "brightness" just change the value of the background colour from your code somewhere:
findViewById(R.id.background).setBackgroundColor(0x66000000);
回答2:
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);
Note: You need SYSTEM_ALERT_WINDOW
permission to lay an overlay on the screen.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Have tested this and it works. Let me know if you get stuck.
回答3:
Of course you can't use this is production code, but if you are playing around .. try this Undocumented hack
It uses :
private void setBrightness(int brightness) {
try {
IHardwareService hardware = IHardwareService.Stub.asInterface(
ServiceManager.getService("hardware"));
if (hardware != null) {
hardware.setScreenBacklight(brightness);
}
} catch (RemoteException doe) {
}
}
Remember that it uses this permission :
<uses-permission android:name="android.permission.HARDWARE_TEST"/>
回答4:
You ca try this also:
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);
}
来源:https://stackoverflow.com/questions/4286075/brightness-screen-filter