我正在制作一个简单的动态壁纸。它应该绘制随机颜色、直径和位置的圆圈。当屏幕被触摸时,它应该在触摸位置上做随机圆圈。目前 ll 有效,唯一的问题是圆圈出现和消失。我想我应该画一个位图?这是我的来源。有谁知道如何使它起作用,所以它只是将圆圈相互叠加,而不会消失并再次出现?
package com.my.livewallpaper;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
public class MyWallpaperService extends WallpaperService {
@Override
public Engine onCreateEngine() {
return new MyWallpaperEngine();
}
private class MyWallpaperEngine extends Engine {
private final Handler handler = new Handler();
private final Runnable drawRunner = new Runnable() {
@Override
public void run() {
draw();
}
};
private List<MyPoint> circles;
private Paint paint = new Paint();
private int width;
int height;
public Canvas canvas = null;
private boolean visible = true;
private int maxNumber;
private int timestamp=0;
private boolean touchEnabled;
public MyWallpaperEngine() {
circles = new ArrayList<MyPoint>();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeJoin(Paint.Join.ROUND);
handler.post(drawRunner);
}
@Override
public void onVisibilityChanged(boolean visible) {
this.visible = visible;
if (visible) {
handler.post(drawRunner);
} else {
handler.removeCallbacks(drawRunner);
}
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
this.visible = false;
handler.removeCallbacks(drawRunner);
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
this.width = width;
this.height = height;
super.onSurfaceChanged(holder, format, width, height);
}
@Override
public void onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
SurfaceHolder holder = getSurfaceHolder();
//Canvas canvas = null;
try {
canvas = holder.lockCanvas();
if (canvas != null) {
drawCircles(canvas, x, y);
}
} finally {
if (canvas != null)
{holder.unlockCanvasAndPost(canvas);}
}
super.onTouchEvent(event);
}
private void draw() {
SurfaceHolder holder = getSurfaceHolder();
//Log.e("Msg","What you have to print");
try {
canvas = holder.lockCanvas();
if (canvas != null) {
drawCircles(canvas,-1f,-1f);
}
} finally {
if (canvas != null)
{
holder.unlockCanvasAndPost(canvas);
}
}
handler.removeCallbacks(drawRunner);
if (visible) {
handler.postDelayed(drawRunner, 400);
}
}
// Surface view requires that all elements are drawn completely
private void drawCircles(Canvas canvas, float xx, float yy) {
float minX = 10.0f;
float maxX = 60.0f;
Random rand = new Random();
float diameter = rand.nextFloat() * (maxX - minX) + minX;
float x = xx;
float y = yy;
if (xx==-1) {
x = (float) (width * Math.random());
y = (float) (height * Math.random());
}
paint.setARGB(rand.nextInt(200), rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
canvas.drawCircle(x, y, diameter, paint);
}
}
}