Skip to content
This repository has been archived by the owner on Dec 17, 2023. It is now read-only.

Commit

Permalink
Slight api changes + added docs
Browse files Browse the repository at this point in the history
  • Loading branch information
alxrm committed Nov 16, 2016
1 parent c736b7b commit d29bf7a
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 63 deletions.
11 changes: 6 additions & 5 deletions app/src/main/java/rm/com/timecon/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ protected void onCreate(Bundle savedInstanceState) {
final TextView timeText = (TextView) findViewById(R.id.clocks_time);

// final ClockDrawable clockDrawable = ClockDrawable.builder(this)
// .hours(10)
// .minutes(30)
// .withColor(Color.WHITE)
// .withPointerWidth(Stroke.THIN)
// .hours(4)
// .minutes(20)
// .withSpeed(-2.5F)
// .withColor(0xAAFFFFFF)
// .withDuration(600)
// .withFrameWidth(Stroke.REGULAR)
// .into(clocks);

new Handler().postDelayed(new Runnable() {
Expand All @@ -45,7 +46,7 @@ public void run() {
@Override
public void run() {
timeText.setText("Indeterminate");
clocks.start();
clocks.animateIndeterminate();
}
}, 3000);

Expand Down
3 changes: 1 addition & 2 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
versionName "1.0.2"
}
buildTypes {
release {
Expand All @@ -24,7 +24,6 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.0.0'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
repositories {
Expand Down
107 changes: 107 additions & 0 deletions lib/src/main/kotlin/rm/com/clocks/Clock.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package rm.com.clocks

import android.animation.Animator
import android.animation.TimeInterpolator

/**
* Created by alex
*/
interface Clock {

/**
* Get or set hours on icon without animation, default is 0
*/
var hours: Int

/**
* Get or set minutes on icon without animation, default is 0
*/
var minutes: Int

/**
* Color of icon(pointers and frame), default is [android.graphics.Color.WHITE]
*/
var clockColor: Int

/**
* Speed of minutes pointer movement for endless(indeterminate) animation, see [animateIndeterminate]
*
* [indeterminateSpeed] can be negative if you need the rewind effect
* or if you need the effect of "ticking clock" you can set a very small fraction
*
* rewind: -1F
* ticking: 0.001F
*/
var indeterminateSpeed: Float

/**
* Flag of whether this icon has frame, e. g. the ring around the pointers
*
* Default is true
*/
var hasFrame: Boolean

/**
* Width of the frame, default is [DEFAULT_STROKE]
*
* It has type of [Stroke]
*
* [Stroke.THIN] -> 1 dip
* [Stroke.LIGHT] -> 2 dip
* [Stroke.REGULAR] -> 3 dip
* [Stroke.BOLD] -> 5 dip
*/
var frameWidth: Stroke

/**
* Width of the pointers, default is [DEFAULT_STROKE]
*
* It has type of [Stroke]
*
* [Stroke.THIN] -> 1 dip
* [Stroke.LIGHT] -> 2 dip
* [Stroke.REGULAR] -> 3 dip
* [Stroke.BOLD] -> 5 dip
*/
var pointerWidth: Stroke

/**
* Duration of animated time setting, default is [DEFAULT_DURATION] ms
*/
var timeSetDuration: Long

/**
* Interpolator of animated time setting, default is [DEFAULT_INTERPOLATOR]
*/
var timeSetInterpolator: TimeInterpolator

/**
* Animation listener for time setting animation
*/
var animationListener: Animator.AnimatorListener?

/**
* Animated time setting to concrete time
*
* [hours] new hours, where the shorter pointer will be animatedly moved
* It's int and can be any number, but it better should be 0..24(12)
* if it is less than zero, it'll be like 12 + [hours]
* so if [hours] == -1, than the value will be 11
*
* [minutes] new minutes, where the longer pointer will be animatedly moved
* It's int and can be any number, but it better should be 0..60
* if it is less than zero, it'll be like 60 + [minutes]
* so if [minutes] == -1, than the value will be 59
*/
fun animateToTime(hours: Int, minutes: Int)

/**
* Starts indeterminate(endless) animation,
* maybe useful for some waiting or processing indicators
*
* The steps of minute step is defined by [indeterminateSpeed]
*
* the hours pointer moves according to the range that minutes pointer moved through
*/
fun animateIndeterminate()
}
38 changes: 18 additions & 20 deletions lib/src/main/kotlin/rm/com/clocks/ClockDrawable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import android.widget.ImageView
/**
* Created by alex
*/
class ClockDrawable(private val ctx: Context) : Drawable(), Animatable {
class ClockDrawable(private val ctx: Context) : Drawable(), Clock, Animatable {

private var _minutes = 0
var minutes: Int
override var minutes: Int
set(value) {
_minutes = value.cycledClamp(to = COUNT_MINUTES)
minutesAngle = minutesDegOf(_minutes)
Expand All @@ -27,7 +27,7 @@ class ClockDrawable(private val ctx: Context) : Drawable(), Animatable {
get() = _minutes

private var _hours = 0
var hours: Int
override var hours: Int
set(value) {
_hours = value.cycledClamp(to = COUNT_HOURS)
hoursAngle = hoursDegOf(_hours, _minutes)
Expand All @@ -36,7 +36,7 @@ class ClockDrawable(private val ctx: Context) : Drawable(), Animatable {
}
get() = _hours

var clockColor = Color.WHITE
override var clockColor = Color.WHITE
set(value) {
field = value

Expand All @@ -46,46 +46,43 @@ class ClockDrawable(private val ctx: Context) : Drawable(), Animatable {
invalidateSelf()
}

/**
* defines whether this should draw a ring around the pointers
*/
var hasFrame = true
override var hasFrame = true
set(value) {
field = value
invalidateSelf()
}

var frameWidth = DEFAULT_STROKE
override var frameWidth = DEFAULT_STROKE
set(value) {
field = value
frame.strokeWidth = field.toDip(ctx)
invalidateSelf()
}

var pointerWidth = DEFAULT_STROKE
override var pointerWidth = DEFAULT_STROKE
set(value) {
field = value
pointers.strokeWidth = field.toDip(ctx)
invalidateSelf()
}

var indeterminateSpeed = DEFAULT_SPEED
override var indeterminateSpeed = DEFAULT_SPEED

var timeSetInterpolator: TimeInterpolator = DEFAULT_INTERPOLATOR
override var timeSetInterpolator: TimeInterpolator = DEFAULT_INTERPOLATOR
set(value) {
field = value

timeSetAnimator.interpolator = field
}

var timeSetDuration = DEFAULT_DURATION
override var timeSetDuration = DEFAULT_DURATION
set(value) {
field = value

timeSetAnimator.duration = field
}

var animationListener: Animator.AnimatorListener? = null
override var animationListener: Animator.AnimatorListener? = null
set(value) {
field = value

Expand All @@ -95,12 +92,12 @@ class ClockDrawable(private val ctx: Context) : Drawable(), Animatable {
}
}

private val frame = smoothLinePaint().apply {
private val frame = linePaintOf().apply {
strokeWidth = frameWidth.toDip(ctx)
color = clockColor
}

private val pointers = smoothLinePaint().apply {
private val pointers = linePaintOf().apply {
strokeWidth = pointerWidth.toDip(ctx)
color = clockColor
}
Expand Down Expand Up @@ -159,12 +156,11 @@ class ClockDrawable(private val ctx: Context) : Drawable(), Animatable {
pointers.colorFilter = filter
}

@JvmName("animateToTime")
fun animateTo(hrs: Int, min: Int) {
override fun animateToTime(hours: Int, minutes: Int) {
if (isRunning) stop()

_hours = hrs.cycledClamp(to = COUNT_HOURS)
_minutes = min.cycledClamp(to = COUNT_MINUTES)
_hours = hours.cycledClamp(to = COUNT_HOURS)
_minutes = minutes.cycledClamp(to = COUNT_MINUTES)
_absMinutes = (COUNT_MINUTES * _hours + _minutes).toFloat()

val beforeHrs = hoursAngle
Expand All @@ -187,6 +183,8 @@ class ClockDrawable(private val ctx: Context) : Drawable(), Animatable {
}
}

override fun animateIndeterminate() = start()

override fun isRunning(): Boolean = indeterminateAnimator.isRunning

override fun start() = indeterminateAnimator.start()
Expand Down
28 changes: 14 additions & 14 deletions lib/src/main/kotlin/rm/com/clocks/ClockImageView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,66 +12,65 @@ import android.widget.ImageView
* Created by alex
*/

class ClockImageView : ImageView, Animatable {

class ClockImageView : ImageView, Clock, Animatable {
var clock = ClockDrawable(context)
private set

var hours: Int
override var hours: Int
get() = clock.hours
set(value) {
clock.hours = value
}

var minutes: Int
override var minutes: Int
get() = clock.minutes
set(value) {
clock.minutes = value
}

var clockColor: Int
override var clockColor: Int
get() = clock.clockColor
set(value) {
clock.clockColor = value
}

var indeterminateSpeed: Float
override var indeterminateSpeed: Float
get() = clock.indeterminateSpeed
set(value) {
clock.indeterminateSpeed = value
}

var hasFrame: Boolean
override var hasFrame: Boolean
get() = clock.hasFrame
set(value) {
clock.hasFrame = value
}

var frameWidth: Stroke
override var frameWidth: Stroke
get() = clock.frameWidth
set(value) {
clock.frameWidth = value
}

var pointerWidth: Stroke
override var pointerWidth: Stroke
get() = clock.pointerWidth
set(value) {
clock.pointerWidth = value
}

var timeSetDuration: Long
override var timeSetDuration: Long
get() = clock.timeSetDuration
set(value) {
clock.timeSetDuration = value
}

var timeSetInterpolator: TimeInterpolator
override var timeSetInterpolator: TimeInterpolator
get() = clock.timeSetInterpolator
set(value) {
clock.timeSetInterpolator = value
}

var animationListener: Animator.AnimatorListener?
override var animationListener: Animator.AnimatorListener?
get() = clock.animationListener
set(value) {
clock.animationListener = value
Expand All @@ -97,8 +96,9 @@ class ClockImageView : ImageView, Animatable {
}
}

@JvmName(name = "animateToTime")
fun animateTo(hours: Int, minutes: Int) = clock.animateTo(hours, minutes)
override fun animateToTime(hours: Int, minutes: Int) = clock.animateToTime(hours, minutes)

override fun animateIndeterminate() = clock.animateIndeterminate()

override fun isRunning() = clock.isRunning

Expand Down
15 changes: 15 additions & 0 deletions lib/src/main/kotlin/rm/com/clocks/Constants.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package rm.com.clocks

import android.view.animation.OvershootInterpolator

/**
* Created by alex
*/
internal const val COUNT_MINUTES = 60
internal const val COUNT_HOURS = 12
internal const val POINTER_FACTOR_HOURS = .5F
internal const val POINTER_FACTOR_MINUTES = .8F
internal const val DEFAULT_SPEED = 1F
internal const val DEFAULT_DURATION = 600L
internal val DEFAULT_STROKE = Stroke.THIN
internal val DEFAULT_INTERPOLATOR = OvershootInterpolator()
Loading

0 comments on commit d29bf7a

Please sign in to comment.