package com.itbox.grzl.widget; import com.itbox.fx.core.Application; import com.zhaoliewang.grzl.R; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Typeface; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class LetterListView extends View { OnTouchingLetterChangedListener onTouchingLetterChangedListener; String[] b = { "#", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z" }; // 选中 int choose = -1; Paint paint = new Paint(); boolean showBkg = false;// 是否显示背景 public LetterListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public LetterListView(Context context, AttributeSet attrs) { super(context, attrs); } public LetterListView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 获取焦点改变字母竖条背景颜色 // if (showBkg) { // canvas.drawColor(Color.parseColor("#40000000")); // } // 获取高度 int height = getHeight(); // 获取宽度 int width = getWidth(); // 获取每一个字母的高度 int singleHeight = height / b.length; for (int i = 0; i < b.length; i++) { // 设置未选中字的颜色 paint.setColor(Color.WHITE); paint.setTextSize(Application.getRes().getDimension(R.dimen.textsize_copyright)); paint.setTypeface(Typeface.DEFAULT_BOLD); paint.setAntiAlias(true); // 选中状态 if (i == choose) { // 选中状态下字颜色 paint.setColor(Application.getRes().getColor(R.color.blue_text)); paint.setFakeBoldText(true); } float xPos = width / 2 - paint.measureText(b[i]) / 2; float yPos = singleHeight * i + singleHeight; canvas.drawText(b[i], xPos, yPos, paint); // 重置画笔 paint.reset(); } } @Override public boolean dispatchTouchEvent(MotionEvent event) { final int action = event.getAction(); // 点击y坐标 final float y = event.getY(); final int oldChoose = choose; final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener; // 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数. final int c = (int) (y / getHeight() * b.length); switch (action) { case MotionEvent.ACTION_DOWN: showBkg = true; if (oldChoose != c && listener != null) { if (c > 0 && c < b.length) { listener.onTouchingLetterChanged(b[c]); choose = c;// 选项 invalidate();// 重绘 } } break; case MotionEvent.ACTION_MOVE: if (oldChoose != c && listener != null) { if (c >= 0 && c < b.length) { listener.onTouchingLetterChanged(b[c]); choose = c; invalidate(); } } break; case MotionEvent.ACTION_UP: showBkg = false; choose = -1; invalidate(); break; } return true; } public void setOnTouchingLetterChangedListener( OnTouchingLetterChangedListener onTouchingLetterChangedListener) { this.onTouchingLetterChangedListener = onTouchingLetterChangedListener; } public interface OnTouchingLetterChangedListener { public void onTouchingLetterChanged(String s); } }