To implement the 24-hour effective time period distribution map on Android and be accurate to the second, you can use TimePickerDialog
and SeekBar
to get the start time and duration selected by the user. Then, use Canvas to draw graphs to represent the distribution of time periods.
Here is sample code to implement this functionality:
activity_main.xml
Add aButton
and a to your layout file (for example )TextView
to display the time period selected by the user:
<Button
android:id="@+id/btn_select_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Time" />
<TextView
android:id="@+id/tv_selected_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Selected Time: "
android:textSize="18sp" />
- In the Java code of the Activity, add the following code to realize the time selection and the drawing of the time period distribution map:
import android.app.TimePickerDialog;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private Button btnSelectTime;
private TextView tvSelectedTime;
private int startHour, startMinute, duration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSelectTime = findViewById(R.id.btn_select_time);
tvSelectedTime = findViewById(R.id.tv_selected_time);
btnSelectTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showTimePickerDialog();
}
});
}
private void showTimePickerDialog() {
Calendar currentTime = Calendar.getInstance();
int hour = currentTime.get(Calendar.HOUR_OF_DAY);
int minute = currentTime.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
startHour = hourOfDay;
startMinute = minute;
showSeekBarDialog();
}
}, hour, minute, true);
timePickerDialog.show();
}
private void showSeekBarDialog() {
final int MAX_DURATION = 86400; // 24 hours in seconds
SeekBarDialog seekBarDialog = new SeekBarDialog(this);
seekBarDialog.setMax(MAX_DURATION);
seekBarDialog.setSeekBarChangeListener(new SeekBarDialog.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(int progress) {
duration = progress;
drawTimeDistribution();
}
});
seekBarDialog.show();
}
private void drawTimeDistribution() {
// 获取绘制时间段分布图的Canvas
// 可以是一个ImageView或其他支持绘图的View
// 这里假设使用一个名为"canvasView"的自定义View
Canvas canvas = canvasView.getCanvas();
// 清空画布
canvas.drawColor(Color.WHITE);
// 绘制时间段分布图
int startX = 0;
int startY = 0;
int endX = canvas.getWidth();
int endY = canvas.getHeight();
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.FILL);
// 计算起始时间的秒数
int startSeconds = startHour * 3600 + startMinute * 60;
// 计算持续时间的秒数
int durationSeconds = duration;
// 计算终止时间的秒数
int endSeconds = startSeconds + durationSeconds;
// 绘制时间段矩形
canvas.drawRect(startX, startY, endX, endY, paint);
// 更新显示选择的时间段
int endHour = endSeconds / 3600;
int endMinute = (endSeconds % 3600) / 60;
String selectedTime = String.format("%02d:%02d - %02d:%02d",
startHour, startMinute, endHour, endMinute);
tvSelectedTime.setText("Selected Time: " + selectedTime);
}
}
- Create a
SeekBarDialog
custom dialog class called , which displays aSeekBar
to let the user choose a duration:
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SeekBar;
public class SeekBarDialog extends Dialog {
private SeekBar seekBar;
private OnSeekBarChangeListener seekBarChangeListener;
public SeekBarDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
seekBar = new SeekBar(getContext());
seekBar.setLayoutParams(layoutParams);
seekBar.setMax(86400); // 24 hours in seconds
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (seekBarChangeListener != null) {
seekBarChangeListener.onProgressChanged(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
LinearLayout layout = new LinearLayout(getContext());
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(seekBar);
setContentView(layout);
}
public interface OnSeekBarChangeListener {
void onProgressChanged(int progress);
}
public void setSeekBarChangeListener(OnSeekBarChangeListener listener) {
this.seekBarChangeListener = listener;
}
}
Note: The above code only implements the drawing and display of the time period distribution graph. If you need to save the graph to a file or perform other operations, you need to expand it according to actual needs.