Skip to content

Commit ddc5a3a

Browse files
authored
Merge pull request #4 from hendrawd/Feat-AddPaddingMethods
Add padding methods by percentage of the screen width and dp
2 parents 2e80442 + 6d23299 commit ddc5a3a

File tree

7 files changed

+119
-26
lines changed

7 files changed

+119
-26
lines changed

README.md

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,50 @@ First of all you need to declare it in your layout xml file
1818
android:layout_width="match_parent"
1919
android:layout_height="match_parent"/>
2020
```
21-
22-
then you just need to set deck default padding
21+
Then you can set the padding(Optional)
2322

2423
```kotlin
2524
private val deckPager by lazy { activity.findViewById<Deck>(R.id.deck_pager) }
25+
```
2626

27-
private fun initDeck() {
28-
deckPager.useDefaultPadding(activity)
29-
}
27+
Set padding based on percentage of the screen width
28+
29+
```kotlin
30+
deckPager.setPercentagePadding(activity, 8)
31+
```
3032

33+
or set padding based on dp value
34+
35+
```kotlin
36+
deckPager.setDpPadding(activity, 16)
3137
```
3238

3339
##### Remember you can set all other view pager properties with deck
3440

41+
# Deck properties
42+
43+
These are all properties supported in deck's xml
44+
45+
```xml
46+
<com.example.lib.Deck
47+
android:id="@+id/deck_pager"
48+
android:layout_width="match_parent"
49+
android:layout_height="match_parent"
50+
app:padding_percentage="8"
51+
       app:padding_dp="16dp"/>
