6.自定义view-侧边字母导航LetterNavigationBar
2024-10-19 02:00:59
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;
}
最新文章
- 前端组件化Polymer入门教程(8)——事件
- 2014.1.23 Discuz论坛迁移+VPS配置手记
- 7个你可能不认识的CSS单位:rem vh vw vmin vmax ex ch
- 转:程序员最值得关注的10个C开源项目
- C++ const &;&; 二叉树合集
- uedit富文本编辑器
- Find Minimum in Rotated Sorted Array问题的困惑
- 非常实用的JQuery的选项卡切换源码
- 初涉JavaScript模式 (9) : 函数 【常用方式】
- 查看Xcode所使用Swift的版本
- A Game of Thrones(19) - Jon
- JavaWeb(四)JDBC操作Oracle
- angular4.0中form表单双向数据绑定正确姿势
- http请求参数中包含特殊字符的严重后果,比如:#
- 013_实践HTTP206状态:部分内容和范围请求
- 26.C++- 泛型编程之类模板(详解)
- TODO 动态执行appium代码,便于修改和调试
- ElasticSearch的API介绍
- how to calculate the best fit to a plane in 3D, and how to find the corresponding statistical parameters
- (第8篇)实时可靠的开源分布式实时计算系统——Storm
热门文章
- k8s+docker_part2
- 下载centos镜像的地址
- Nebula Flink Connector 的原理和实践
- jwt介绍
- CoProcessFunction实战三部曲之二:状态处理
- moviepy音视频剪辑:与time时间线相关的变换函数freeze_region、make_loopable、speedx、time_mirror、time_symmetrize介绍
- flask实现分类搜索的小测试
- 百度前端技术学院-基础-day22-24
- 【题解】Generator(UVA1358)
- 题解-Decrease