如何修改我的Android代码,以便在使用WallpaperService时每隔10分钟更改一次背景壁纸?

时间:2019-05-09 06:15:27

标签: android android-canvas

我尝试了很多事情,但是似乎无法设置计时器,因此我有点新,所以我可能做错了什么,但是我尝试实现我在Stack Overflow中看到的代码。我需要做什么来使背景每10分钟更改一次?

WallpaperService

public class ClockWallpaperService extends WallpaperService {

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

private class ClockWallpaperEngine extends Engine implements
        OnSharedPreferenceChangeListener {
    private final Handler handler = new Handler();
    private final Runnable drawRunner = new Runnable() {
        @Override
        public void run() {
            draw();
        }

    };

    private Paint paint;
    /** hands colors for hour, min, sec */
    private int[] colors = { 0xFFFF0000, 0xFF0000FF, 0xFFA2BC13 };
    private int bgColor;
    private int width;
    private int height;
    private boolean visible = true;
    private boolean displayHandSec;
    private AnalogClock clock;
    private SharedPreferences prefs;

    public ClockWallpaperEngine() {
        prefs = PreferenceManager
                .getDefaultSharedPreferences(ClockWallpaperService.this);
        prefs.registerOnSharedPreferenceChangeListener(this);
        displayHandSec = prefs.getBoolean(
                SettingsActivity.DISPLAY_HAND_SEC_KEY, true);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        bgColor = Color.parseColor("#C0C0C0");
        clock = new AnalogClock(getApplicationContext());
        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);
        prefs.unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSurfaceChanged(SurfaceHolder holder, int format,
            int width, int height) {
        this.width = width;
        this.height = height;
        super.onSurfaceChanged(holder, format, width, height);
    }

    private void draw() {
        SurfaceHolder holder = getSurfaceHolder();
        Canvas canvas = null;
        try {
            canvas = holder.lockCanvas();
            if (canvas != null) {
                draw(canvas);
            }
        } finally {
            if (canvas != null)
                holder.unlockCanvasAndPost(canvas);
        }

        handler.removeCallbacks(drawRunner);

        if (visible) {
            handler.postDelayed(drawRunner, 200);
        }
    }

    private void draw(Canvas canvas) {
        canvas.drawColor(bgColor);
        Rect dstRect = new Rect();
        Bitmap b= BitmapFactory.decodeResource(getResources(),R.drawable.wallpaper);
        canvas.getClipBounds(dstRect);
        canvas.drawBitmap(b, null, dstRect, null);
        clock.config(width / 2, height / 2, (int) (width * 0.6f),
                new Date(), paint, colors, displayHandSec);
        clock.draw(canvas);
    }

    @Override
    public void onSharedPreferenceChanged(
            SharedPreferences sharedPreferences, String key) {
        if (SettingsActivity.DISPLAY_HAND_SEC_KEY.equals(key)) {
            displayHandSec = sharedPreferences.getBoolean(
                    SettingsActivity.DISPLAY_HAND_SEC_KEY, true);
        }
    }

}

}

模拟时钟Java

在墙纸服务中称为此类,用于在墙纸上方绘制事物

public class AnalogClock extends View {

/** center X. */
private float x;
/** center Y. */
private float y;
private int radius;
private Calendar cal;
private Paint paint;
private Bitmap minDial = BitmapFactory.decodeResource(getResources(),
        R.drawable.min_frame);
private Bitmap hourDial = BitmapFactory.decodeResource(getResources(),
        R.drawable.hour_frame);
private int sizeScaled = -1;
private Bitmap clockDialScaled;
/** Hands colors. */
private int[] colors;
private boolean displayHandSec;

public AnalogClock(Context context) {
    super(context);
    cal = Calendar.getInstance();
}

public void config(float x, float y, int size, Date date, Paint paint, int[] colors, boolean displayHandSec) {
    this.x = x;
    this.y = y;
    this.paint = paint;
    this.colors = colors;
    this.displayHandSec = displayHandSec;

    cal.setTime(date);

    // scale bitmap if needed
    if (size != sizeScaled) {
        clockDialScaled = Bitmap.createScaledBitmap(minDial, size, size, false);
        radius = size / 2;
    }
}

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (paint != null) {
        // draw clock img
        canvas.drawBitmap(clockDialScaled, x - radius, y - radius, null);
    }
}

}

它的设置方式是每200milisec连续更换一次画布。也许这就是为什么我无法使用其他可运行时钟进行计时的原因,

0 个答案:

没有答案