Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Font Customization #44

Open
wants to merge 2 commits into
base: master
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ Here are the attributes you can specify through XML or related setters:
* `duration` - Duration of "bubble" rise in milliseconds.
* `initial_position` - Initial positon of "bubble" in range form `0.0` to `1.0`.
* `size` - Height of slider. Can be `small` (40dp) and `normal` (56dp).
* `slider_font` - Font resource used to render the slider's text.


This library is a part of a <a href="https://github.com/Ramotion/android-ui-animation-components-and-libraries"><b>selection of our best UI open-source projects.</b></a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import android.view.MotionEvent
import android.view.View
import android.view.ViewOutlineProvider
import android.view.animation.OvershootInterpolator
import androidx.annotation.FontRes
import androidx.core.content.res.ResourcesCompat
import com.ramotion.fluidslider.FluidSlider.Size.NORMAL
import com.ramotion.fluidslider.FluidSlider.Size.SMALL
import kotlin.math.*
Expand Down Expand Up @@ -196,6 +198,20 @@ class FluidSlider @JvmOverloads constructor(
*/
var endTrackingListener: (() -> Unit)? = null

/**
* Font resource used to render the slider's text
*/
@FontRes
var fontId: Int = -1
set(value) {
field = value

paintText.typeface = value.takeIf { it != -1 }?.let {
ResourcesCompat.getFont(context, it)
}
invalidate()
}

@SuppressLint("NewApi")
inner class OutlineProvider : ViewOutlineProvider() {
override fun getOutline(v: View?, outline: Outline?) {
Expand Down Expand Up @@ -225,6 +241,7 @@ class FluidSlider @JvmOverloads constructor(
val colorBarText: Int
val colorLabelText: Int
val duration: Long
val fontId: Int

constructor(superState: Parcelable?,
position: Float,
Expand All @@ -235,7 +252,8 @@ class FluidSlider @JvmOverloads constructor(
colorBar: Int,
colorBarText: Int,
colorLabelText: Int,
duration: Long) : super(superState) {
duration: Long,
fontId: Int) : super(superState) {
this.position = position
this.startText = startText
this.endText = endText
Expand All @@ -245,6 +263,7 @@ class FluidSlider @JvmOverloads constructor(
this.colorBarText = colorBarText
this.colorLabelText = colorLabelText
this.duration = duration
this.fontId = fontId
}

private constructor(parcel: Parcel) : super(parcel) {
Expand All @@ -257,6 +276,7 @@ class FluidSlider @JvmOverloads constructor(
this.colorBarText = parcel.readInt()
this.colorLabelText = parcel.readInt()
this.duration = parcel.readLong()
this.fontId = parcel.readInt()
}

override fun writeToParcel(parcel: Parcel, i: Int) {
Expand All @@ -270,6 +290,7 @@ class FluidSlider @JvmOverloads constructor(
parcel.writeInt(colorBarText)
parcel.writeInt(colorLabelText)
parcel.writeLong(duration)
parcel.writeInt(fontId)
}

override fun describeContents(): Int = 0
Expand Down Expand Up @@ -307,6 +328,8 @@ class FluidSlider @JvmOverloads constructor(

val defaultBarHeight = if (a.getInteger(R.styleable.FluidSlider_size, 1) == 1) Size.NORMAL.value else Size.SMALL.value
barHeight = defaultBarHeight * density

fontId = a.getResourceId(R.styleable.FluidSlider_slider_font, -1)
} finally {
a.recycle()
}
Expand Down Expand Up @@ -345,7 +368,7 @@ class FluidSlider @JvmOverloads constructor(
override fun onSaveInstanceState(): Parcelable {
return State(super.onSaveInstanceState(),
position, startText, endText, textSize,
colorBubble, colorBar, colorBarText, colorBubbleText, duration)
colorBubble, colorBar, colorBarText, colorBubbleText, duration, fontId)
}

override fun onRestoreInstanceState(state: Parcelable) {
Expand All @@ -360,6 +383,7 @@ class FluidSlider @JvmOverloads constructor(
colorBarText = state.colorBarText
colorBubbleText = state.colorLabelText
duration = state.duration
fontId = state.fontId
} else {
super.onRestoreInstanceState(state)
}
Expand Down
1 change: 1 addition & 0 deletions fluid-slider/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
<enum name="small" value="0"/>
<enum name="normal" value="1"/>
</attr>
<attr name="slider_font" format="reference" />
</declare-styleable>
</resources>