1.效果

2.实现原理

1.绘制右侧的字母

2.onTouchEvent 中获取点击、滑动的位置,在通过invalidate() 去重新绘制,

​ 回调给activity去处理中间显示的文字

核心代码:

    @Override
protected void onDraw(Canvas canvas) {
mItemHeight = (getHeight() - getPaddingTop() - getPaddingBottom()) / mLetters.length;
for (int i = 0; i < mLetters.length; i++) {
int x = (int) (getWidth() / 2 - mPaint.measureText(mLetters[i]) / 2);
int middleHeight = mItemHeight * i + getHeight() / mLetters.length / 2 + getPaddingTop(); Paint.FontMetricsInt fontMetricsInt = mPaint.getFontMetricsInt();
int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom; int baseLine = middleHeight + dy;
//当前滑动到的字母 高亮显示
if (mCurrentLetter.equals(mLetters[i])) {
canvas.drawText(mLetters[i], x, baseLine, mHighPaint);
} else {
canvas.drawText(mLetters[i], x, baseLine, mPaint);
} }
}
 @Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
//获取当前触摸的下标
int currentIndex = getCurrentIndex(event.getY()); //防止重绘
if (!mCurrentLetter.equals(mLetters[currentIndex])) {
mCurrentLetter = mLetters[currentIndex];
if (mLetterTouchListener != null) {
mLetterTouchListener.onTouch(mCurrentLetter, true);
}
invalidate(); }
break;
case MotionEvent.ACTION_UP:
if (mLetterTouchListener != null) {
mLetterTouchListener.onTouch(mCurrentLetter, false);
}
break;
} return true;
}

源码地址

最新文章

  1. 前端组件化Polymer入门教程(8)——事件
  2. 2014.1.23 Discuz论坛迁移+VPS配置手记
  3. 7个你可能不认识的CSS单位:rem vh vw vmin vmax ex ch
  4. 转:程序员最值得关注的10个C开源项目
  5. C++ const &amp;&amp; 二叉树合集
  6. uedit富文本编辑器
  7. Find Minimum in Rotated Sorted Array问题的困惑
  8. 非常实用的JQuery的选项卡切换源码
  9. 初涉JavaScript模式 (9) : 函数 【常用方式】
  10. 查看Xcode所使用Swift的版本
  11. A Game of Thrones(19) - Jon
  12. JavaWeb(四)JDBC操作Oracle
  13. angular4.0中form表单双向数据绑定正确姿势
  14. http请求参数中包含特殊字符的严重后果,比如:#
  15. 013_实践HTTP206状态:部分内容和范围请求
  16. 26.C++- 泛型编程之类模板(详解)
  17. TODO 动态执行appium代码,便于修改和调试
  18. ElasticSearch的API介绍
  19. how to calculate the best fit to a plane in 3D, and how to find the corresponding statistical parameters
  20. (第8篇)实时可靠的开源分布式实时计算系统——Storm

热门文章

  1. k8s+docker_part2
  2. 下载centos镜像的地址
  3. Nebula Flink Connector 的原理和实践
  4. jwt介绍
  5. CoProcessFunction实战三部曲之二:状态处理
  6. moviepy音视频剪辑:与time时间线相关的变换函数freeze_region、make_loopable、speedx、time_mirror、time_symmetrize介绍
  7. flask实现分类搜索的小测试
  8. 百度前端技术学院-基础-day22-24
  9. 【题解】Generator(UVA1358)
  10. 题解-Decrease