サークルインジケーターがすべて設定された間隔(同じ距離)にあると仮定すると、次のことが最も論理的な解決策のように思われます
サークル用のスペースがあることを確認するために、ビューのパディングを調整する必要がある場合があります。以下は、必要なものの簡単なアイデアです。
public class TickedProgressBarView extends ProgressBar {
private static final float DEFAULT_INTERVAL = 25f;
private float INDICATOR_RADIUS;
private Paint mTickPaint;
private float mInterval; //%
public TickedProgressBarView(Context context) {
super(context);
initPainters(context, null); //because draw is called a lot of times, don't want to do loads of allocations in onDraw
}
public TickedProgressBarView(Context context, AttributeSet attrs) {
super(context, attrs);
initPainters(context, attrs);
}
public TickedProgressBarView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initPainters(context, attrs);
}
private void initPainters(Context context, @Nullable AttributeSet attrs) {
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TickedProgressBarView, 0, 0);
mInterval = a.getFloat(R.styleable.TickedProgressBarView_tickInterval, DEFAULT_INTERVAL);
} else {
mInterval = DEFAULT_INTERVAL;
}
//5 on the line below is HALF how many Dp wide you want the circles - ie a 10 Dp circle results from this
INDICATOR_RADIUS = 5 * getResources().getDisplayMetrics().density + 0.5f;
mTickPaint = new Paint();
mTickPaint.setColor(ContextCompat.getColor(getContext(), R.color.my_color));
mTickPaint.setStyle(Paint.Style.FILL);
mTickPaint.setStrokeCap(Paint.Cap.ROUND);
}
public void setTickInterval(float intervalPercentage) {
mInterval = intervalPercentage;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mInterval > 0f) {
final float midHeight = canvas.getHeight() / 2f;
final int end = canvas.getWidth();
final int intervalPx = (int) ((end / 100f) * mInterval);
int nextInterval = intervalPx;
while (nextInterval <= end) {
canvas.drawCircle(nextInterval, midHeight, INDICATOR_RADIUS, mTickPaint);
nextInterval += intervalPx;
}
}
}
}
attrs.xml
<declare-styleable name="TickedProgressBarView">
<attr name="tickInterval" format="float" />
</declare-styleable>
attr宣言を使用すると、xmlからビューを使用できます
<!-- draw a circle every 10% along the bar -->
<com.my.packge.TickedProgressBarView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tickInterval="10.0"
/>
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加