fc2ブログ

記事一覧

画面の明るさを調整・変更する方法 | Xamarin.Forms


今回は Xamarin.Forms で スマホ画面の明るさを調整・変更する方法についてご紹介いたします。
今回もデバイス特有の制御が必要となり、お馴染みの DependencyService を使用しています。


前提条件
・Windows10 Pro 64Bit 1709
・Visual Studio 2015 Community Update3
・Xamarin 4.8.0.760 (NuGet Xamarin.Forms 2.4.0.282)
・macOS Sierra 10.12.6 / Xcode 9 / Xamarin.iOS 11.6.1.4



1.PCLの記述方法

(1)PCL プロジェクト内に DependencyService で呼び出すためのインターフェースを配置します。

IDeviceService.cs
namespace AppName.Services
{
public interface IDeviceService
{
void SetScreenBrightness(float value);
float GetScreenBrightness();
}
}



2.Androidの実装方法

(1)Androidプロジェクト内の AndroidManifest.xml に以下の設定値を追加します。

AndroidManifest.xml

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


(2)Android プロジェクト内に DeviceService クラスを配置します。

DeviceService.cs
using System;
using Android.App;
using Android.Provider;
using Android.Views;
using Xamarin.Forms;
using AppName.Droid.Services;
using AppName.Services;
[assembly: Dependency(typeof(DeviceService))]
namespace AppName.Droid.Services
{
public class DeviceService : IDeviceService
{
/// <summary>
/// 画面の明るさを設定する
/// </summary>
/// <param name="value"></param>
public void SetScreenBrightness(float value)
{
//現在のアプリの画面の明るさを設定する
            var activity = Forms.MainActivity;
            Window wm = activity.Window;
            WindowManagerLayoutParams param =  wm.Attributes;
            param.ScreenBrightness = value;
            wm.Attributes = param;
}

/// <summary>
/// 画面の明るさを取得する
/// </summary>
/// <returns></returns>
public float GetScreenBrightness()
{
var activity = (Activity)Forms.Context;

//アプリの画面の明るさを取得する
var attributesWindow = new WindowManagerLayoutParams();
Window wm = activity.Window;
attributesWindow.CopyFrom(wm.Attributes);
if (attributesWindow.ScreenBrightness >= 0)
{
return attributesWindow.ScreenBrightness;
}

//OSの画面の明るさを取得する
string brightness = Settings.System.GetString(activity.ContentResolver, Settings.System.ScreenBrightness);
float ret = 0f;
if (!String.IsNullOrEmpty(brightness) &&
float.TryParse(brightness, out ret))
{
return ret / 255;
}
return ret;
}
}
}

※2019/05/07修正
Android でアプリの画面の明るさが取得できない場合は OS の画面の明るさを取得するようにコードを変更しました。
つまり、Android では OS の画面の明るさとアプリの画面の明るさが別に制御されていますので、注意して実装してください。

※2022/07/03修正
Android で正しく明るさを変更できないケースがりましたので、ソースコードを修正しました。activityを取得する部分は自プロパティです。予めMainActivityのインスタンスを保持しているStaticプロパティです。



3.iOSの実装方法

(1)iOS プロジェクト内に DeviceService クラスを配置します。

DeviceService.cs
using System;
using UIKit;
using Xamarin.Forms;
using AppName.iOS.Services;
using AppName.Services;
[assembly: Dependency(typeof(DeviceService))]
namespace AppName.iOS.Services
{
public class DeviceService : IDeviceService
{
/// <summary>
/// 画面の明るさを設定する
/// </summary>
/// <param name="value"></param>
public void SetScreenBrightness(float value)
{
UIScreen.MainScreen.Brightness = value;
}

/// <summary>
/// 画面の明るさを取得する
/// </summary>
/// <returns></returns>
public float GetScreenBrightness()
{
return (float)(UIScreen.MainScreen.Brightness);
}
}
}

※iOS では画面の明るさを変更するとシステム全体の明るさも変更されるため、影響範囲を考慮して実装するようにしましょう。



4.使用方法

PCL プロジェクトの中の任意のページに記述します。
変更する前に現在の画面の明るさの設定値を記憶しておき、明るさの設定を戻す場合に一度記憶した設定値で上書きするようにしています。

TestPage.xaml.cs
using AppName.Services;
using Xamarin.Forms;
public class TestPage : ContentPage
{
private float _screenBrithness = 0f;
void SetScreenBrightness_Click(object sender, EventArgs e)
{
//画面の明るさを設定する(0~1の間で)
_screenBrithness = DependencyService.Get<IDeviceService>().GetScreenBrightness();
DependencyService.Get<IDeviceService>().SetScreenBrightness(0f);
}
void ClearScreenBrightness_Click(object sender, EventArgs e)
{
//画面の明るさを元に戻す
DependencyService.Get<IDeviceService>().SetScreenBrightness(_screenBrithness);
}
}






最後までお読みいただきありがとうございます。
当ブログの内容をまとめた Xamarin逆引きメニュー は以下のURLからご覧になれます。
http://itblog.dynaspo.com/blog-entry-81.html



関連記事

コメント

コメントの投稿

※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。

 入力されていないコメントには返信しませんのであらかじめご了承くださいませ。

※ニックネームでも良いので必ずご入力ください。

    

※必ずご入力ください。

    
    

※必ずご入力ください。

※技術的な質問には環境やエラーについて正確かつ詳細にお教えください。

・正確なエラーの内容

・Windowsのバージョン番号

・Visual Studioのバージョン

・機器の型番

・アプリやソフトのバージョン

    

カテゴリ別記事一覧

広告

プロフィール

石河 純


著者名 :石河 純
自己紹介:素人上がりのIT技術者。趣味は卓球・車・ボウリング

IT関連の知識はざっくりとこんな感じです。
【OS関連】
WindowsServer: 2012/2008R2/2003/2000/NT4
Windows: 10/8/7/XP/2000/me/NT4/98
Linux: CentOS RedHatLinux9
Mac: macOS Catalina 10.15 / Mojave 10.14 / High Sierra 10.13 / Sierra 10.12 / OSX Lion 10.7.5 / OSX Snow Leopard 10.6.8
【言語】
VB.net ASP.NET C#.net Java VBA
Xamarin.Forms
【データベース】
Oracle 10g/9i
SQLServer 2016/2008R2/2005/2000
SQLAnywhere 16/11/8
【BI/レポートツール】
Cognos ReportNet (IBM)
Microsoft PowerBI
ActiveReport (GrapeCity)
CrystalReport
【OCX関連】
GrapeCity InputMan SPREAD MultiRow GridView
【ネットワーク関連】
CCNP シスコ技術者認定
Cisco Catalyst シリーズ
Yamaha RTXシリーズ
FireWall関連
【WEB関連】
SEO SEM CSS jQuery IIS6/7 apache2

休みの日は卓球をやっています。
現在、卓球用品通販ショップは休業中です。