@@ -2,6 +2,7 @@ package com.example.lib
2
2
3
3
import android.app.Activity
4
4
import android.content.Context
5
+ import android.content.res.TypedArray
5
6
import android.support.v4.view.ViewPager
6
7
import android.util.AttributeSet
7
8
import android.util.DisplayMetrics
@@ -17,7 +18,19 @@ class Deck : ViewPager {
17
18
private val pageTransformer = CoverFlowTransformer ()
18
19
19
20
constructor (context: Context ) : super (context)
20
- constructor (context: Context , attributeSet: AttributeSet ) : super (context, attributeSet)
21
+
22
+ constructor (context: Context , attributeSet: AttributeSet ) : super (context, attributeSet) {
23
+ val typedArray: TypedArray = context.obtainStyledAttributes(attributeSet, R .styleable.Deck )
24
+ val percentagePaddingXml = typedArray.getInt(R .styleable.Deck_padding_percentage , Integer .MAX_VALUE )
25
+ if (percentagePaddingXml != Integer .MAX_VALUE ) {
26
+ setPercentagePadding(context, percentagePaddingXml)
27
+ }
28
+ val dipPaddingXmlInPixel = typedArray.getDimensionPixelSize(R .styleable.Deck_padding_dp , Integer .MAX_VALUE )
29
+ if (dipPaddingXmlInPixel != Integer .MAX_VALUE ) {
30
+ initProperties(context, dipPaddingXmlInPixel.toFloat())
31
+ }
32
+ typedArray.recycle()
33
+ }
21
34
22
35
init {
23
36
initView()
@@ -30,22 +43,22 @@ class Deck : ViewPager {
30
43
/* *
31
44
* Set left and right padding with default value
32
45
*/
33
- fun setDefaultPadding (context : Activity ) {
46
+ fun setDefaultPadding (context : Context ) {
34
47
setPercentagePadding(context, DEFAULT_PERCENTAGE_PADDING )
35
48
}
36
49
37
50
/* *
38
51
* Set left and right padding based on percentage of the screen width
39
52
* If the percentage is more than 18, the left and right items height might not be consistent
40
53
*/
41
- fun setPercentagePadding (context : Activity , percentage : Int ) {
54
+ fun setPercentagePadding (context : Context , percentage : Int ) {
42
55
when {
43
56
percentage == 0 -> initProperties(context, 0f )
44
57
percentage < 0 -> throw IllegalArgumentException (" Percentage can't be lower than 0" )
45
58
percentage > 100 -> throw IllegalArgumentException (" Percentage can't be higher than 100" )
46
59
else -> {
47
60
val metrics = DisplayMetrics ()
48
- context.windowManager.defaultDisplay.getMetrics(metrics)
61
+ ( context as Activity ) .windowManager.defaultDisplay.getMetrics(metrics)
49
62
val padding = metrics.widthPixels * percentage / 100f
50
63
initProperties(context, padding)
51
64
}
@@ -55,7 +68,7 @@ class Deck : ViewPager {
55
68
/* *
56
69
* Set left and right padding based on dp value
57
70
*/
58
- fun setDpPadding (context : Activity , dp : Float ) {
71
+ fun setDpPadding (context : Context , dp : Float ) {
59
72
val padding = TypedValue .applyDimension(
60
73
TypedValue .COMPLEX_UNIT_DIP ,
61
74
dp,
@@ -64,14 +77,14 @@ class Deck : ViewPager {
64
77
initProperties(context, padding)
65
78
}
66
79
67
- private fun initProperties (context : Activity , padding : Float ) {
80
+ private fun initProperties (context : Context , padding : Float ) {
68
81
val intPadding = padding.toInt()
69
82
setPadding(intPadding, 0 , intPadding, 0 )
70
83
clipToPadding = false
71
84
pageMargin = 0
72
85
73
86
val metrics = DisplayMetrics ()
74
- context.windowManager.defaultDisplay.getMetrics(metrics)
87
+ ( context as Activity ) .windowManager.defaultDisplay.getMetrics(metrics)
75
88
pageTransformer.paddingFactor = padding / metrics.widthPixels
76
89
}
77
90
}
0 commit comments