import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class IndexView extends View {
private Paint paint = new Paint();
private OnTouchCallback callback;
private boolean showBg = false;
private int choose = -1;
public static String[] letters = {"#", "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 width;
private int height;
private int textSize = 20;
private int textColor = Color.BLACK;
private int selectTextColor = Color.YELLOW;
private int selectBackGround = Color.LTGRAY;
private int singleHeight;
public IndexView(Context context) {
this(context, null);
}
public IndexView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public IndexView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public IndexView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.IndexView);
textSize = (int) array.getDimension(R.styleable.IndexView_indexTextSize, textSize);
textColor = array.getColor(R.styleable.IndexView_indexTextColor, textColor);
selectTextColor = array.getColor(R.styleable.IndexView_indexSelectTextColor, selectTextColor);
selectBackGround = array.getColor(R.styleable.IndexView_indexSelectBackGround, selectBackGround);
initPaint();
array.recycle();
}
private void initPaint() {
paint.setColor(textColor);
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setAntiAlias(true);
paint.setTextSize(textSize);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getMeasuredWidth();
height = getMeasuredHeight();
singleHeight = height / letters.length;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (showBg) {
// 畫出背景
canvas.drawColor(selectBackGround);
}
// 畫字母
for (int i = 0; i < letters.length; i++) {
// 若是這一項被選中,則換一種顏色畫
if (i == choose) {
paint.setColor(selectTextColor);
paint.setFakeBoldText(true);
} else {
paint.setColor(textColor);
paint.setFakeBoldText(false);
}
// 要畫的字母的x,y座標
float posX = width / 2 - paint.measureText(letters[i]) / 2;
float posY = i * singleHeight + singleHeight;
// 畫出字母
canvas.drawText(letters[i], posX, posY, paint);
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
final float y = event.getY();
final int index = (int) (y / getHeight() * letters.length);
final int oldChoose = choose;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
showBg = true;
if (oldChoose != index
&& callback != null
&& index > 0
&& index < letters.length) {
choose = index;
callback.onTouchChange(showBg, letters[index]);
invalidate();
}
if (textView != null) {
textView.setVisibility(VISIBLE);
}
break;
case MotionEvent.ACTION_MOVE:
if (oldChoose != index
&& callback != null
&& index > 0
&& index < letters.length) {
choose = index;
callback.onTouchChange(showBg, letters[index]);
invalidate();
if (textView != null) {
textView.setText(letters[index]);
}
}
break;
case MotionEvent.ACTION_UP:
showBg = false;
choose = -1;
if (callback != null) {
if (index <= 0) {
callback.onTouchChange(showBg, "A");
} else if (index > 0 && index < letters.length) {
callback.onTouchChange(showBg, letters[index]);
} else if (index >= letters.length) {
callback.onTouchChange(showBg, "Z");
}
}
if (textView != null) {
textView.setVisibility(GONE);
}
invalidate();
break;
}
return true;
}
private TextView textView;
public void setText(TextView view) {
this.textView = view;
}
public void setOnTouchCallback(OnTouchCallback callback) {
this.callback = callback;
}
public interface OnTouchCallback {
void onTouchChange(boolean isTouched, String s);
}
public int getSelectBackGround() {
return selectBackGround;
}
public void setSelectBackGround(int selectBackGround) {
this.selectBackGround = selectBackGround;
invalidate();
}
public int getSelectTextColor() {
return selectTextColor;
}
public void setSelectTextColor(int selectTextColor) {
this.selectTextColor = selectTextColor;
invalidate();
}
public int getTextColor() {
return textColor;
}
public void setTextColor(int textColor) {
this.textColor = textColor;
invalidate();
}
public int getTextSize() {
return textSize;
}
public void setTextSize(int textSize) {
this.textSize = textSize;
invalidate();
}
}
<resources>
<declare-styleable name="IndexView">
<attr name="indexTextSize" format="dimension" />
<attr name="indexTextColor" format="color" />
<attr name="indexSelectTextColor" format="color"/>
<attr name="indexSelectBackGround" format="color"/>
</declare-styleable>
</resources>