public class MyWallpaperService extends WallpaperService {
int x, y;
Canvas c;
Rect dest;
Paint paint;
private Path m_Path;
ArrayList<Pair<Path, Paint>> paths = new ArrayList<Pair<Path, Paint>>();
ArrayList<Pair<Path, Paint>> undonePaths = new ArrayList<Pair<Path, Paint>>();
private float mX, mY;
private static final float TOUCH_TOLERANCE = 6;
public void onCreate() {
super.onCreate();
}
public void onDestroy() {
super.onDestroy();
}
public Engine onCreateEngine() {
return new MyWallpaperEngine();
}
class MyWallpaperEngine extends Engine {
private final Handler handler = new Handler();
private final Runnable drawRunner = new Runnable() {
@Override
public void run() {
draw();
}
};
private boolean visible = true;
public Bitmap image1, backgroundImage;
MyWallpaperEngine() {
// get the fish and background image references
image1 = BitmapFactory.decodeResource(getResources(),
R.drawable.fish);
backgroundImage = BitmapFactory.decodeResource(getResources(),
R.drawable.back);
x = -130; // initialize x position
y = 200; // initialize y position
}
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
}
@Override
public void onVisibilityChanged(boolean visible) {
this.visible = visible;
// if screen wallpaper is visible then draw the image otherwise do
// not draw
if (visible) {
handler.post(drawRunner);
} else {
handler.removeCallbacks(drawRunner);
}
}
@Override
public void onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
super.onTouchEvent(event);
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(getApplicationContext(), "TODO", Toast.LENGTH_SHORT).show();
touch_start(x, y);
break;
case MotionEvent.ACTION_MOVE:
// Toast.makeText(null, "TODO", Toast.LENGTH_LONG).show();
touch_move(x, y);
break;
case MotionEvent.ACTION_UP:
// Toast.makeText(null, "TODO", Toast.LENGTH_LONG).show();
touch_up();
break;
}
}
protected void onDraw(Canvas canvas) {
for (Pair<Path, Paint> p : paths) {
canvas.drawPath(p.first, p.second);
}
}
private void touch_up() {
// TODO Auto-generated method stub
m_Path.lineTo(mX, mY);
// commit the path to our offscreen
c.drawPath(m_Path, paint);
// kill this so we don't double draw
m_Path = new Path();
Paint newPaint = new Paint(paint); // Clones the mPaint object
paths.add(new Pair<Path, Paint>(m_Path, newPaint));
}
private void touch_move(float x, float y) {
// TODO Auto-generated method stub
paint.setColor(Color.TRANSPARENT);
paint.setStrokeWidth(2);
Paint newPaint = new Paint(paint); // Clones the mPaint object
paths.add(new Pair<Path, Paint>(m_Path, newPaint));
m_Path.reset();
m_Path.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_start(float x, float y) {
// TODO Auto-generated method stub
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
m_Path.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
this.visible = false;
handler.removeCallbacks(drawRunner);
}
public void onOffsetsChanged(float xOffset, float yOffset, float xStep,
float yStep, int xPixels, int yPixels) {
draw();
}
void draw() {
final SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
try {
c = holder.lockCanvas();
// clear the canvas
c.drawColor(Color.BLACK);
DisplayMetrics displaymetrics = new DisplayMetrics();
((WindowManager) getBaseContext().getSystemService(
Context.WINDOW_SERVICE)).getDefaultDisplay()
.getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
if (c != null) {
Rect dest = new Rect(0, 0, width, height);
Paint paint = new Paint();
paint.setFilterBitmap(true);
c.drawBitmap(backgroundImage, null, dest, paint);
Rect im = new Rect(0, 0, width, height);
Paint paint1 = new Paint();
paint.setFilterBitmap(true);
c.drawBitmap(image1, null, im, paint1);
int width1 = c.getWidth();
// if x crosses the width means x has reached to right edge
if (x > width1 + 100) {
// assign initial value to start with
x = -130;
}
// change the x position/value by 1 pixel
x = x + 1;
}
}
finally {
if (c != null)
holder.unlockCanvasAndPost(c);
}
handler.removeCallbacks(drawRunner);
if (visible) {
handler.postDelayed(drawRunner, 10); // delay 10 mileseconds
}
}
}
}
logcat的
06-06 09:35:34.613: I/dalvikvm(8774): Turning on JNI app bug workarounds for target SDK version 10...
06-06 09:35:34.636: E/Trace(8774): error opening trace file: No such file or directory (2)
06-06 09:35:39.730: D/AndroidRuntime(8774): Shutting down VM
06-06 09:35:39.730: W/dalvikvm(8774): threadid=1: thread exiting with uncaught exception (group=0x40f172a0)
06-06 09:35:39.730: E/AndroidRuntime(8774): FATAL EXCEPTION: main
06-06 09:35:39.730: E/AndroidRuntime(8774): java.lang.NullPointerException
06-06 09:35:39.730: E/AndroidRuntime(8774): at com.sabeel.livewallpaper.MyWallpaperService$MyWallpaperEngine.touch_start(MyWallpaperService.java:159)
06-06 09:35:39.730: E/AndroidRuntime(8774): at com.sabeel.livewallpaper.MyWallpaperService$MyWallpaperEngine.onTouchEvent(MyWallpaperService.java:101)
06-06 09:35:39.730: E/AndroidRuntime(8774): at android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:1102)
06-06 09:35:39.730: E/AndroidRuntime(8774): at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:61)
06-06 09:35:39.730: E/AndroidRuntime(8774): at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 09:35:39.730: E/AndroidRuntime(8774): at android.os.Looper.loop(Looper.java:137)
06-06 09:35:39.730: E/AndroidRuntime(8774): at android.app.ActivityThread.main(ActivityThread.java:5059)
06-06 09:35:39.730: E/AndroidRuntime(8774): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 09:35:39.730: E/AndroidRuntime(8774): at java.lang.reflect.Method.invoke(Method.java:511)
06-06 09:35:39.730: E/AndroidRuntime(8774): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
06-06 09:35:39.730: E/AndroidRuntime(8774): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
06-06 09:35:39.730: E/AndroidRuntime(8774): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
试试这个..
内部m_Path
方法中 touch_start()
null 尝试初始化它,如下所示并使用它。
m_Path = new Path();