Skip to content

Public method and XML attribute for setting minimum alpha #6

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

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
5 changes: 2 additions & 3 deletions lib/src/main/java/com/example/lib/CoverFlowTransformer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import android.view.View
*/
private const val SCALE_MIN = .3f
private const val SCALE_MAX = 1f
private const val MIN_ALPHA = .5f
private const val SCALE = .05f

class CoverFlowTransformer : ViewPager.PageTransformer {
class CoverFlowTransformer(var minAlpha: Float) : ViewPager.PageTransformer {

var paddingFactor: Float = 0.08f

Expand All @@ -23,7 +22,7 @@ class CoverFlowTransformer : ViewPager.PageTransformer {
page.scaleY = realScale
if (realPosition != 0f) {
val scaleFactor = Math.max(SCALE_MIN, 1 - Math.abs(realPosition))
page.alpha = MIN_ALPHA + (scaleFactor - SCALE_MIN) / (1 - SCALE_MIN) * (1 - MIN_ALPHA)
page.alpha = minAlpha + (scaleFactor - SCALE_MIN) / (1 - SCALE_MIN) * (1 - minAlpha)
}
}

Expand Down
17 changes: 16 additions & 1 deletion lib/src/main/java/com/example/lib/Deck.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import android.util.TypedValue
* Created by bloder on 25/08/17.
*/
private const val DEFAULT_PERCENTAGE_PADDING = 8
private const val MIN_ALPHA = .5f

class Deck : ViewPager {

private val pageTransformer = CoverFlowTransformer()
private val pageTransformer = CoverFlowTransformer(MIN_ALPHA)

constructor(context: Context) : super(context) {
setPercentagePadding(context, DEFAULT_PERCENTAGE_PADDING)
Expand All @@ -31,6 +32,10 @@ class Deck : ViewPager {
if (dipPaddingXmlInPixel != Integer.MAX_VALUE) {
initProperties(context, dipPaddingXmlInPixel.toFloat())
}
val minAlphaXml = typedArray.getFloat(R.styleable.Deck_min_alpha, MIN_ALPHA)
if (minAlphaXml != MIN_ALPHA) {
setMinAlpha(minAlphaXml)
}
typedArray.recycle()

// set the default padding if no properties from XML
Expand Down Expand Up @@ -75,6 +80,16 @@ class Deck : ViewPager {
initProperties(context, padding)
}

/**
* Set minimum alpha for left and right views
*/
fun setMinAlpha(minAlpha: Float) {
when {
minAlpha < 0 || minAlpha > 1 -> throw IllegalArgumentException("Minimum alpha must be between 0 and 1")
else -> pageTransformer.minAlpha = minAlpha
}
}

private fun initProperties(context: Context, padding: Float) {
val intPadding = padding.toInt()
setPadding(intPadding, 0, intPadding, 0)
Expand Down
1 change: 1 addition & 0 deletions lib/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<declare-styleable name="Deck">
<attr name="padding_percentage" format="integer"/>
<attr name="padding_dp" format="dimension"/>
<attr name="min_alpha" format="float"/>
</declare-styleable>
</resources>