Foreword
Look at the effect of it ~
Function Point Analysis difficult to achieve
analysis:
- Assumptions: average height of the control block is divided into 26, and the width of the control block of the same width.
- Assumptions: Each letter is a square cell welt package. Centered within the cell block.
- Each letter coordinates (X axis, Y axis) is the bottom left corner position, i.e. the position of the lower left corner of the cell.
- In Case A: index 0
x coordinate: a float x = 0.5f the measuredWidth * - * the textWidth 0.5f (half the width of the control - half the width of the cell)
Y-axis coordinate: a float 0.5f + Y = cellHeight the textWidth * 0.5 * f (+ half the width of the cell is half the width of the control)- In Case B: Index 1
x coordinate: a float the measuredWidth * x = 0.5f - (half the width of the control - half the width of the cell) the textWidth * 0.5f
Y-axis coordinate: a float 0.5f + Y = cellHeight the textWidth * 0.5 * f + i * cellHeight (half of the half width of the cell control + width + front of all the height of the cell)
Description: the measuredWidth - the entire width of the custom control; cellHeight - height of the cell; textWidth - single letter width (available through the brush)
- 26 letters of the drawing, and the position of each letter
of the code sample (also refer to the specific implementation Demo)
//绘制 A-Z
for (int i = 0; i < LETTERS.length; i++) {
String letter = LETTERS[i];
//计算每个字母的坐标位置,通过画笔可以获取字母的宽度:measureText()
float x = cellWidth * 0.5f - paint.measureText(letter) * 0.5f;
//获取指定字符串指定区域的宽高信息
Rect rect = new Rect();
paint.getTextBounds(letter, 0, letter.length(), rect);
//获取字母的高度
int textHeight = rect.height();
//获取字母的宽度 和 paint.measureText(letter) 结果一样
//int textWidth = rect.width();
float y = cellHeight * 0.5f + textHeight * 0.5f + i * cellHeight;
//【改变选择字母】字母被选中时,重绘字母时,改变字母状态
paint.setColor(i == currentIndex ? Color.GRAY : Color.WHITE);
canvas.drawText(letter, x, y, paint);
}
- When generating a touch event, determine the selected position
private void getCurrentIndex(MotionEvent event) {
//获取当前所处位置的-Y轴-坐标(取值可能会小于0)
float y = event.getY();
//得到当前触摸位置是第几个索引位置
int index = (int) (y / cellHeight);
//获取当前索引和上次获取索引对比,相同则不处理
if (index != currentIndex) {
if (index >= 0 && index < LETTERS.length) {
currentIndex = index;
//设置一个监听回调:返回当前被选中的字母(也可以处理其他业务)
if (updateListener != null) {
updateListener.onLetterUpdate(LETTERS[index]);
}
}
}
}
- Pullback in activity in the listener, and the combination of custom controls and listView
@Override
public void onLetterUpdate(String letter) {
showCustomToast(letter); //自定义的一个Toast(可以查看Demo)
//触摸监听回调
//找到数据集合中首次出现letter的索引位置,listView直接跳到该索引位置
for (int index = 0; index < persons.size(); index++) {
String pin = persons.get(index).getNamePinYin().charAt(0) + "";
if (TextUtils.equals(pin, letter)) {
listView.setSelection(index);
break;
}
}
}
PS: Well, here, a simple custom fast indexing function is realized. I am pleased to share with you here and learn from each other.