クイッククエリーAからZ
4814 ワード
/**
* View
* @author Jiang
*
*/
public class MyQuickSearchView extends View {
/**
*
* @param context
*/
public MyQuickSearchView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyQuickSearchView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyQuickSearchView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
//
private OnTouchingLetterChangedListener onTouchingLetterChangedListener = null;
//
private String[] mAllChar = { "#", "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 mChoose = -1;
private Paint mPaint = new Paint();
private boolean isShowBackground = false;
/*
* (non-Javadoc)
* @see android.view.View#onDraw(android.graphics.Canvas)
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (null == mPaint) {
return;
}
float height = getHeight();
float width = getWidth();
if (isShowBackground) {
// ,
RectF rect = new RectF(0, 0, width, height);
mPaint.setColor(Color.parseColor("#40000000"));
canvas.drawRoundRect(rect, width / 2, width / 2, mPaint);
}
float singleHeight = height / mAllChar.length;
for (int i = 0; i < mAllChar.length; i++) {
//
mPaint.setColor(getResources().getColor(R.color.color_small_text));
//
mPaint.setTypeface(Typeface.DEFAULT);
// ,
mPaint.setTextSize(singleHeight);
//
mPaint.setAntiAlias(true);
if (i == mChoose) {
mPaint.setColor(Color.RED);
mPaint.setFakeBoldText(true);
}
// ,
float xPos = width / 2 - mPaint.measureText(mAllChar[i]) / 2;
//
float yPos = singleHeight * i + singleHeight;
//
canvas.drawText(mAllChar[i], xPos, yPos, mPaint);
mPaint.reset();
}
}
/*
* (non-Javadoc)
* @see android.view.View#dispatchTouchEvent(android.view.MotionEvent)
*/
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
final int action = event.getAction();
final float y = event.getY();
final int oldChoose = mChoose;
final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
//
final int position = (int) (y / getHeight() * mAllChar.length);
switch (action) {
case MotionEvent.ACTION_DOWN:
isShowBackground = true;
if (null != listener && oldChoose != position) {
if (position > 0 && position < mAllChar.length) {
listener.onTouchingLetterChanged(mAllChar[position]);
mChoose = position;
invalidate();
}
}
break;
case MotionEvent.ACTION_MOVE:
if (oldChoose != position && listener != null) {
if (position > 0 && position < mAllChar.length) {
listener.onTouchingLetterChanged(mAllChar[position]);
mChoose = position;
invalidate();
}
}
break;
case MotionEvent.ACTION_UP:
isShowBackground = false;
mChoose = -1;
invalidate();
break;
}
return true;
}
/*
* (non-Javadoc)
* @see android.view.View#onTouchEvent(android.view.MotionEvent)
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
/**
*
* @param onTouchingLetterChangedListener
*/
public void setOnTouchingLetterChangedListener(OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
}
/**
* listener interface
* @author Jiang
*
*/
public interface OnTouchingLetterChangedListener {
public void onTouchingLetterChanged(String s);
}
}