52+
```
53+
3554
# Import
3655

37-
It's easy import deck in your project, first you need add jit pack maven reference in your project
56+
Add JitPack maven repository reference in your project
3857

3958
```groovy
4059
repositories {
4160
maven { url 'https://jitpack.io' }
4261
}
4362
```
4463

45-
then add deck referente in your gradle dependencies
64+
then add deck reference in your gradle dependencies
4665

4766
##### Gradle
4867

app/src/main/java/com/example/bloder/deck/DeckAdapter.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.example.bloder.deck
22

33
import android.content.Context
4+
import android.support.v4.content.ContextCompat
45
import android.support.v4.view.PagerAdapter
56
import android.support.v7.widget.CardView
67
import android.view.LayoutInflater
@@ -18,7 +19,8 @@ class DeckAdapter(private val context: Context) : PagerAdapter() {
1819
override fun instantiateItem(container: ViewGroup?, position: Int): Any {
1920
val view = LayoutInflater.from(context).inflate(R.layout.card, null)
2021
container?.addView(view, 0)
21-
view.findViewById<CardView>(R.id.card).cardBackgroundColor = context.getColorStateList(cards[position].color)
22+
view.findViewById<CardView>(R.id.card).cardBackgroundColor =
23+
ContextCompat.getColorStateList(context, cards[position].color)
2224
view.findViewById<TextView>(R.id.title).text = cards[position].name
2325
return view
2426
}

app/src/main/java/com/example/bloder/deck/MainActivity.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,5 @@ class MainActivity : AppCompatActivity() {
1616
setContentView(R.layout.activity_main)
1717
deckPager.offscreenPageLimit = 5
1818
deckPager.adapter = DeckAdapter(this)
19-
deckPager.useDefaultPadding(this)
2019
}
2120
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<RelativeLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
34
xmlns:tools="http://schemas.android.com/tools"
45
android:layout_width="match_parent"
56
android:layout_height="match_parent"
@@ -9,6 +10,7 @@
910
android:id="@+id/deck_pager"
1011
android:layout_width="match_parent"
1112
android:layout_height="400dp"
12-
android:layout_centerInParent="true"/>
13+
android:layout_centerInParent="true"
14+
/>
1315

1416
</RelativeLayout>

lib/src/main/java/com/example/lib/CoverFlowTransformer.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@ import android.view.View
77
/**
88
* Created by bloder on 25/08/17.
99
*/
10+
private const val SCALE_MIN = .3f
11+
private const val SCALE_MAX = 1f
12+
private const val MIN_ALPHA = .5f
13+
private const val SCALE = .05f
14+
1015
class CoverFlowTransformer : ViewPager.PageTransformer {
1116

12-
private var SCALE_MIN = 0.3f
13-
private var SCALE_MAX = 1f
14-
private var MIN_ALPHA = 0.5f
15-
private var scale = .05f
16-
private var paddingFactor: Float = 0.08f
17+
var paddingFactor: Float = 0.08f
1718

1819
override fun transformPage(page: View, position: Float) {
1920
val realPosition = position - paddingFactor
20-
val realScale = getFloat(1 - Math.abs(realPosition * scale), SCALE_MIN, SCALE_MAX)
21+
val realScale = getFloat(1 - Math.abs(realPosition * SCALE), SCALE_MIN, SCALE_MAX)
2122
page.scaleX = realScale
2223
page.scaleY = realScale
2324
if (realPosition != 0f) {

lib/src/main/java/com/example/lib/Deck.kt

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,91 @@ package com.example.lib
22

33
import android.app.Activity
44
import android.content.Context
5+
import android.content.res.TypedArray
56
import android.support.v4.view.ViewPager
67
import android.util.AttributeSet
78
import android.util.DisplayMetrics
9+
import android.util.TypedValue
810

911
/**
1012
* Created by bloder on 25/08/17.
1113
*/
14+
private const val DEFAULT_PERCENTAGE_PADDING = 8
15+
1216
class Deck : ViewPager {
1317

14-
constructor(context: Context) : super(context)
15-
constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)
18+
private val pageTransformer = CoverFlowTransformer()
1619

17-
init { initView() }
20+
constructor(context: Context) : super(context) {
21+
setPercentagePadding(context, DEFAULT_PERCENTAGE_PADDING)
22+
}
1823

19-
private fun initView() = this.setPageTransformer(true, CoverFlowTransformer())
24+
constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) {
25+
val typedArray: TypedArray = context.obtainStyledAttributes(attributeSet, R.styleable.Deck)
26+
val percentagePaddingXml = typedArray.getInt(R.styleable.Deck_padding_percentage, Integer.MAX_VALUE)
27+
if (percentagePaddingXml != Integer.MAX_VALUE) {
28+
setPercentagePadding(context, percentagePaddingXml)
29+
}
30+
val dipPaddingXmlInPixel = typedArray.getDimensionPixelSize(R.styleable.Deck_padding_dp, Integer.MAX_VALUE)
31+
if (dipPaddingXmlInPixel != Integer.MAX_VALUE) {
32+
initProperties(context, dipPaddingXmlInPixel.toFloat())
33+
}
34+
typedArray.recycle()
2035

21-
fun useDefaultPadding(context: Activity) {
22-
val metrics = DisplayMetrics()
23-
context.windowManager.defaultDisplay.getMetrics(metrics)
24-
val padding = (metrics.widthPixels * 0.08).toInt()
25-
setPadding(padding, 0, padding, 0)
36+
// set the default padding if no properties from XML
37+
if (percentagePaddingXml == Integer.MAX_VALUE && dipPaddingXmlInPixel == Integer.MAX_VALUE) {
38+
setPercentagePadding(context, DEFAULT_PERCENTAGE_PADDING)
39+
}
40+
}
41+
42+
init {
43+
initView()
44+
}
45+
46+
private fun initView() {
47+
setPageTransformer(true, pageTransformer)
48+
}
49+
50+
/**
51+
* Set left and right padding based on percentage of the screen width
52+
* If the percentage is more than 18, the left and right items height might not be consistent
53+
*/
54+
fun setPercentagePadding(context: Context, percentage: Int) {
55+
when {
56+
percentage == 0 -> initProperties(context, 0f)
57+
percentage < 0 -> throw IllegalArgumentException("Percentage can't be lower than 0")
58+
percentage >= 50 -> throw IllegalArgumentException("Your layout will not visible if the percentage equals or higher than 50")
59+
else -> {
60+
val padding = screenWidth(context) * percentage / 100f
61+
initProperties(context, padding)
62+
}
63+
}
64+
}
65+
66+
/**
67+
* Set left and right padding based on dp value
68+
*/
69+
fun setDpPadding(context: Context, dp: Float) {
70+
val padding = TypedValue.applyDimension(
71+
TypedValue.COMPLEX_UNIT_DIP,
72+
dp,
73+
context.resources.displayMetrics
74+
)
75+
initProperties(context, padding)
76+
}
77+
78+
private fun initProperties(context: Context, padding: Float) {
79+
val intPadding = padding.toInt()
80+
setPadding(intPadding, 0, intPadding, 0)
2681
clipToPadding = false
2782
pageMargin = 0
83+
84+
pageTransformer.paddingFactor = padding / screenWidth(context)
85+
}
86+
87+
private fun screenWidth(context: Context): Int {
88+
val metrics = DisplayMetrics()
89+
(context as Activity).windowManager.defaultDisplay.getMetrics(metrics)
90+
return metrics.widthPixels
2891
}
2992
}

lib/src/main/res/values/attrs.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<declare-styleable name="Deck">
4+
<attr name="padding_percentage" format="integer"/>
5+
<attr name="padding_dp" format="dimension"/>
6+
</declare-styleable>
7+
</resources>

0 commit comments

Comments
 (0)