Created
March 5, 2011 10:36
-
-
Save itoz/856286 to your computer and use it in GitHub Desktop.
[Android] LiveWallpaper Template (アンドロイドライブ壁紙テンプレート)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
*============================================================ | |
* copyright(c) 2011 www.romatica.com | |
* @author itoz | |
* 2011/03/05 | |
*============================================================ | |
*/ | |
package ****.****.*****; | |
import android.content.res.Resources; | |
import android.graphics.Canvas; | |
import android.os.Handler; | |
import android.service.wallpaper.WallpaperService; | |
import android.view.SurfaceHolder; | |
public class WallpaperSample extends WallpaperService{ | |
// ================================================================ | |
// 生成時に呼ばれる | |
@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 { | |
//ハンドラ | |
private final Handler handler = new Handler(); | |
// 表示状態 | |
private boolean visible; | |
// ================================================================ | |
// コンストラクタ | |
public MyEngine(Resources r) { | |
} | |
// ================================================================ | |
// 描画スレッド | |
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(); | |
} | |
// ================================================================ | |
// フレームの描画 | |
void drawFrame() { | |
// ロック | |
SurfaceHolder holder = getSurfaceHolder(); | |
Canvas c = holder.lockCanvas(); | |
/** | |
* ここに描画処理 | |
* ex) | |
* c.drawColor(Color.BLACK); | |
* c.drawBitmap(****, 0 , 0 , null); | |
*/ | |
// アンロック | |
holder.unlockCanvasAndPost(c); | |
// 再描画 | |
handler.removeCallbacks(drawThread); | |
if (visible) | |
handler.postDelayed(drawThread, 25); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment