エンジニア未満がエンジニア以上を目指す日々(仮)

戦力外通告の悔しさをバネに自身の技術力向上を目指した技術ブログ

壁紙サイズの固定 - 壁紙変更アプリ (5)

画像表示が期待通りになっていないので対策を考えます。

f:id:hielf:20140601210222p:plain:w300

まずは Nexus7 の標準ホームの画面サイズはどうなっているか確認。
参考にしたのは公式。
WallpaperManager | Android Developers

サイズ取得はこの辺りかな?

Public Methods Explanation
int getDesiredMinimumHeight() Returns the desired minimum height for the wallpaper.
int getDesiredMinimumWidth() Returns the desired minimum width for the wallpaper.

で、実装。

        int height = mWM.getDesiredMinimumHeight();
        int width = mWM.getDesiredMinimumWidth();

        Log.v(TAG, "Initial height = " + height);
        Log.v(TAG, "Initial width  = " + width);

結果はこれ。

06-08 02:59:13.343    V/WallpaperChange? Initial height = 1920
06-08 02:59:13.343    V/WallpaperChange? Initial width  = 2108

width が 2108 という謎サイズになってる。。
Nexus7 は Full HD なので、スクロール分でこうなってるのか。
今回はスクリーンショットで取得した画像を壁紙に設定しているから、
Full HD で固定したいのだが、、、
この辺りかな?

Public Methods Explanation
void suggestDesiredDimensions(int minimumWidth, int minimumHeight) For use only by the current home application, to specify the size of wallpaper it would like to use.

permission に SET_WALLPAPER_HINTS が必要なので追加。
サイズを width = 1200, height = 1920 決め打ちで設定しました。

    <uses-permission android:name="android.permission.SET_WALLPAPER_HINTS" />
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String click_parent = (String) parent.getClass().getSimpleName();
                        String click_position = String.valueOf(position);
                        String click_id = String.valueOf(id);

                        Log.v(TAG, String.format("onItemClick click_parent   : %s", click_parent));
                        Log.v(TAG, String.format("onItemClick click_position : %s", click_position));
                        Log.v(TAG, String.format("onItemClick click_id       : %s", click_id));

                        // Implementation for changing the wallpaper
                        Log.v(TAG, "onItemClick:"+files[position].toString());
                        bmpImageFile = BitmapFactory.decodeFile(files[position].toString());

                        int height = mWM.getDesiredMinimumHeight();
                        int width = mWM.getDesiredMinimumWidth();

                        Log.v(TAG, "before height:"+height);
                        Log.v(TAG, "before width:"+width);

                        try {
                            mWM.setBitmap(bmpImageFile);
                            mWM.suggestDesiredDimensions (1200, 1920);

                            height = mWM.getDesiredMinimumHeight();
                            width = mWM.getDesiredMinimumWidth();

                            Log.v(TAG, "after height:"+height);
                            Log.v(TAG, "after width:"+width);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

実行すると、height, width が期待する値に変更になりました。

06-08 02:59:39.939    V/WallpaperChange? before height:1920
06-08 02:59:39.939    V/WallpaperChange? before width:2108
06-08 02:59:40.820    V/WallpaperChange? after height:1920
06-08 02:59:40.820    V/WallpaperChange? after width:1200

で、無事に画像も期待した通りに表示されました。

f:id:hielf:20140608121442p:plain:w300

ただ、この変更したサイズって壁紙をクリアしても戻らない。。

06-08 03:03:18.843    V/WallpaperChange? before height:1920
06-08 03:03:18.843    V/WallpaperChange? before width:1200
06-08 03:03:19.654    V/WallpaperChange? after height:1920
06-08 03:03:19.654    V/WallpaperChange? after width:1200

アプリから再設定するか、ホームアプリの再起動が必要なんだろうな。。