1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| class YVPDotIndicator : LinearLayout { private var mStartPos: Float = 0.0F private var mWidthOffset: Int = 0
private var mPaint: Paint? = null
private var mIndicatorColor = Color.parseColor("#FFFFFF") private var mIndicatorRadius = 2
private var mVp: ViewPager? = null private var pageListener = InterPageChangeListener()
private var mTabCount: Int? = 0 private var mTabWidth: Float? = 0.0F private val defaultLayoutParams = LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f)
constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0) constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) { setWillNotDraw(false)
val dm = resources.displayMetrics
val a = context.theme.obtainStyledAttributes(attrs, R.styleable.YVPDotIndicator, defStyle, 0) mIndicatorRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mIndicatorRadius.toFloat(), dm).toInt()
mIndicatorColor = a.getColor(R.styleable.YVPDotIndicator_y_indicator_color, Color.parseColor("#FFFFFF")) mIndicatorRadius = a.getDimensionPixelSize(R.styleable.YVPDotIndicator_y_indicator_radius, 2) a.recycle()
initPaint() }
fun setViewPager(vp: ViewPager) { mVp = vp if (vp.adapter == null) { throw IllegalArgumentException() } notifyDataSetChanged() mVp?.addOnPageChangeListener(pageListener) }
fun notifyDataSetChanged() { this.removeAllViews() mTabCount = mVp?.adapter?.count for (i in 0..mTabCount?.let { it - 1 } as Int) { addTextTab(i, mVp?.adapter?.getPageTitle(i).toString()) } }
fun addTextTab(position: Int, title: String) { var tab = TextView(context) tab.text = title tab.gravity = Gravity.CENTER tab.setSingleLine()
tab.isFocusable = true tab.setOnClickListener { mVp?.currentItem = position }
this.addView(tab, position, defaultLayoutParams) }
private fun initPaint() { mPaint = Paint() mPaint?.color = mIndicatorColor mPaint?.isAntiAlias = true mPaint?.style = Paint.Style.FILL }
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { super.onSizeChanged(w, h, oldw, oldh) mTabWidth = (w / childCount).toFloat() mStartPos = mTabWidth?.let { it/2 } as Float }
override fun dispatchDraw(canvas: Canvas?) { canvas?.save() canvas?.translate(0.0F, height.toFloat()) canvas?.drawCircle(mStartPos + mWidthOffset, -mIndicatorRadius.toFloat(), mIndicatorRadius.toFloat(), mPaint) canvas?.restore() super.dispatchDraw(canvas) }
inner class InterPageChangeListener: ViewPager.OnPageChangeListener { override fun onPageScrollStateChanged(state: Int) {
}
override fun onPageSelected(position: Int) {
}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { val tabWidth = screenWidth / childCount mWidthOffset = (tabWidth * position + tabWidth * positionOffset).toInt() invalidate() } }
private val screenWidth: Int get() { val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager val displayMetrics = DisplayMetrics() windowManager.defaultDisplay.getMetrics(displayMetrics) return displayMetrics.widthPixels } }
|