大家好,我面临进度条进度问题,我正在尝试更新进度条上的进度,但由于某种未知原因,它始终显示 100% 或更高的值。
以下是相关的 XML 和 Java 代码片段:
<!-- Other Code -->
<ProgressBar
android:id="@+id/overall_score_progress_bar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_gravity="bottom"
android:max="100"
android:layout_marginBottom="35dp"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp" />
public class FragmentResults extends Fragment {
ProgressBar overallScoreProgressBar;
int overall;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_results, container, false);
// Initialize ProgressBars
overallScoreProgressBar = view.findViewById(R.id.overall_score_progress_bar);
// Generate random scores
generateRandomScores();
// Set progress bar colors based on scores
setProgressBarColor(overallScoreProgressBar, overall);
return view;
}
// Method to calculate color based on progress
private void setProgressBarColor(ProgressBar progressBar, int progress) {
int color = getColorFromProgress(progress);
progressBar.getProgressDrawable().setColorFilter(color, android.graphics.PorterDuff.Mode.SRC_IN);
}
// Method to determine the color based on the progress percentage
private int getColorFromProgress(int progress) {
if (progress <= 20) {
return interpolateColor(Color.parseColor("#CC0000"), Color.parseColor("#FF6666"), progress / 20.0f);
} else if (progress <= 40) {
return interpolateColor(Color.parseColor("#E68A00"), Color.parseColor("#FFB266"), (progress - 20) / 20.0f);
} else if (progress <= 60) {
return interpolateColor(Color.parseColor("#CCCC00"), Color.parseColor("#FFFF66"), (progress - 40) / 20.0f);
} else if (progress <= 80) {
return interpolateColor(Color.parseColor("#009900"), Color.parseColor("#66FF66"), (progress - 60) / 20.0f);
} else {
return interpolateColor(Color.parseColor("#33CC33"), Color.parseColor("#99FF99"), (progress - 80) / 20.0f);
}
}
// Helper method to interpolate between two colors
private int interpolateColor(int colorStart, int colorEnd, float ratio) {
int rStart = (colorStart >> 16) & 0xFF;
int gStart = (colorStart >> 8) & 0xFF;
int bStart = colorStart & 0xFF;
int rEnd = (colorEnd >> 16) & 0xFF;
int gEnd = (colorEnd >> 8) & 0xFF;
int bEnd = colorEnd & 0xFF;
int r = (int) (rStart + (rEnd - rStart) * ratio);
int g = (int) (gStart + (gEnd - gStart) * ratio);
int b = (int) (bStart + (bEnd - bStart) * ratio);
return Color.rgb(r, g, b);
}
private void generateRandomScores() {
Random random = new Random();
// Generate random scores between 0 and 100
overall = random.nextInt(101);
// Other score generations
// Set scores to TextViews and progress bars
setScoresAndProgress();
}
private void setScoresAndProgress() {
// Set the scores to TextViews
overallScore.setText(String.valueOf(overall));
// Set the progress bars
overallScoreProgressBar.setProgress(overall);
}
}
我将非常感谢任何解决此问题的解决方案!
函数
getProgressDrawable().setColorFilter()
设置进度条的颜色而不是进度。
这是一种仅更改进度量颜色的方法。
val progressBar = findViewById<ProgressBar>(R.id.progressBar)
// Retrieve the LayerDrawable from the progress bar
val progressDrawable = progressBar.progressDrawable as LayerDrawable
然后使用这个
progressDrawable
并改变它的颜色。
private void setProgressBarColor(ProgressBar progressBar, int progress) {
int color = getColorFromProgress(progress);
val progressLayer = progressDrawable.findDrawableByLayerId(android.R.id.progress)
// Set the desired color for the progress (not affecting the background)
val color = ContextCompat.getColor(this, R.color.your_color)
progressLayer.colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN)
}