2010-11-26 40 views
14

有沒有人有一個想法如何實現像這裏的一個的亮度濾網:亮度濾網

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

我需要一個起點,我無法弄清楚如何做到這一點。

+0

我面臨同樣的問題。我也發佈了這個問題http://stackoverflow.com/questions/2222146/control-screen-brightness-in-android-using-background-service。我能解決這個問題。你可以在帖子中看到正確的方法。 – 2011-04-14 08:09:50

+0

試試這個鏈接它可能會幫助你一點:http://android-er.blogspot.com/2011/02/change-android-screen-brightness.html – 2011-05-20 12:56:03

回答

8

只需製作透明的全屏活動即可讓觸摸通過。爲了使觸摸通過使用下面的窗口標誌設置內容查看前:

@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); 
+0

謝謝,它的工作與設置透明度有關,但觸摸事件似乎奇怪,我可以滾動,但當我點擊一個程序圖標看起來像凍結(不完全凍結,但不打開程序) – Alex 2010-11-26 20:13:56

+0

任何其他的想法?我不認爲頂部有一個透明的活動是解決方案 – Alex 2010-11-29 12:20:57

0

您CA也試試這個:

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

    } 
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"/> 
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.addView(view, layoutParams);

注意:您需要SYSTEM_ALERT_WINDOW許可奠定了屏幕上的覆蓋。

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

已經測試過,它的工作原理。讓我知道如果你卡住了。