android 字母索引三方,Android 字母索引动态自定义布局
public class SideBar extends View {
/** 字母更新的监听器 */
private OnLetterUpdateListener onLetterUpdateListener;
/** 当前的字母指示的位置 */
private int currentIndex = -100;
/** 是否松手 */
private boolean isUp = true;
/** 字母测量的尺寸 */
private float measureText;
/** 数字模拟器 */
private Scroller scroller;
/** 触摸临界值 */
private float touchSlop;
/** 起始位置 */
private int startX = 100;
/** 变化比率 */
private float mX = 100;
//up事件的接口
public interface OnUpListener {
void up();
}
private OnUpListener onUpListener;
public interface OnLetterUpdateListener {
void onLetterUpdate(String letter);
}
public OnUpListener getOnUpListener() {
return onUpListener;
}
public void setOnUpListener(OnUpListener onUpListener) {
this.onUpListener = onUpListener;
}
public void setOnLetterUpdateListener(OnLetterUpdateListener onLetterUpdateListener) {
this.onLetterUpdateListener = onLetterUpdateListener;
}
private static final 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 Paint paint;
// 单元格宽度
private int cellWidth;
// 单元格高度
private float cellHeight;
public SideBar(Context context) {
this(context, null);
}
public SideBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SideBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
/**
* 初始化数字模拟器
*/
private void init() {
scroller = new Scroller(getContext());
touchSlop = ViewConfiguration.get(getContext()).getTouchSlop();
// 创建一个抗锯齿的画笔
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// 画笔文本加粗
paint.setTypeface(Typeface.DEFAULT_BOLD);
// 颜色
paint.setColor(getResources().getColor(R.color.text_blue_search));
setTextSize(26);
measureText = paint.measureText("M");
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int mHeight = getMeasuredHeight();
cellWidth = getMeasuredWidth();
cellHeight = mHeight * 1.0f / LETTERS.length;
if (!isUp) {
setMeasuredDimension(widthMeasureSpec * 3, heightMeasureSpec);
} else {
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
}
@Override
protected void onDraw(Canvas canvas) {
// 遍历26个英文字母, 计算坐标, 进行绘制
for (int i = 0; i < LETTERS.length; i++) {
String letter = LETTERS[i];
// 计算x坐标
float dx = 0;
//float x = cellWidth * 0.5f - paint.measureText(letter) * 0.5f + 80;
// float x = cellWidth - paint.measureText(letter);
float x = cellWidth - measureText * 1.5f;
if (!isUp) {
mX = 1.0f;
}
switch (currentIndex - i) {
case -4:
dx = measureText * 1.5f * mX;
paint.setColor(getResources().getColor(R.color.text_blue_search_4));
setTextSize(27);
break;
case -3:
dx = measureText * 2.5f * mX;
paint.setColor(getResources().getColor(R.color.text_blue_search_3));
setTextSize(33);
break;
case -2:
dx = measureText * 4.0f * mX;
paint.setColor(getResources().getColor(R.color.text_blue_search_2));
setTextSize(38);
break;
case -1:
dx = measureText * 5.0f * mX;
paint.setColor(getResources().getColor(R.color.text_blue_search_1));
setTextSize(42);
break;
case 0:
paint.setColor(getResources().getColor(R.color.text_blue_search));
dx = measureText * 6.0f * mX;
setTextSize(50);
break;
case 1:
dx = measureText * 5.0f * mX;
paint.setColor(getResources().getColor(R.color.text_blue_search_1));
setTextSize(42);
break;
case 2:
dx = measureText * 4.0f * mX;
paint.setColor(getResources().getColor(R.color.text_blue_search_2));
setTextSize(38);
break;
case 3:
dx = measureText * 2.5f * mX;
paint.setColor(getResources().getColor(R.color.text_blue_search_2));
setTextSize(33);
break;
case 4:
dx = measureText * 1.5f * mX;
paint.setColor(getResources().getColor(R.color.text_blue_search_3));
setTextSize(28);
break;
default:
dx = 0;
paint.setColor(getResources().getColor(R.color.text_blue_search_4));
setTextSize(26);
break;
}
if (!isUp) { //触摸状态
x -= dx;
} else { //松手状态
x -= dx;
paint.setColor(getResources().getColor(R.color.text_blue_search));
setTextSize(26);
}
// 计算y坐标
Rect bounds = new Rect();
// 获取文本的矩形区域
paint.getTextBounds(letter, 0, letter.length(), bounds);
float y = cellHeight * 0.5f + bounds.height() * 0.5f + i * cellHeight;
// 绘制文本
if (letter.equals("I")) {
x += measureText * 0.2f;
}
canvas.drawText(letter, x, y, paint);
}
}
private int lastIndex = -1;
@Override
public boolean onTouchEvent(MotionEvent event) {
float y;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
scroller.forceFinished(true);
isUp = false;
setPadding(500, 0, 0, 0);
// 获取被点击到的字母索引
y = event.getY();
// 根据y值, 计算当前按下的字母位置
currentIndex = (int) (y / cellHeight);
if (currentIndex != lastIndex) {
if (currentIndex >= 0 && currentIndex < LETTERS.length) {
String letter = LETTERS[currentIndex];
if (onLetterUpdateListener != null) {
onLetterUpdateListener.onLetterUpdate(letter);
}
// 记录上一次触摸的字母
lastIndex = currentIndex;
}
}
break;
case MotionEvent.ACTION_MOVE:
// 获取被点击到的字母索引
y = event.getY();
// 根据y值, 计算当前按下的字母位置
currentIndex = (int) (y / cellHeight);
if (currentIndex != lastIndex) {
if (currentIndex >= 0 && currentIndex < LETTERS.length) {
String letter = LETTERS[currentIndex];
if (onLetterUpdateListener != null) {
onLetterUpdateListener.onLetterUpdate(letter);
}
// 记录上一次触摸的字母
lastIndex = currentIndex;
}
}
break;
case MotionEvent.ACTION_UP:
isUp = true;
lastIndex = -1;
//调用回调里面的方法
if (null != onUpListener) {
onUpListener.up();
}
smaooth();
break;
default:
break;
}
invalidate();
return true;
}
/**
* 开启数字模拟器
*/
private void smaooth() {
scroller.startScroll(startX, 0,
-99, 0, 550);
}
@Override
public void computeScroll() {
// 当scroller数据模拟完毕时, 不应该继续进行递归
// 反之, 如果正在模拟数据才进行递归的操作
if (scroller.computeScrollOffset()) {// 当前还是正在模拟数据中
// 把当前scroller正在模拟的数据取出来, 使用scrollTo方法切换屏幕
mX = ((float) scroller.getCurrX()) / 100f;
// scrollTo(currX, 0);
if (mX == 0.01f) {
setPadding(0, 0, 0, 0);
}
invalidate(); // 在触发当前方法, 相当于递归.
}
}
private void setTextSize(int size) {
paint.setTextSize(size+10);
}
}
android 字母索引三方,Android 字母索引动态自定义布局相关推荐
- android 字母索引三方,Android ListView字母索引(仿微信通讯录列表)
布局代码 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_paren ...
- Android学习笔记(Android Studio)3-3(ProgressBar ProgressDialog)(加载进度条、转圈圈)UI组件之弹出组件
Android学习笔记3-3 推荐新手向学习视频:B站https://www.bilibili.com/video/av38409964点我传送 3-3 ProgressBar & Progr ...
- android 语音发送功能,Android仿微信、录制音频并发送功能
MyRecorder(仿微信,录制音频并发送功能) ①布局实现(activity_main.xml)布局采用线性布局,上面使用的一个ListView,下面使用的是一个自定义的Button(会在下面进行 ...
- android 仿微信联系人 首字母分组快速索引
总结是一种习惯,不能停,一停人就懒了,都快一个月没有写了!该提提神了! 进入正题:android 仿微信联系人 首字母快速索引,先用下美团的索引效果图: 1.自定义View字母索引栏(右边那一列): ...
- Android App列表之游标ListView(索引ListView)
原帖地址:http://www.apkbus.com/android-14717-1-1.html 游标ListView,提供索引标签,使用户能够快速定位列表项. 也可以叫索引ListVi ...
- Android 实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音
转载请注明出处:http://blog.csdn.net/xiaanming/article/details/12684155 前段时间因为换工作的缘故又恰巧碰到国庆节,所以有段时间自己没有更新博客了 ...
- Android实现ListView的A-Z字母排序和过滤搜索功能
原文地址: http://blog.csdn.net/xiaanming/article/details/12684155 首先先看下效果图 上面是一个带删除按钮的EditText,我们在输入框中 ...
- Android实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音
转载请注明出处:http://blog.csdn.net/xiaanming/article/details/12684155 前段时间因为换工作的缘故又恰巧碰到国庆节,所以有段时间自己没有更新博客了 ...
- [Android精品源码] Android 仿美团网,探索ListView的A-Z字母排序功能实现选择城市
Material Design中文版Code4APPPHP100UI4APP 开启辅助访问设为首页收藏本站快捷导航切换到宽版切换风格 石刚 | |我的 |签到打卡 |设置 |消息 |提醒(2) |退出 ...
最新文章
- jemter接口并发数怎么算_JMeter学习使用(1) - 接口GET请求并发测试
- 域与活动目录(下) windows server 2008
- (转载)MultiAnimation
- 修改thinkphp配置nginx服务器,thinkphp5.0配置nginx重写规则
- CentOS 7.4 基于LNMP搭建wordpress
- Zend_Feed 的项目实际应用
- float.equals_Java Float类equals()方法与示例
- html5和flash播放器
- ant 发布web应用脚本
- egg服务重启及child_process的使用
- 汇编语言指令大全(详细)
- ps怎么更改背景图层大小_PS怎么修改图片尺寸大小?ps修改图片大小方法图解
- 多项式乘法 快速傅里叶变换
- tp6多表联合查询的几种方式(模糊搜索+分页+字段限制)
- html全景直播播放器,Insta360 Player(全景视频播放器) V2.3.6 官方版
- layui结合json viewer实现代码格式化
- 做人如水 做事如山
- 基于JAVA的TCP网络QQ聊天工具系统
- Java设计模式之外观模式
- ReentrantLock源码走读分析