我想制定一个确定的进度条,但当前代码总是给出不确定的
我的XML
<ProgressBar
android:id="@+id/progressBar3"
style="?android:attr/progressBarStyle"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_weight="0.07"
android:indeterminate="false"
android:max="100"
android:progress="20"
/>
我的代码
holder.pb=rowView.findViewById(R.id.progressBar3);
holder.pb.setIndeterminate(false);
//holder.pb.setIndeterminateDrawable(null);
holder.pb.setProgress(50);
holder.pb.setMax(100);
holder.pb.requestLayout();
答案 0 :(得分:1)
这是我实现的循环进度视图。 (支持将其值绑定到TextView)
适用于Android 16+(或更低版本未经测试)
设置总计和进度。
重要事项此视图不会自行测量,因此您需要提供一些方形属性,例如width = 100dp height = 100dp
package app.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;
/**
* Created by MarcosVasconcelos on 28/07/2017.
*/
public class CircularProgressView extends View {
private final float strokeWidth;
private Paint totalPaint, progressPaint;
private float progress = 360;
private float total = 360;
public TextView textView;
public CircularProgressView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
strokeWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, context.getResources().getDisplayMetrics());
totalPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
totalPaint.setStyle(Paint.Style.STROKE);
totalPaint.setColor(Color.parseColor("#CCCCCC"));
totalPaint.setStrokeWidth(strokeWidth);
progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
progressPaint.setStyle(Paint.Style.STROKE);
progressPaint.setColor(Color.GREEN);
progressPaint.setStrokeWidth(strokeWidth);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
drawProgress(canvas, (int) 360f, totalPaint);
if(total != 0 && progress != 0)
drawProgress(canvas, total == progress ? 360 : (int) ((360f / total) * progress), progressPaint);
canvas.restore();
}
private void drawProgress(Canvas canvas, int total, Paint paint) {
canvas.drawArc(new RectF(strokeWidth, strokeWidth, getWidth() - strokeWidth, getHeight() - strokeWidth), -90, total, false, paint);
}
public void setProgress(int progress) {
this.progress = progress;
invalidate();
}
public void setProgressColor(int rgb) {
progressPaint.setColor(rgb);
invalidate();
}
public void setTotal(int total) {
this.total = total;
if(textView != null)
textView.setText(String.valueOf(total));
invalidate();
}
public void setTotalTextView(TextView view) {
this.textView = view;
textView.setText(String.valueOf((int)total));
}
}