/**
* LiveWallpaper Launcher And Preference Template
* The MIT License
* copyright (c) 2011 mdlab.jp
* @author itoz
*/
package jp.mdlab.livewallpaper.template;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.view.SurfaceHolder;
import android.widget.Toast;
public class Main extends WallpaperService
{
// 設定画面クラスのパス
public static final String SHARED_PREFS_NAME = "jp.mdlab.livewallpaper.Preference";
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public Engine onCreateEngine() {
return new MyEngine(getResources());
}
public class MyEngine extends Engine implements SharedPreferences.OnSharedPreferenceChangeListener
{
SharedPreferences prefs;
private final Handler handler = new Handler();
private boolean visible;
// ================================================================
/**
* コンストラクタ
*/
public MyEngine(Resources r)
{
// 設定画面を取得
prefs = Main.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
// 設定画面の変更をリスナーする
prefs.registerOnSharedPreferenceChangeListener(this);
}
// ================================================================
/**
* 設定画面が変更されたときに呼ばれる
*/
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// ---------------------------
// 設定パネルの値を取得する
// ---------------------------
// list_keyを取得
String listStr = prefs.getString("list_key", "");
// checkbox_keyを取得
Boolean checkboxStr = prefs.getBoolean("checkbox_key", false);
// edittext_keyを取得
String edittextStr = prefs.getString("edittext_key", "");
// ---------------------------
// 値を表示する
// ---------------------------
Toast.makeText(getApplicationContext(),
"[設定値]" + "\nリスト=" + listStr + "\nチェックボックス=" + checkboxStr + "\nエディットテキスト=" + edittextStr,
Toast.LENGTH_SHORT).show();
}
// ================================================================
/**
* 描画
*/
void drawFrame() {
SurfaceHolder holder = getSurfaceHolder();
Canvas c = holder.lockCanvas();
// ---------------------------
// 描画処理など
// ---------------------------
Paint p = new Paint();
p.setTextSize(30);
p.setAntiAlias(true);
c.drawColor(Color.BLACK);
c.drawText("HELLO!", 10, 150, p);
c.drawText("LIVEWALLPAPER!", 10, 190, p);
holder.unlockCanvasAndPost(c);
handler.removeCallbacks(drawThread);
if (visible) handler.postDelayed(drawThread, 25);
}
private final Runnable drawThread = new Runnable() {
@Override
public void run() {
drawFrame();
}
};
@Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
}
@Override
public void onDestroy() {
super.onDestroy();
handler.removeCallbacks(drawThread);
}
@Override
public void onVisibilityChanged(boolean visible) {
this.visible = visible;
if (visible) {
drawFrame();
}
else {
handler.removeCallbacks(drawThread);
}
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
super.onSurfaceChanged(holder, format, width, height);
drawFrame();
}
@Override
public void onSurfaceCreated(SurfaceHolder holder) {
super.onSurfaceCreated(holder);
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
visible = false;
handler.removeCallbacks(drawThread);
}
@Override
public void onOffsetsChanged(float xOffset, float yOffset, float xStep, float yStep, int xPixels, int yPixels) {
drawFrame();
}
@Override
public void onDesiredSizeChanged(int desiredWidth, int desiredHeight) {
drawFrame();
}
}
}
- 1
- 2
- 3
前往页