0

我正在制作一个简单的动态壁纸。它应该绘制随机颜色、直径和位置的圆圈。当屏幕被触摸时,它应该在触摸位置上做随机圆圈。目前 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);
        }
    }

}
4

1 回答 1

1

使用以下代码将对您有所帮助。

package com.fingerprintmagic;

public class LineDrawPoint {
    Float x = 0.0f;
    Float y = 0.0f;
    String text;

    public LineDrawPoint(String text, Float x, Float y) {
        super();
        this.text = text;
        this.x = x;
        this.y = y;
    }

}

这是服务

package com.fingerprintmagic;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

public class FingerPrintService extends WallpaperService {

    private SurfaceHolder holder;
    private Canvas c = null;
    private List<LineDrawPoint> point;
    Paint paint;

    @Override
    public void onCreate() {
        super.onCreate();
        point = new ArrayList<LineDrawPoint>();
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setStrokeWidth(20);
        paint.setColor(Color.WHITE);
    }

    @Override
    public Engine onCreateEngine() {
        return new FingerPrintEngine();
    }

    class FingerPrintEngine extends Engine {

        private Boolean visble = true;
        private final Handler handle = new Handler();
        public Bitmap backgroundImage;

        public FingerPrintEngine() {
            backgroundImage = BitmapFactory.decodeResource(getResources(),
                    R.drawable.cupid_02);

        }

        private final Runnable drawRunner = new Runnable() {
            @Override
            public void run() {
                draw(point);
            }
        };

        @Override
        public Bundle onCommand(String action, int x, int y, int z,
                Bundle extras, boolean resultRequested) {
            return super.onCommand(action, x, y, z, extras, resultRequested);
        }

        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);
        }

        @Override
        public void onOffsetsChanged(float xOffset, float yOffset,
                float xOffsetStep, float yOffsetStep, int xPixelOffset,
                int yPixelOffset) {
            draw(point);
        }

        @Override
        public void onSurfaceCreated(SurfaceHolder holder) {
            super.onSurfaceCreated(holder);
            this.visble = false;
            handle.removeCallbacks(drawRunner);
        }

        @Override
        public void onTouchEvent(MotionEvent event) {
            super.onTouchEvent(event);
            float x = event.getX();
            float y = event.getY();
            point.add(new LineDrawPoint(String.valueOf(point.size() + 1), x, y));
            draw(point);
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            this.visble = visible;
            if (visible) {
                handle.post(drawRunner);
            } else {
                handle.removeCallbacks(drawRunner);
            }
            super.onVisibilityChanged(visible);
        }

        private void draw(List<LineDrawPoint> point) {
            holder = getSurfaceHolder();
            try {
                c = holder.lockCanvas();
                if (c != null) {
                    c.drawBitmap(backgroundImage, 0, 0, null);
                    drawCircles(c, point);
                }
            } catch (Exception e) {

            } finally {
                try {
                    if (c != null) {
                        holder.unlockCanvasAndPost(c);
                    }
                    handle.removeCallbacks(drawRunner);
                    if (visble) {
                        handle.postDelayed(drawRunner, 1);
                    }
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void drawCircles(Canvas canvas, List<LineDrawPoint> circles) {
        for (LineDrawPoint point : circles) {
            canvas.drawPoint(point.x, point.y, paint);
        }
    }
}

您可以根据需要更改 drawCircle 方法

于 2015-01-16T11:33:39.763 回答