Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

add support for specifying a static position for dot on CircularProgressIndicator #26

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class CircularProgressIndicator extends View {
private static final int DEFAULT_ANIMATION_DURATION = 1_000;

private static final String PROPERTY_ANGLE = "angle";
private static final int DEFAULT_DOT_ANGLE_POSITION = -1;


private Paint progressPaint;
Expand All @@ -76,6 +77,7 @@ public class CircularProgressIndicator extends View {

private int startAngle = DEFAULT_PROGRESS_START_ANGLE;
private int sweepAngle = 0;
private int dotAnglePosition = DEFAULT_DOT_ANGLE_POSITION;

private RectF circleBounds;

Expand Down Expand Up @@ -139,6 +141,7 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
int textSize = sp2px(DEFAULT_TEXT_SIZE_SP);

shouldDrawDot = true;

int dotColor = progressColor;
int dotWidth = progressStrokeWidth;

Expand All @@ -155,6 +158,7 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
textSize = a.getDimensionPixelSize(R.styleable.CircularProgressIndicator_textSize, textSize);

shouldDrawDot = a.getBoolean(R.styleable.CircularProgressIndicator_drawDot, true);
dotAnglePosition = a.getInt(R.styleable.CircularProgressIndicator_dotAnglePosition, DEFAULT_DOT_ANGLE_POSITION);
dotColor = a.getColor(R.styleable.CircularProgressIndicator_dotColor, progressColor);
dotWidth = a.getDimensionPixelSize(R.styleable.CircularProgressIndicator_dotWidth, progressStrokeWidth);

Expand Down Expand Up @@ -348,9 +352,18 @@ private void drawProgress(Canvas canvas) {
}

private void drawDot(Canvas canvas) {
double angleRadians = Math.toRadians(startAngle + sweepAngle + 180);

double angleRadians;

if(startAngle != DEFAULT_DOT_ANGLE_POSITION){
angleRadians = Math.toRadians(dotAnglePosition + 180);
}else{
angleRadians = Math.toRadians(startAngle + sweepAngle + 180);
}

float cos = (float) Math.cos(angleRadians);
float sin = (float) Math.sin(angleRadians);

float x = circleBounds.centerX() - radius * cos;
float y = circleBounds.centerY() - radius * sin;

Expand Down Expand Up @@ -639,6 +652,11 @@ public void setStartAngle(@IntRange(from = 0, to = 360) int startAngle) {
invalidate();
}

public void setDotAnglePosition(@IntRange(from = 0, to = 360) int dotAnglePosition){
this.dotAnglePosition = dotAnglePosition;
invalidate();
}

@Direction
public int getDirection() {
return direction;
Expand Down
1 change: 1 addition & 0 deletions circularprogressindicator/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<attr name="dotWidth" format="dimension" />

<attr name="startAngle" format="integer|reference" />
<attr name="dotAnglePosition" format="integer|reference"/>

<attr name="direction" format="enum">
<enum name="clockwise" value="0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe

private Button dotColor;
private SeekBar dotWidth;
private SeekBar dotPositionSeekBar;

private CircularProgressIndicator circularProgress;

Expand Down Expand Up @@ -53,6 +54,10 @@ protected void onCreate(Bundle savedInstanceState) {
SeekBar textSize = findViewById(R.id.sb_text_size);
dotWidth = findViewById(R.id.sb_dot_width);

dotPositionSeekBar = findViewById(R.id.sb_dot_position);
dotPositionSeekBar.setOnSeekBarChangeListener(this);
dotPositionSeekBar.setEnabled(false);//default to disabled since we cannot disable via xml

progress.setOnSeekBarChangeListener(this);
progressStrokeWidth.setOnSeekBarChangeListener(this);
progressBackgroundStrokeWidth.setOnSeekBarChangeListener(this);
Expand Down Expand Up @@ -83,6 +88,14 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});

final CheckBox dotPosition = findViewById(R.id.cb_dot_position);
dotPosition.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
dotPositionSeekBar.setEnabled(isChecked);
}
});

RadioGroup progressCap = findViewById(R.id.rg_cap);
progressCap.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
Expand Down Expand Up @@ -199,6 +212,9 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
case R.id.sb_progress_background_width:
circularProgress.setProgressBackgroundStrokeWidthDp(progress);
break;
case R.id.sb_dot_position:
circularProgress.setDotAnglePosition(progress);
break;
}
}

Expand Down
21 changes: 21 additions & 0 deletions example/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@
android:layout_marginLeft="8dp"
android:text="Fill background" />

<CheckBox
android:id="@+id/cb_dot_position"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text="Static dot position"/>


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -126,6 +134,19 @@
android:min="0"
android:progress="8" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Progress dot position angle"/>

<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="360"
android:min="0"
android:layout_marginTop="8dp"
android:id="@+id/sb_dot_position"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down