替え方は
- setBitmap(Bitmap bitmap)
- setResource(int resid)
- setStream(InputStream data)
まず AndroidManifest でパーミッションを設定する必要があります
AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- ... >
- ・
- ・
- ・
- <uses-permission
- android:name="android.permission.SET_WALLPAPER" />
- </manifest>
サンプルコード
- public class WallpaperSample extends Activity implements OnClickListener {
- WallpaperManager mWM;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.wallpaper_sample);
- Button button;
- button = (Button) findViewById(R.id.Button_Set);
- button.setOnClickListener(this);
- button = (Button) findViewById(R.id.Button_Clear);
- button.setOnClickListener(this);
- // WindowManager の取得
- mWM = WallpaperManager.getInstance(this);
- // 壁紙の最小の幅,最小の高さの取得
- int width = mWM.getDesiredMinimumWidth();
- int height = mWM.getDesiredMinimumHeight();
- }
- @Override
- public void onClick(View v) {
- switch(v.getId()) {
- case R.id.Button_Set:
- try {
- // 壁紙をリソースから設定
- mWM.setResource(R.drawable.icon);
- } catch (IOException e) {
- e.printStackTrace();
- }
- break;
- case R.id.Button_Clear:
- try {
- // 壁紙をデフォルトに戻す
- mWM.clear();
- } catch (IOException e) {
- e.printStackTrace();
- }
- break;
- }
- }
- }
プログラムを実行すると...
変更前
変更後
こんな感じ!
Activity の背景を壁紙にしたいときは
- <activity
- android:name=".activity.WallpaperSample"
- android:theme="@android:style/Theme.Wallpaper">
ちなみに
API 2.0以前ではできないので注意!
下記の方法で設定するようです
int width = getWallpaperDesiredMinimumWidth();
int height = getWallpaperDesiredMinimumHeight();
setWallpaper(Bitmap or InputStream);
clearWallpaper();
参考サイト
http://developer.android.com/intl/ja/reference/android/app/WallpaperManager.html
0 件のコメント:
コメントを投稿