Skip to content

Commit

Permalink
Added attrManager to handle xml styledAttrs.
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosmuvi committed Jun 23, 2017
1 parent f5a2296 commit a7f86ec
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 46 deletions.
3 changes: 0 additions & 3 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 24
compileSdkVersion 25
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.carlosmuvi.segmentedprogressbar"
minSdkVersion 16
targetSdkVersion 24
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand Down
8 changes: 1 addition & 7 deletions app/src/main/java/com/carlosmuvi/sample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.carlosmuvi.sample;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
Expand Down Expand Up @@ -37,13 +36,8 @@ public class MainActivity extends AppCompatActivity {

private void initSegmentedProgressBar() {
segmentedProgressBar = (SegmentedProgressBar) findViewById(R.id.segmented_progressbar);
segmentedProgressBar.setSegmentCount(7); // number of segments in your bar

//customize colors.
segmentedProgressBar.setContainerColor(Color.BLUE); //empty segment color
segmentedProgressBar.setFillColor(Color.GREEN); //empty segment color

//set filled segments directly
segmentedProgressBar.setCompletedSegments(3);
segmentedProgressBar.setCompletedSegments(1);
}
}
30 changes: 23 additions & 7 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
Expand All @@ -9,23 +10,38 @@
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.carlosmuvi.sample.MainActivity">
tools:context="com.carlosmuvi.sample.MainActivity"
>

<com.carlosmuvi.segmentedprogressbar.SegmentedProgressBar
android:id="@+id/segmented_progressbar"
android:layout_width="match_parent"
android:layout_height="5dp"/>
android:layout_height="5dp"
app:container_color="@color/colorAccent"
app:fill_color="@color/colorPrimary"
app:gap_size="@dimen/progressbar_gap"
app:segment_count="3"
/>

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" android:layout_alignParentStart="true" android:layout_below="@+id/button"
android:text="Pause"/>
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/button"
android:text="Pause"
/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" android:layout_alignParentStart="true"
android:layout_below="@+id/segmented_progressbar" android:text="Start next segment"/>
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/segmented_progressbar"
android:text="Start next segment"
/>

</RelativeLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="progressbar_gap">2dp</dimen>
</resources>
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 24
compileSdkVersion 25
buildToolsVersion "26.0.0"

defaultConfig {
minSdkVersion 16
targetSdkVersion 24
targetSdkVersion 25
versionCode 1
versionName "1.0"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
package com.carlosmuvi.segmentedprogressbar

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet


data class PropertiesModel(
var segmentCount : Int = 5,
var containerColor : Int = Color.LTGRAY,
var fillColor : Int = Color.BLUE
)
data class PropertiesModel(val context: Context, val attrs: AttributeSet?) {

var segmentCount: Int
var containerColor: Int
var fillColor: Int
var segmentGapWidth: Int

init {
if (attrs != null) {
val styledAttrs = context.theme.obtainStyledAttributes(attrs, R.styleable.SegmentedProgressBar, 0, 0)
segmentCount = styledAttrs.getInt(R.styleable.SegmentedProgressBar_segment_count, 5)
containerColor = styledAttrs.getColor(R.styleable.SegmentedProgressBar_container_color, Color.LTGRAY)
fillColor = styledAttrs.getColor(R.styleable.SegmentedProgressBar_fill_color, Color.BLUE)
segmentGapWidth = styledAttrs.getDimensionPixelSize(R.styleable.SegmentedProgressBar_gap_size, dpToPx(1))
} else {
segmentCount = 5
containerColor = Color.LTGRAY
fillColor = Color.BLUE
segmentGapWidth = dpToPx(1)
}
}

private fun dpToPx(valueInDp: Int): Int {
val density = context.resources.displayMetrics.density
return (valueInDp * density).toInt()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,25 @@ class SegmentedProgressBar : View {
private lateinit var containerRectanglePaint: Paint
private lateinit var fillRectanglePaint: Paint
private lateinit var drawingTimer: DrawingTimer
private val propertiesModel = PropertiesModel()
private lateinit var properties: PropertiesModel

constructor(context: Context) : super(context) {
initView()
}

constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
initView()
initView(attrs)
}

private fun initView() {
private fun initView(attrs: AttributeSet? = null) {
initDrawingTimer()
containerRectanglePaint = buildContainerRectanglePaint(propertiesModel.containerColor)
fillRectanglePaint = buildFillRectanglePaint(propertiesModel.fillColor)
initPropertiesModel(attrs)
containerRectanglePaint = buildContainerRectanglePaint(properties.containerColor)
fillRectanglePaint = buildFillRectanglePaint(properties.fillColor)
}

private fun initPropertiesModel(attrs: AttributeSet?) {
properties = PropertiesModel(context, attrs)
}

private fun initDrawingTimer() {
Expand Down Expand Up @@ -70,7 +75,7 @@ class SegmentedProgressBar : View {
}

fun setSegmentCount(segmentCount: Int) {
propertiesModel.segmentCount = segmentCount
properties.segmentCount = segmentCount
}

/*
Expand All @@ -88,7 +93,7 @@ class SegmentedProgressBar : View {
}

fun setCompletedSegments(completedSegments: Int) {
if (completedSegments <= propertiesModel.segmentCount) {
if (completedSegments <= properties.segmentCount) {
lastCompletedSegment = completedSegments
invalidate()
}
Expand All @@ -105,10 +110,10 @@ class SegmentedProgressBar : View {
val topY = 0
val botY = height

for (i in 0..propertiesModel.segmentCount - 1) {
for (i in 0..properties.segmentCount - 1) {
drawRoundedRect(canvas, leftX.toFloat(), topY.toFloat(), rightX.toFloat(), botY.toFloat(),
containerRectanglePaint)
leftX = leftX + segmentWidth + segmentGapWidth
leftX = leftX + segmentWidth + properties.segmentGapWidth
rightX = leftX + segmentWidth
}
}
Expand All @@ -123,15 +128,15 @@ class SegmentedProgressBar : View {

for (i in 0..lastCompletedSegment - 1) {
drawRoundedRect(canvas, leftX.toFloat(), topY.toFloat(), rightX.toFloat(), botY.toFloat(), fillRectanglePaint)
leftX = leftX + segmentWidth + segmentGapWidth
leftX = leftX + segmentWidth + properties.segmentGapWidth
rightX = leftX + segmentWidth
}
}

private fun drawCurrentRectangle(canvas: Canvas) {
val segmentWidth = segmentWidth

val leftX = lastCompletedSegment * (segmentWidth + segmentGapWidth)
val leftX = lastCompletedSegment * (segmentWidth + properties.segmentGapWidth)
val rightX = leftX + currentSegmentProgressInPx
val topY = 0
val botY = height
Expand Down Expand Up @@ -186,13 +191,5 @@ class SegmentedProgressBar : View {
}

private val segmentWidth: Int
get() = width / propertiesModel.segmentCount - segmentGapWidth

private val segmentGapWidth: Int
get() = dpToPx(1)

private fun dpToPx(valueInDp: Int): Int {
val density = context.resources.displayMetrics.density
return (valueInDp * density).toInt()
}
get() = width / properties.segmentCount - properties.segmentGapWidth
}
3 changes: 3 additions & 0 deletions library/src/main/res/values/segmentedprogressbar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
<resources>
<declare-styleable name="SegmentedProgressBar">
<attr name="gap_size" format="dimension" />
<attr name="segment_count" format="integer" />
<attr name="fill_color" format="color" />
<attr name="container_color" format="color" />
</declare-styleable>
</resources>

0 comments on commit a7f86ec

Please sign in to comment.