//这个方法用来创建9个点 private fun initNineDot() { for (i in 1..9) { ImageView(context).apply { //给每张图片设置最初显示的图片 setImageResource(R.drawable.dot_normal) //设置每个点的 tag 值 tag = "$i" //将每个 ImageView 加到容器中去 addView(this) //收集已经创建好的点 dotViews.add(this) } } }
//这个方法用来指定这9个点如何布局和显示 private fun layoutNineDot() { for (row in 0..2) { for (column in 0..2) { //指定每个图片的 左 上 右 下 各个位置 val left = column * (dotSize + space) val top = row * (dotSize + space) val right = left + dotSize val bottom = top + dotSize /** * 每个视图添加到容器之后,后一个视图会重叠在前一个视图上 * 第一个被添加上去的索引是0,第二个就是1... * 因此可以用索引来拿到每个视图,并将其摆放在合适的位置 * 用 getChildAt(index) 来得到视图 */ val index = row * 3 + column val dotView = getChildAt(index) //将这个子控件进行布局,告诉它因该在父容器中如何显示 dotView.layout(left, top, right, bottom) } } }
六条横线的创建及显示
由于六条横线和上面九个点的创建和显示大同小异,这里就省略了一些注释,其实道理都是一样的,计算 tag 值,计算对应 view 的 index 值,计算 lefttoprightbottom 并进行对应的布局,
private fun initHorizontalLine() { /** * 这是六条横线对应的 tag 值 * 12 23 * 45 56 * 78 89 */ var tag = 0 for (i in 0..5) { ImageView(context).apply { setImageResource(R.drawable.line_horizontal) if (i == 0) { tag = 12 } else { tag += i % 2 * 11 + (i + 1) % 2 * 22 } this.tag = tag Log.d(TAG, "tag: $tag") Log.d(TAG, "initLandscapeLine: ") addView(this) } } }
private fun layoutHorizontalLine() { for (row in 0..2) { for (column in 0..1) { val left = dotSize + column * (dotSize + space) val top = dotSize / 2 + row * lineSize val right = left + space val bottom = top + dp2px(2)
//找到这根线在父容器中的索引 val index = 9 + row * 2 + column val lineView = getChildAt(index) lineView.layout(left, top, right, bottom) lineView.visibility = INVISIBLE } } }
//创建六条竖线 private fun initVerticalLine() { //14 25 36 //47 58 69 var tag = 0 for (i in 0..5) { ImageView(context).apply { setImageResource(R.drawable.line_vertical) if (i == 0) { tag = 14 } else { tag += 11 }
this.tag = tag addView(this) } } }
//对这六条竖线进行布局 private fun layoutVerticalLine() { for (row in 0..1) { for (column in 0..2) { val left = dotSize / 2 + column * (dotSize + space) val top = dotSize + row * (dotSize + space) val right = left + dp2px(2) val bottom = top + space
//找到这根线在父容器中的索引 val index = 15 + row * 3 + column val lineView = getChildAt(index) lineView.layout(left, top, right, bottom) lineView.visibility = INVISIBLE } } }
//创建四条向右倾斜的直线 private fun initSlashLine() { //15 26 //48 59 var tag = 0 for (i in 0..3) { ImageView(context).apply { setImageResource(R.drawable.line_right) if (i == 0) { tag = 15 } else { tag += (i % 2) * 11 + (i + 1) % 2 * 22 } this.tag = tag Log.d(TAG, "initSlashLine: tag = $tag") addView(this) } } }
//对这四条直线进行布局 private fun layoutSlashLine() { for (row in 0..1) { for (column in 0..1) { val left = dotSize / 2 + sqrt(2.0) / 4 * dotSize + column * (dotSize + space) val top = dotSize / 2 + sqrt(2.0) * dotSize / 4 + row * (dotSize + space) val right = left + space + (dotSize / 2 - sqrt(2.0) * dotSize.toDouble() / 4) * 2 val bottom = top + space + (dotSize / 2 - sqrt(2.0) * dotSize.toDouble() / 4) * 2
//找到这根线在父容器中的索引 val index = 21 + row * 2 + column val lineView = getChildAt(index) lineView.layout(left.toInt(), top.toInt(), right.toInt(), bottom.toInt()) lineView.visibility = INVISIBLE } } }
private fun initLLine() { //24 35 //57 68 var tag = 0 for (i in 0..3) { ImageView(context).apply { setImageResource(R.drawable.line_left) if (i == 0) { tag = 24 } else { tag += (i % 2) * 11 + (i + 1) % 2 * 22 } this.tag = tag addView(this) } } }
private fun layoutLLine() { for (row in 0..1) { for (column in 0..1) { val left = dotSize / 2 + sqrt(2.0) / 4 * dotSize + column * (dotSize + space) val top = dotSize / 2 + sqrt(2.0) * dotSize / 4 + row * (dotSize + space) val right = left + space + (dotSize / 2 - sqrt(2.0) * dotSize.toDouble() / 4) * 2 val bottom = top + space + (dotSize / 2 - sqrt(2.0) * dotSize.toDouble() / 4) * 2
//找到这根线在父容器中的索引 val index = 25 + row * 2 + column val lineView = getChildAt(index) lineView.layout(left.toInt(), top.toInt(), right.toInt(), bottom.toInt()) lineView.visibility = INVISIBLE } } }