自定义控件 - 字母索引 : LetterIndexView
实现字母列表,滑动列表显示当前选中字母,回调接口。
1.实现字母列表。初始化相关属性。计算每个字母所占宽高。绘制字母A-Z,#。
private int itemWidth;//每个字母所占宽度
private int itemHeight;//每个字母所占高度
private Paint mPaint;//画笔
private int selectIndex = -1;//选中索引
private String[] letters = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
"M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"}; private int letterColor;//字母颜色
private int selectLetterColor;//选中字母颜色
private int selectBgColor;//选中背景颜色 private OnLetterChangedListener mLetterChangedListener;// 触摸字母改变事件 public LetterIndexView(Context context) {
this(context, null);
} public LetterIndexView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
} //初始化文字颜色属性,画笔
public LetterIndexView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LetterIndexView, 0, 0);
letterColor = typedArray.getColor(R.styleable.LetterIndexView_letterColor, 0);
selectLetterColor = typedArray.getColor(R.styleable.LetterIndexView_selectLetterColor, 0);
selectBgColor = typedArray.getColor(R.styleable.LetterIndexView_selectBackgroundColor, 0);
typedArray.recycle();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setTextSize(30);
mPaint.setAntiAlias(true);
} //获取item宽高
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
itemWidth = getMeasuredWidth();
itemHeight = getMeasuredHeight() / letters.length;
} //绘制字母列表
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//字母坐标从文字左下角开始
for (int i = 0; i < letters.length; i++) {
if (i == selectIndex) {
mPaint.setColor(selectLetterColor);
} else {
mPaint.setColor(letterColor);
}
//获取x坐标 (行宽-字母宽度)除以 2
float x = (itemWidth - mPaint.measureText(letters[i])) / 2;
Rect rect = new Rect();
mPaint.getTextBounds(letters[i], 0, letters[i].length(), rect);
int textHeight = rect.height();
//获取y坐标 (行高+字母高度)除以 2 + 行高*i
float y = (itemHeight + textHeight) / 2 + itemHeight * i;
canvas.drawText(letters[i], x, y, mPaint);
}
if (selectIndex == -1) {
setBackgroundColor(0);
} else {
setBackgroundColor(selectBgColor);
}
}
2 滑动列表,处理ontouchevent事件,改变选中字母颜色。
/**
* 当手指触摸按下的时候改变字母背景颜色
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
float downY = event.getY();
int index = (int) (downY / itemHeight);//获取按下点的索引
if (index != selectIndex)
selectIndex = index;
//防止数组越界
if (mLetterChangedListener != null && selectIndex > -1 && selectIndex < letters.length)
mLetterChangedListener.onChanged(letters[selectIndex], selectIndex);
break;
case MotionEvent.ACTION_UP:
selectIndex = -1;
break;
}
invalidate();
return true;
}
3 触摸回调接口
//触摸选中字母回调接口
public interface OnLetterChangedListener {
void onChanged(String s, int position);
} public void setOnLetterChangedListener(OnLetterChangedListener letterChangedListener) {
mLetterChangedListener = letterChangedListener;
}
几个关键的API:
typearray:获取属性
paint:画笔
canvas:画布,canvas.drawText(“text”,x,y,paint);
获取文本宽度:paint.measureText()
获取文本高度:Rect rect;paint.getTextBounds(rect);rect.height();
获取按下点的字母索引 int index = (int) event.getY() / itemHeight ;
重绘:invalidate();
自定义控件 - 字母索引 : LetterIndexView的更多相关文章
- 做个简单的Android列表字母索引控件
相信大家在许多App中都见到过带字母索引的界面,比如我最近看到的这个开源控件: WaveSideBar 很酷是不是?!!!如果加在例如联系人列表界面上,大大提升了用户体验. 那么这个索引控件要怎么做呢 ...
- HBuilder+eclipse开发:使用ajax异步传值生成首字母索引
使用ajax异步传值生成首字母索引大致有以下几个步骤: 1.服务器端使用servlet提取出数据库里的数据; 2.使用首字母工具类对数据进处理得到首字母; 3.再将首字母和数据一一对应存入json数组 ...
- 简单实现UITableView索引功能(中英文首字母索引) (二) By HL
简单实现UITableView索引功能(中英文首字母索引)(一) ByH罗 相关类: NSString+PinYing(获取中英文首字母) 参考上面链接 #import "ViewCon ...
- android字母索引实现ListView定位
最近闲的很,没什么事干 ,在玩手机的时间看到android系统自带的那个通讯录软件对联系人的快速定位功能. 感觉这个功能也比较实用自己就试着自己去实现. 虽然网络上还是有大牛封闭好了的框架,但是如果 ...
- 分享一份550多个Linux命令的文档,按照命令首字母索引排序
输入一个命令,让我给你一个关于它的完美解释! 众所周知,Linux命令是IT人必须掌握的一个技能,有了它,我们可以部署和维护各种各样的服务和应用.但是,大部分的Linux命令我们不一定记得住,而别是各 ...
- 简单实现UITableView索引功能(中英文首字母索引)(一) ByH罗
UITableView索引功能是常见的,主要是获取中英文的首字母并排序,系统自带获取首字母 //系统获取首字母 - (NSString *) pinyinFirstLetter:(NSString*) ...
- 联系人的侧边字母索引ListView 将手机通讯录姓名通过首字母排序。
package com.lixu.letterlistview; import java.util.ArrayList; import java.util.List; import org.apa ...
- 微信小程序通讯录首字母索引效果,车辆品牌选择列表
效果图: wxml代码: <block wx:for="{{list}}"> <view class='letter' id="letter{{inde ...
- sqlserver 汉字转拼音 首写字母 索引 函数
create function fun_getPY(@str nvarchar(4000)) returns nvarchar(4000) as begin declare @word nchar(1 ...
随机推荐
- 3-6如何在一个for语句中迭代多个可迭代对象
1.并行迭代 迭代元组可以进行拆包迭代. >>> zip([1,2,3,4],('a','b','c','d')) [(1, 'a'), (2, 'b'), (3, 'c'), (4 ...
- “程序包com.sun.tools.javac.util不存在” 问题解决
最近工作中在编译打包项目的时候遇到了如标题所示的问题,报这个错误的类是 com.sun.tools.javac.util.Pair.问题很诡异,在Idea可以导入此类,项目启动运行也很正常,但就是在打 ...
- input check复选框选择后修改<a>标签超链接href
1. 给复选框添加onclick事件 获取标签id <tbody> <c:forEach var="file" items="${files}" ...
- ubuntu不能登陆
开机按shift,找到之前的内核版本或者recovery 安装vmtools 报错Not enough free space to extract VMwareTools 解决办法:将此文件夹复制到另 ...
- GUI学习之十二——QTextEdit学习总结
在学习了QFrame和QAbstractScrollArea两个父类后,接下来是一个重头戏:QTextEdit. 一.QTextEdit特性 QTextEdit是一个高级的WYSIWYG(What Y ...
- python3:类和实例
面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各自的数据可 ...
- xtrabackup备份mysql以及创建自动定时任务
xtrabackup的安装 安装依赖关系 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo y ...
- 一、MyBatis基本使用,包括xml方式、注解方式、及动态SQL
一.简介 发展历史:MyBatis 的前 身是 iBATIS.最初侧重于 密码软件的开发 , 后来发展成为一款基于 Java 的持久层框架. 定 位:MyBatis 是一款优秀的支持自定义 ...
- sql server,mysql 和navicat for mysql的区别
一.定义 sql server 应该指的是sqlserver数据库,包含数据库管理系统等. navicat for sql server只是一个sqlserver的第三方的开发工具,管理工具. 二.开 ...
- 线程协作之threading.Condition
领会下面这个示例吧,其实跟java中wait/nofity是一样一样的道理 import threading # 条件变量,用于复杂的线程间同步锁 """ 需求: 男:小 ...