1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| package ca.jvsh.livewallpaper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
public class LiveWallpaper extends WallpaperService
{
public static final String SHARED_PREFS_NAME = "livewallpapersettings";
private final Handler mHandler = new Handler();
public Bitmap myBg;
public int idx = 0;
public static final int NUM_RES = 20;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public Engine onCreateEngine() {
return new CubeEngine();
}
class CubeEngine extends Engine {
private final Paint mPaint = new Paint();
private boolean mAnime = true;
private Matrix mMatrix = new Matrix();
private final Runnable mDrawAnim = new Runnable() {
@Override
public void run() {
drawFrame();
}
};
private boolean mVisible;
CubeEngine() {
myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n01);
}
@Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
setTouchEventsEnabled(false);
}
@Override
public void onDestroy() {
super.onDestroy();
mHandler.removeCallbacks(mDrawAnim);
}
@Override
public void onVisibilityChanged(boolean visible) {
mVisible = visible;
if (visible) {
drawFrame();
} else {
mHandler.removeCallbacks(mDrawAnim);
}
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
super.onSurfaceChanged(holder, format, width, height);
float w = myBg.getWidth();
float s = width / w;
mMatrix.reset();
mMatrix.setScale(s, s);
drawFrame();
}
@Override
public void onSurfaceCreated(SurfaceHolder holder) {
super.onSurfaceCreated(holder);
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
mVisible = false;
mHandler.removeCallbacks(mDrawAnim);
}
@Override
public void onOffsetsChanged(float xOffset, float yOffset,
float xStep, float yStep, int xPixels, int yPixels) {
drawFrame();
}
@Override
public void onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
mAnime = !mAnime;
}
super.onTouchEvent(event);
}
void drawFrame() {
final SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
try {
c = holder.lockCanvas();
if (c != null) {
// draw something
drawAnim(c);
//drawTouchPoint(c);
}
} finally {
if (c != null) holder.unlockCanvasAndPost(c);
}
// Reschedule the next redraw
mHandler.removeCallbacks(mDrawAnim);
if (mVisible && mAnime) {
mHandler.postDelayed(mDrawAnim, 50 );
}
}
void drawAnim(Canvas c) {
if (idx == NUM_RES) {idx = 0;}
idx += 1;
switch (idx) {
case 0: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n01), mMatrix, mPaint); break;
case 1: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n02), mMatrix, mPaint); break;
case 2: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n03), mMatrix, mPaint); break;
case 3: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n04), mMatrix, mPaint); break;
case 4: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n05), mMatrix, mPaint); break;
case 5: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n06), mMatrix, mPaint); break;
case 6: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n07), mMatrix, mPaint); break;
case 7: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n08), mMatrix, mPaint); break;
case 8: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n09), mMatrix, mPaint); break;
case 9: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n10), mMatrix, mPaint); break;
case 10: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n11), mMatrix, mPaint); break;
case 11: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n12), mMatrix, mPaint); break;
case 12: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n13), mMatrix, mPaint); break;
case 13: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n14), mMatrix, mPaint); break;
case 14: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n15), mMatrix, mPaint); break;
case 15: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n16), mMatrix, mPaint); break;
case 16: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n17), mMatrix, mPaint); break;
case 17: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n18), mMatrix, mPaint); break;
case 18: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n19), mMatrix, mPaint); break;
case 19: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n20), mMatrix, mPaint); break;
}
}
}
} |
Partager