java bar_java-顺利进行ProgressBar更新

这是一个自成体系的解决方案:

import android.animation.ValueAnimator;

import android.animation.ValueAnimator.AnimatorUpdateListener;

import android.content.Context;

import android.util.AttributeSet;

import android.view.animation.AccelerateDecelerateInterpolator;

import android.view.animation.Interpolator;

import android.widget.ProgressBar;

public class AnimatingProgressBar extends ProgressBar {

private static final Interpolator DEFAULT_INTERPOLATER = new AccelerateDecelerateInterpolator();

private ValueAnimator animator;

private ValueAnimator animatorSecondary;

private boolean animate = true;

public AnimatingProgressBar(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

}

public AnimatingProgressBar(Context context, AttributeSet attrs) {

super(context, attrs);

}

public AnimatingProgressBar(Context context) {

super(context);

}

public boolean isAnimate() {

return animate;

}

public void setAnimate(boolean animate) {

this.animate = animate;

}

@Override

public synchronized void setProgress(int progress) {

if (!animate) {

super.setProgress(progress);

return;

}

if (animator != null)

animator.cancel();

if (animator == null) {

animator = ValueAnimator.ofInt(getProgress(), progress);

animator.setInterpolator(DEFAULT_INTERPOLATER);

animator.addUpdateListener(new AnimatorUpdateListener() {

@Override

public void onAnimationUpdate(ValueAnimator animation) {

AnimatingProgressBar.super.setProgress((Integer) animation.getAnimatedValue());

}

});

} else

animator.setIntValues(getProgress(), progress);

animator.start();

}

@Override

public synchronized void setSecondaryProgress(int secondaryProgress) {

if (!animate) {

super.setSecondaryProgress(secondaryProgress);

return;

}

if (animatorSecondary != null)

animatorSecondary.cancel();

if (animatorSecondary == null) {

animatorSecondary = ValueAnimator.ofInt(getProgress(), secondaryProgress);

animatorSecondary.setInterpolator(DEFAULT_INTERPOLATER);

animatorSecondary.addUpdateListener(new AnimatorUpdateListener() {

@Override

public void onAnimationUpdate(ValueAnimator animation) {

AnimatingProgressBar.super.setSecondaryProgress((Integer) animation

.getAnimatedValue());

}

});

} else

animatorSecondary.setIntValues(getProgress(), secondaryProgress);

animatorSecondary.start();

}

@Override

protected void onDetachedFromWindow() {

super.onDetachedFromWindow();

if (animator != null)

animator.cancel();

if (animatorSecondary != null)

animatorSecondary.cancel();

}

}

在您的布局中将setAnimate()替换为AnimatingProgressBar

您还可以将类型更改为AnimatingProgressBar以利用setAnimate()禁用动画(在恢复活动状态时可能很有用)

以下是一个简单的Java ProgressBar示例: ``` import javax.swing.*; import java.awt.*; public class ProgressBarExample extends JFrame { private JProgressBar progressBar; private JButton startButton; private JLabel statusLabel; public ProgressBarExample() { setTitle("Progress Bar Example"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); progressBar = new JProgressBar(0, 100); progressBar.setValue(0); progressBar.setStringPainted(true); startButton = new JButton("Start"); startButton.addActionListener(e -> new ProgressWorker().execute()); statusLabel = new JLabel("Status: Not started"); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(3, 1)); panel.add(progressBar); panel.add(startButton); panel.add(statusLabel); getContentPane().add(panel, BorderLayout.CENTER); pack(); setLocationRelativeTo(null); } private class ProgressWorker extends SwingWorker<Void, Integer> { @Override protected Void doInBackground() throws Exception { for (int i = 0; i <= 100; i++) { Thread.sleep(50); publish(i); } return null; } @Override protected void process(java.util.List<Integer> chunks) { int value = chunks.get(chunks.size() - 1); progressBar.setValue(value); statusLabel.setText("Status: " + value + "%"); } @Override protected void done() { startButton.setEnabled(true); statusLabel.setText("Status: Done"); } } public static void main(String[] args) { new ProgressBarExample().setVisible(true); } } ``` 在这个示例,我们创建了一个包含进度条、开始按钮和状态标签的窗口。当用户点击开始按钮时,我们启动一个后台线程来模拟进度,并使用SwingWorker类来更新进度条和状态标签。当进度完成时,我们使开始按钮可用,并将状态标签设置为“完成”。 你可以根据需要自定义进度条的最小值、最大值和步长,并在进度条上显示百分比或任何其他文本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值