android桌面动态时钟
时间: 2023-10-15 20:04:37 浏览: 190
您可以使用Android中的`SurfaceView`和`Canvas`来创建动态时钟。
首先,您需要创建一个`SurfaceView`并将其添加到您的布局中。然后,您可以使用`Canvas`在`SurfaceView`上绘制时钟的各个部分,例如时针、分针和秒针。
以下是一个简单的示例代码:
```java
public class ClockView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
private Thread thread;
private boolean isRunning;
private Paint paint;
private RectF rect;
public ClockView(Context context) {
super(context);
init();
}
public ClockView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ClockView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
getHolder().addCallback(this);
paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeCap(Paint.Cap.ROUND);
rect = new RectF();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
isRunning = true;
thread = new Thread(this);
thread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// Do nothing
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
isRunning = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (isRunning) {
if (!getHolder().getSurface().isValid()) {
continue;
}
Canvas canvas = getHolder().lockCanvas();
// Clear canvas
canvas.drawColor(Color.BLACK);
// Draw clock
drawClock(canvas);
getHolder().unlockCanvasAndPost(canvas);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void drawClock(Canvas canvas) {
int width = canvas.getWidth();
int height = canvas.getHeight();
int cx = width / 2;
int cy = height / 2;
// Draw hour hand
paint.setStrokeWidth(10);
int hours = Calendar.getInstance().get(Calendar.HOUR);
int minutes = Calendar.getInstance().get(Calendar.MINUTE);
float hourAngle = ((hours * 60) + minutes) / 2f;
float hourRadians = (float) Math.toRadians(hourAngle - 90);
float hourX = (float) (cx + (100 * Math.cos(hourRadians)));
float hourY = (float) (cy + (100 * Math.sin(hourRadians)));
canvas.drawLine(cx, cy, hourX, hourY, paint);
// Draw minute hand
paint.setStrokeWidth(6);
float minuteAngle = minutes * 6f;
float minuteRadians = (float) Math.toRadians(minuteAngle - 90);
float minuteX = (float) (cx + (150 * Math.cos(minuteRadians)));
float minuteY = (float) (cy + (150 * Math.sin(minuteRadians)));
canvas.drawLine(cx, cy, minuteX, minuteY, paint);
// Draw second hand
paint.setStrokeWidth(2);
paint.setColor(Color.RED);
float seconds = Calendar.getInstance().get(Calendar.SECOND);
float secondAngle = seconds * 6f;
float secondRadians = (float) Math.toRadians(secondAngle - 90);
float secondX = (float) (cx + (200 * Math.cos(secondRadians)));
float secondY = (float) (cy + (200 * Math.sin(secondRadians)));
canvas.drawLine(cx, cy, secondX, secondY, paint);
// Draw ticks
paint.setStrokeWidth(3);
paint.setColor(Color.WHITE);
for (int i = 0; i < 60; i++) {
float tickAngle = i * 6f;
float tickRadians = (float) Math.toRadians(tickAngle - 90);
float tickX1 = cx + (190 * (float) Math.cos(tickRadians));
float tickY1 = cy + (190 * (float) Math.sin(tickRadians));
float tickX2 = cx + (200 * (float) Math.cos(tickRadians));
float tickY2 = cy + (200 * (float) Math.sin(tickRadians));
canvas.drawLine(tickX1, tickY1, tickX2, tickY2, paint);
}
// Draw center circle
paint.setColor(Color.WHITE);
canvas.drawCircle(cx, cy, 10, paint);
}
}
```
在这个例子中,我们创建了一个名为`ClockView`的自定义`SurfaceView`,并实现了`SurfaceHolder.Callback`和`Runnable`接口。`SurfaceHolder.Callback`用于在`SurfaceView`被创建、改变和销毁时进行处理,而`Runnable`用于在后台线程中更新时钟。
`init()`方法用于初始化一些变量,包括`Paint`对象和一个`RectF`对象,后者用于绘制圆形。
`surfaceCreated()`方法用于启动后台线程,从而开始更新时钟。`surfaceChanged()`方法不执行任何操作,而`surfaceDestroyed()`方法用于停止后台线程。
`run()`方法是后台线程的主要逻辑。在其中,我们使用`lockCanvas()`方法获取一个`Canvas`对象,并在其中绘制时钟。最后,我们使用`unlockCanvasAndPost()`方法将`Canvas`对象提交给`SurfaceView`以进行显示。
`drawClock()`方法用于绘制时钟的各个部分,包括时针、分针、秒针、刻度和中心圆。
最后,在您的布局中添加`ClockView`即可显示动态时钟。
阅读全文