import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
public class LetterView extends View {
//private NinePatchDrawable localBitmap = null;
private Drawable bg;
private static final int SPACE_RIGHT = 15;//padding left and right
private Align align = Align.CENTER;
private int count = 27;//字母个数 ;
public LetterView(Context context) {
super(context);
bg = context.getResources().getDrawable(R.drawable.reg_tel_letter_color);
}
public LetterView(Context context, AttributeSet attrs) {
super(context, attrs);
bg = context.getResources().getDrawable(R.drawable.reg_tel_letter_color);
}
public LetterView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
bg = context.getResources().getDrawable(R.drawable.reg_tel_letter_color);
}
public float getLetterHeight()
{
float h = getHeight() - 10;
float textSize = h * 5 / 6 / count;
float spaceH = (h - count * textSize) / (count - 1);//vertical margin
return textSize+spaceH;
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(0xff6d7276);
paint.setStyle(Style.FILL);
float h = getHeight() - 10;
float textSize = h * 5 / 6 / count;
paint.setTextSize(textSize);
paint.setTextAlign(align);
float spaceH = (h - count * textSize) / (count - 1);
if (isTouch) {
bg.setBounds(new Rect(getWidth() - (int) textSize - 2*SPACE_RIGHT, 0, getWidth(), getHeight()));
bg.draw(canvas);
}
String drawText = null;
for (int i = 0; i < 27; i++) {
if (i == 26) {
drawText = "#";
} else {
drawText = String.valueOf((char) ('A' + (i)));
}
canvas.drawText(drawText, getWidth() - textSize / 2 - SPACE_RIGHT+5, (i + 1) * (textSize + spaceH),
paint);
}
if (isTouch) {
invalidate(new Rect(getWidth() - (int) textSize - 2*SPACE_RIGHT, 0, getWidth(),getHeight()));
}
}
private boolean isTouch = false;
public void setTouch(boolean isTouch) {
this.isTouch = isTouch;
}
}
letterView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
float y = event.getY();
height = letterView.getLetterHeight();
int index = (int) (y / height);
if (index > -1 && index < alpha.length) {// 防止越界
String key = alpha[index];
if (mapIndex.containsKey(key)) {
int pos = mapIndex.get(key);
if (countryView.getHeaderViewsCount() > 0) {// 防止ListView有标题栏
countryView.setSelectionFromTop(pos + countryView.getHeaderViewsCount(), 0);
} else {
countryView.setSelectionFromTop(pos, 0);// 滑动到第一项
}
}
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
letterView.setBackgroundColor(Color.parseColor("#606060"));
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
letterView.setBackgroundColor(Color.parseColor("#00ffffff"));
break;
}
return true;
}
});