本篇文章主要介绍了"手机联系人快速索引",主要涉及到gravity,索引,RelativeLayou,Canvas,Activity方面的内容,对于移动开发感兴趣的同学可以参考一下:
这是一个手机联系人快速索引的效果,总体来说代码不算难,拼音转换的地方略有复杂。下面上源码:源码中有注释。MainActivity:import java.uti...
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
* 这里是自定义View
* @author lxd
*
*/
public class QuickIndexView extends View {
private float itemWidth;
private float itemHeight;
// private float wordWidth;
// private float wordHeight;
private String[] indexArr = { "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;
public QuickIndexView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(16);
paint.setAntiAlias(true);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
itemWidth = this.getMeasuredWidth();
itemHeight = this.getMeasuredHeight() / 26f;
}
@Override
protected void onDraw(Canvas Canvas) {
//当每次触发重绘的时候,就把26个字母循环一遍
for (int i = 0; i < indexArr.length; i++) {
String word = indexArr[i];
// 设置文字的颜色
if (i == touchIndex) {
//这里设置被点击的字母变化:颜色变灰色、字体变25sp
paint.setColor(Color.GRAY);
paint.setTextSize(25);
} else {
//其他没被点击的字母,保持原有状态:设置颜色、字体大小为18sp
paint.setColor(Color.BLACK);
paint.setTextSize(18);
}
// 得到word的宽高
Rect bounds = new Rect();
paint.getTextBounds(word, 0, word.length(), bounds);
//得到字体的宽
int wordWidth = bounds.width();
//得到字体的高
int wordHeight = bounds.height();
// 计算word的左上角的坐标:字母所在的X坐标、Y坐标
float x = itemWidth / 2 - wordWidth / 2;
float y = itemHeight / 2 + wordHeight / 2 + i * itemHeight;
// 绘制word
Canvas.drawText(word, x, y, paint);
}
}
// ///////////////////////////////////////////////////////////////////////
private int touchIndex = -1;// 触摸的字母的下标
@Override
public boolean onTouchEvent(MotionEvent event) {
// 得到事件坐标
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
// 计算下标
int index = (int) (eventY / itemHeight);
if (index > 25) {
index = 25;
}
if (index < 0) {
index = 0;
}
// 如果下标有改变, 强制重绘
if (index != touchIndex) {
// 更新touchIndex
touchIndex = index;
// 强制重绘
invalidate();
// 通知Activity更新TextView
if (onIndexChangedListener != null) {
onIndexChangedListener.onIndexChanged(indexArr[index]);
}
}
break;
case MotionEvent.ACTION_UP:
touchIndex = -1;
// 强制重绘
invalidate();
// 通知Activity更新TextView
if (onIndexChangedListener != null) {
onIndexChangedListener.onUp();
}
break;
default:
break;
}
return true;// 所有的事件都由当前视图消费
}
private OnIndexChangedListener onIndexChangedListener;
/*
* 设置监听对象的方法 这个方法一般是Activity调用
*/
public void setOnIndexChangedListener(
OnIndexChangedListener onIndexChangedListener) {
this.onIndexChangedListener = onIndexChangedListener;
}
interface OnIndexChangedListener {
// 当操作的下标改变时自动调用
public void onIndexChanged(String word);
// 当up时调用
public void onUp();
}
}
联系人类:
/**
* 联系人类
* @author lxd
*
*/
public class Friend implements Comparable {
private String name;
private String pinyin;
public Friend(String name) {
super();
this.name = name;
pinyin = PinYinUtils.getPinYin(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPinyin() {
return pinyin;
}
public void setPinyin(String pinyin) {
this.pinyin = pinyin;
}
@Override
public String toString() {
return "Friend [name=" + name + ", pinyin=" + pinyin + "]";
}
@Override
public int compareTo(Friend another) {
return this.pinyin.compareTo(another.getPinyin());
}
}
工具类:用于将汉字转换为拼音
/**
* 将汉字转换为拼音
* @author lxd
*
*/
public class PinYinUtils {
/**
* 得到指定汉字的拼音
* 注意:不应该被频繁调用,它消耗一定内存
* @param hanzi
* @return
*/
public static String getPinYin(String hanzi){
String pinyin = "";
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();//控制转换是否大小写,是否带音标
format.setCaseType(HanyuPinyinCaseType.UPPERCASE);//大写
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
//由于不能直接对多个汉字转换,只能对单个汉字转换
char[] arr = hanzi.toCharArray();
for (int i = 0; i < arr.length; i++) {
if(Character.isWhitespace(arr[i]))continue;//如果是空格,则不处理,进行下次遍历
//汉字是2个字节存储,肯定大于127,所以大于127就可以当为汉字转换
if(arr[i]>127){
try {
//由于多音字的存在,单 dan shan
String[] pinyinArr = PinyinHelper.toHanyuPinyinStringArray(arr[i], format);
if(pinyinArr!=null){
pinyin += pinyinArr[0];
}else {
pinyin += arr[i];
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
//不是正确的汉字
pinyin += arr[i];
}
}else {
//不是汉字,
pinyin += arr[i];
}
}
return pinyin;
}
}
运行效果:

以上就是全部代码了,需要的同学可以直接拿去用,样式的话根据需要自己改下就行了。
以上就介绍了手机联系人快速索引,包括了gravity,索引,RelativeLayou,Canvas,Activity方面的内容,希望对移动开发有兴趣的朋友有所帮助。
本文网址链接:http://www.codes51.com/article/detail_251324_2.html