我创建了带有进度条的CountDown Timer,该进度条应显示进度已过去了多少时间,例如10到0(秒)(达到2.5秒的进度条应完成25%,5秒的进度条应完成完成50%,应该每秒更新一次),但是进度栏无法正常工作
activity_timer.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TimerActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/TimerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_dark"
android:orientation="vertical"
android:padding="16dp"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
<TextView
android:id="@+id/TimerTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:background="@android:color/background_dark"
android:gravity="center_horizontal"
android:text="@string/_00_00"
android:textColor="@android:color/white"
android:textSize="70sp"
tools:ignore="MissingConstraints" />
<ProgressBar
android:id="@+id/circular_progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_below="@id/TimerTV"
android:layout_centerInParent="true"
android:indeterminate="false"
android:progressDrawable="@drawable/circular"
android:visibility="visible" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:background="@android:color/white"
android:gravity="bottom"
android:orientation="horizontal"
android:paddingTop="30dp"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="348dp">
<Button
android:id="@+id/StartPauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_gravity="left"
android:layout_marginLeft="56dp"
android:layout_marginStart="56dp"
android:layout_marginBottom="60dp"
android:background="@android:color/background_dark"
android:text="@string/start"
android:textColor="@android:color/white"
android:textSize="18sp"
android:visibility="visible"
tools:ignore="MissingConstraints,NotSibling" />
<Button
android:id="@+id/ResetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_gravity="right"
android:layout_marginEnd="56dp"
android:layout_marginRight="56dp"
android:background="@android:color/background_dark"
android:text="@string/reset"
android:textColor="@android:color/white"
android:visibility="invisible"
tools:ignore="MissingConstraints"
tools:visibility="invisible" />
</RelativeLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
circular.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:useLevel="true"
android:innerRadiusRatio="2.3"
android:shape="ring"
android:thickness="3.8sp"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<solid android:color="@color/colorPrimary" />
</shape>
TimerActivity.java
package killer.timer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.Locale;
public class TimerActivity extends AppCompatActivity {
private static final long START_TIME_IN_MILLIS = 20000; //20sec
// TODO Accept Input from user & store it in START_TIME_IN_MILLIS
int progress;
private TextView CountDown_Tv;
private Button StartPauseButton;
private Button ResetButton;
private CountDownTimer MyCountDownTimer;
private boolean TimerRunning;
// Initially TimeLeftInMillis will be same as START_TIME_IN_MILLIS
private long TimeLeftInMillis = START_TIME_IN_MILLIS;
ProgressBar MyProgressBar;
private int ProgressBarStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
CountDown_Tv = findViewById(R.id.TimerTV);
StartPauseButton = findViewById(R.id.StartPauseButton);
ResetButton = findViewById(R.id.ResetButton);
MyProgressBar = findViewById(R.id.circular_progress_bar);
StartPauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (TimerRunning) {
pauseTimer();
} else {
startTimer();
}
}
});
ResetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
resetTimer();
}
});
updateCountDownText();
MyProgressBar.setVisibility(View.VISIBLE);
MyProgressBar.setProgress(0);
MyProgressBar.setMax(100);
}
private void startTimer() {
MyCountDownTimer = new CountDownTimer(START_TIME_IN_MILLIS, 1000) {
@Override
public void onTick(long millisUntilFinished) {
TimeLeftInMillis = millisUntilFinished;
updateCountDownText(); // Updating CountDown_Tv
/*for incrementing progressbar every second calculating progress for every second*/
progress = (int) (START_TIME_IN_MILLIS / (1 * 100));
//incrementing progress on every tick
ProgressBarStatus +=progress;
MyProgressBar.setProgress(ProgressBarStatus);
}
@Override
public void onFinish() {
TimerRunning = false;
CountDown_Tv.setText("00:00");
MyProgressBar.setProgress(100);
StartPauseButton.setText("Start");
StartPauseButton.setVisibility(View.INVISIBLE);
ResetButton.setVisibility(View.VISIBLE);
}
}.start();
TimerRunning = true;
StartPauseButton.setText("Pause");
ResetButton.setVisibility(View.INVISIBLE);
}
private void pauseTimer() {
MyCountDownTimer.cancel();
TimerRunning = false;
StartPauseButton.setText("Resume");
ResetButton.setVisibility(View.VISIBLE);
MyProgressBar.clearAnimation();
}
private void resetTimer() {
TimeLeftInMillis = START_TIME_IN_MILLIS;
updateCountDownText();
ResetButton.setVisibility(View.INVISIBLE);
StartPauseButton.setVisibility(View.VISIBLE);
MyProgressBar.setProgress(0);
StartPauseButton.setText("Start");
}
private void updateCountDownText() {
int minutes = (int) (TimeLeftInMillis / 1000) / 60;
int seconds = (int) (TimeLeftInMillis / 1000) % 60;
String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
// String newtime = hours + ":" + minutes + ":" + seconds;
CountDown_Tv.setText(timeLeftFormatted);
}
}
答案 0 :(得分:0)
您需要根据要倒计时的总秒数计算百分比系数。例如您的情况100/START_TIME_IN_MILLIS/1000 = 5
;
毕竟需要将每个刻度数乘以这个。
这是修改后的代码。
int numberOfSeconds = START_TIME_IN_MILLIS/1000; // Ex : 20000/1000 = 20
int factor = 100/numberOfSeconds; // 100/20 = 5, for each second multiply this, for sec 1 progressPercentage = 1x5 =5, for sec 5 progressPercentage = 5x5 = 25, for sec 20 progressPercentage = 20x5 =100
@Override
public void onTick(long millisUntilFinished) {
TimeLeftInMillis = millisUntilFinished;
updateCountDownText(); // Updating CountDown_Tv
int secondsRemaining = (int) (millisUntilFinished / 1000);
int progressPercentage = (numberOfSeconds-secondsRemaining) * factor ;
MyProgressBar.setProgress(progressPercentage);
}
答案 1 :(得分:-1)
您的计算应该是这样的:
import numpy as np
import pandas as pd
from scipy import stats
df=pd.read_excel('I:/Python/Data/Copy.xlsx')
grouped = df.groupby('Test Event')
for test_event, g in grouped:
print('Test Event: {}'.format(test_event))
df_np=np.array(g)
x=np.array(df_np[:,1],dtype=float)
y=np.array(df_np[:,2],dtype=float)
for i, pair in enumerate(zip(x, y)):
slope, intercept, r_value, p_value, std_err = stats.linregress(np.delete(x,i),np.delete(y,i))
print('slope', slope, 'for data without pair', i, ':', pair)