Android实现随时间变化的钟表的布局和代码示例,不使用AnalogClock
时间: 2024-02-18 21:03:56 浏览: 176
Android时间对话框使用方法详解
如果你不想使用Android SDK自带的AnalogClock,你可以自定义一个View来实现随时间变化的钟表。下面是一个简单的示例:
布局:
```xml
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/clock_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/time_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="24sp" />
</FrameLayout>
```
代码:
```java
public class ClockView extends View {
private Paint mPaint;
private RectF mRectF;
private float mHourHandLength;
private float mMinuteHandLength;
private float mSecondHandLength;
private int mHourHandColor;
private int mMinuteHandColor;
private int mSecondHandColor;
private int mHour;
private int mMinute;
private int mSecond;
public ClockView(Context context) {
this(context, null);
}
public ClockView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ClockView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// 初始化画笔
mPaint = new Paint();
mPaint.setAntiAlias(true);
// 初始化时钟指针长度
mHourHandLength = dp2px(context, 50);
mMinuteHandLength = dp2px(context, 70);
mSecondHandLength = dp2px(context, 90);
// 初始化时钟指针颜色
mHourHandColor = Color.BLACK;
mMinuteHandColor = Color.BLACK;
mSecondHandColor = Color.RED;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 获取时钟中心坐标
float centerX = getWidth() / 2f;
float centerY = getHeight() / 2f;
// 计算时钟半径
float radius = Math.min(centerX, centerY);
// 绘制时钟圆盘
mPaint.setColor(Color.WHITE);
canvas.drawCircle(centerX, centerY, radius, mPaint);
// 绘制时钟刻度
mPaint.setColor(Color.BLACK);
mPaint.setStrokeWidth(dp2px(getContext(), 2));
for (int i = 0; i < 12; i++) {
float angle = i * 30f;
float startX = centerX + (radius - dp2px(getContext(), 10)) * (float) Math.sin(Math.toRadians(angle));
float startY = centerY - (radius - dp2px(getContext(), 10)) * (float) Math.cos(Math.toRadians(angle));
float stopX = centerX + radius * (float) Math.sin(Math.toRadians(angle));
float stopY = centerY - radius * (float) Math.cos(Math.toRadians(angle));
canvas.drawLine(startX, startY, stopX, stopY, mPaint);
}
// 绘制时针
mPaint.setColor(mHourHandColor);
mPaint.setStrokeWidth(dp2px(getContext(), 5));
float hourAngle = (mHour % 12 + mMinute / 60f) * 30f;
float hourStartX = centerX;
float hourStartY = centerY;
float hourStopX = centerX + mHourHandLength * (float) Math.sin(Math.toRadians(hourAngle));
float hourStopY = centerY - mHourHandLength * (float) Math.cos(Math.toRadians(hourAngle));
canvas.drawLine(hourStartX, hourStartY, hourStopX, hourStopY, mPaint);
// 绘制分针
mPaint.setColor(mMinuteHandColor);
mPaint.setStrokeWidth(dp2px(getContext(), 3));
float minuteAngle = mMinute * 6f;
float minuteStartX = centerX;
float minuteStartY = centerY;
float minuteStopX = centerX + mMinuteHandLength * (float) Math.sin(Math.toRadians(minuteAngle));
float minuteStopY = centerY - mMinuteHandLength * (float) Math.cos(Math.toRadians(minuteAngle));
canvas.drawLine(minuteStartX, minuteStartY, minuteStopX, minuteStopY, mPaint);
// 绘制秒针
mPaint.setColor(mSecondHandColor);
mPaint.setStrokeWidth(dp2px(getContext(), 1));
float secondAngle = mSecond * 6f;
float secondStartX = centerX;
float secondStartY = centerY;
float secondStopX = centerX + mSecondHandLength * (float) Math.sin(Math.toRadians(secondAngle));
float secondStopY = centerY - mSecondHandLength * (float) Math.cos(Math.toRadians(secondAngle));
canvas.drawLine(secondStartX, secondStartY, secondStopX, secondStopY, mPaint);
}
public void setTime(int hour, int minute, int second) {
mHour = hour;
mMinute = minute;
mSecond = second;
invalidate();
}
private float dp2px(Context context, float dp) {
float scale = context.getResources().getDisplayMetrics().density;
return dp * scale + 0.5f;
}
}
```
在Activity或Fragment中,你可以使用以下代码来更新ClockView的时间:
```java
ClockView clockView = findViewById(R.id.clock_view);
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
clockView.setTime(hour, minute, second);
```
这样就可以实现一个简单的随时间变化的钟表了。你可以根据需要自定义ClockView的各种属性,例如时钟指针的长度和颜色等。
阅读全文