我想一个接一个地对我的progressBar应用两个操作。此progressBar显示在自定义对话框中。
首先我有一个正在进行的状态,其中当前时间显示在我的progressBar上的editText中,然后我想管理第二个状态,其中我的进度条已满,但是超过的时间是通过此editText显示在我的进度条。
这是我的代码:
// Define custom progress bar style
final float[] roundedCorners = new float[]{27, 27, 27, 27, 27, 27, 27, 27};
pgDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null));
pgDrawable.getPaint().setColor(Color.parseColor("#56d2c2"));
progress = new ClipDrawable(pgDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
mProgress.setProgressDrawable(progress);
// Start lengthy operation in a background thread
new Thread(new Runnable() {
public void run() {
mProgressFull = defineValueFull();
// Operation in progress: display time flow
while (mProgressStatus < 100) {
mProgressStatus = doProgress();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
mProgress.setProgress(mProgressStatus);
mCurrentTimeProgressBar.setText(String.valueOf(mCurrentTime));
mMaxTimeProgressBar.setText(String.valueOf(mProgressFull) + " mins");
}
});
}
if (mProgressStatus >= 101) {
pgDrawable.getPaint().setColor(Color.parseColor("#ff0000"));// set progress bar in red
// Operation ended: display time in excess
while (mProgressStatus >= 101) {
mProgressStatus = doProgress();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
mProgress.setProgress(mProgressStatus);
int timeExcess = mCurrentTime - mProgressFull;
mCurrentTimeExcessProgressBar.setVisibility(View.VISIBLE);
mCurrentTimeExcessProgressBar.setText("(+ " + Helpers.minutesRefactor(timeExcess) + ")");
mMaxTimeProgressBar.setText(String.valueOf(mProgressFull) + " mins");
mCurrentTimeProgressBar.setText(String.valueOf(mProgressFull + timeExcess));
}
});
}
}
}
}).start();
我的问题是我无法在UI上管理这两种状态我的意思是当我的progressBar已满并且我想要显示红色progressBar时超出时间,我必须关闭我的自定义对话框我的进度bar已设置并打开新时间以更新progressBar。
我如何避免这种情况,并保持我的progressBar不断更新,并能够在我尝试按代码定义时更改其状态。我也想知道在这种情况下最佳做法是什么,我可以为这两种状态使用两种不同的线程吗?我已经尝试了但是我没有设法在这个之后启动这些wo线程并且能够在不关闭对话框的情况下更新我的两个状态之间的progressBar。
答案 0 :(得分:1)
我将从我的项目中粘贴代码,这与此非常相似。并且还注意到我没有使用线程。使用Handle就足够了。希望这段代码会有所帮助。
public abstract class ProgressView extends RelativeLayout {
protected View rootLayout;
protected TextView titleTextView;
protected SeekBar seekBar;
protected TextView secondTextView;
protected View secondLayout;
protected int initialTime;
protected int maxTime;
protected long timerStartTime;
protected int currentTime;
public ProgressView(Context context) {
super(context);
init();
}
public ProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ProgressView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
inflate(getContext(), R.layout.progress_view, this);
rootLayout = findViewById(R.id.rootLayout);
titleTextView = (TextView) findViewById(R.id.titleTextView);
seekBar = (SeekBar) findViewById(R.id.seekBar);
secondTextView = (TextView) findViewById(R.id.secondTextView);
secondLayout = findViewById(R.id.secondLayout);
updateTime();
}
public void startTimer(int current, int max) {
if(getVisibility() != VISIBLE) {
this.initialTime = current;
this.maxTime = max;
this.timerStartTime = System.currentTimeMillis();
setVisibility(VISIBLE);
updateTime();
post(new Runnable() {
@Override
public void run() {
updateTime();
if(currentTime > 0) {
post(this);
}
}
});
}
}
protected void updateTime() {
currentTime = (int) (initialTime - (System.currentTimeMillis() - timerStartTime));
secondTextView.setText(Integer.toString(Math.round(currentTime / 1000.0f)));
seekBar.setProgress((int) (10000.0f * currentTime / maxTime));
if(currentTime <= 0 || currentTime > maxTime) {
titleTextView.setText("No More Bets");
secondLayout.setVisibility(GONE);
seekBar.setVisibility(GONE);
rootLayout.setBackgroundResource(R.drawable.bet_progress_no_more_bets);
}
else {
titleTextView.setText("Please, Place Your Bets");
secondLayout.setVisibility(VISIBLE);
seekBar.setVisibility(VISIBLE);
rootLayout.setBackgroundResource(R.drawable.bet_progress_18_sec);
}
}
}
progress_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/titleTextView"
android:text="Please, Place Your Bets"
android:textColor="#ffffff"
android:textSize="25sp"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBar"
android:layout_below="@id/titleTextView"
android:progressDrawable="@drawable/seek_bar"
android:padding="0dp"
android:thumb="@null"
android:max="10000"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/secondLayout"
android:orientation="horizontal"
android:layout_above="@id/seekBar"
android:layout_marginLeft="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/secondTextView"
android:text="0"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sec"
android:textColor="#ffffff"
android:textSize="18sp"
android:layout_marginLeft="5dp"/>
</LinearLayout>