C/C++プログラマの管理者が, Androidプログラムにチャレンジ. AndroidプログラミングのTipsをメモっていく予定です.

アプリケーション画面の明るさを変更するには, WindowManager.LayoutParamsクラスを利用する.



画面の明るさの変更

  • ScreenBrightness.java
    • WindowManager#getWindowメソッドで, LayoutParamsのインスタンスを取得する.
    • 初期値(0.0〜1.0)を代入する.
    • WindowManager#setWindowメソッドで, LayoutParamsを設定する.
  注) アプリケーション画面の明るさを変更しても, "画面の明るさを取得する"で取得できる端末の明るさは変更されない.
package com.moonlight_aska.android.screenbrightness;
import android.os.Bundle;
import android.app.Activity;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;

public class ScreenBrightness extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // LayoutParamsのインスタンスを取得
    LayoutParams lp = getWindow().getAttributes();
    // 明るさを設定
    lp.screenBrightness = 0.8F;
    getWindow().setAttributes(lp);

    TextView text = (TextView)findViewById(R.id.text_label);
    text.setText("明るさ:" + String.valueOf(lp.screenBrightness));
  }
}



コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Menu


逆引き(基礎編)

画面表示/操作(49)

フラグメント(1)

逆引き(応用編)

Firebase(2)

AD



管理人/副管理人のみ編集できます