シンプルなライブ壁紙を作ろうと思っています。ランダムな色、直径、位置の円を描くことになっています。そして、スクリーンがタッチされると、タッチ位置でランダムな円を描く必要があります。現在は動作しますが、唯一の問題は、円が表示されたり消えたりすることです。私はビットマップに描くべきだと思いますか?これが私の情報源です。これを機能させる方法を知っている人はいますか?
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);
}
}
}