importandroid.content.Context;importandroid.graphics.Canvas;importandroid.graphics.Paint;importandroid.support.annotation.Nullable;importandroid.util.AttributeSet;importandroid.view.MotionEvent;importandroid.view.View;importcom.example.z.xxxx.R;/*** Created by z on 2017/9/21.*/
public class SlideBar extendsView {private static final String[] BAR_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 Paint paint = newPaint();private int selectIndex = -1;//选项的数组下标
privateonTouchLetterListener onTouchLetterListener;private boolean isShowBkg = false;//背景是否变化
publicSlideBar(Context context) {super(context);
}publicSlideBar(Context context, @Nullable AttributeSet attrs) {super(context, attrs);
}public SlideBar(Context context, @Nullable AttributeSet attrs, intdefStyleAttr) {super(context, attrs, defStyleAttr);
}public interfaceonTouchLetterListener {void onTouchLetterChange(String letter,booleantouch);
}public voidsetOnTouchLetterListener(onTouchLetterListener onTouchLetterListener) {this.onTouchLetterListener =onTouchLetterListener;
}
@Overrideprotected voidonDraw(Canvas canvas) {super.onDraw(canvas);if (isShowBkg == true) {
canvas.drawColor(getResources().getColor(R.color.text_backgroud_color));//显示背景
}int height =getHeight();int width =getWidth();int singleHeight = (height - 20) / BAR_LETTERS.length;//每个字母占得高度
System.out.println(height + "-------" +width);for (int i = 0; i < BAR_LETTERS.length; i++) {//遍历字母
paint.setColor(getResources().getColor(R.color.text_color));
paint.setTextSize(50);
paint.setAntiAlias(true);//设置抗锯齿样式
System.out.println(i+"-----"+selectIndex);if (i == selectIndex) {//如果被选中
System.out.println(i+"*****"+selectIndex);
paint.setColor(getResources().getColor(R.color.isread_true));
paint.setFakeBoldText(true);//加粗
}//从左下角开始绘制
float xpos = (width - paint.measureText(BAR_LETTERS[i])) / 2;//x轴
float ypos = singleHeight * i + singleHeight;//y轴 (i从0开始算)//System.out.println(xpos + "" + ypos);
canvas.drawText(BAR_LETTERS[i], xpos, ypos, paint);//开始绘制
paint.reset();//重置画笔
}
}
@Overridepublic booleandispatchTouchEvent(MotionEvent event) {int action =event.getAction();float ypos =event.getY();int oldSelectIndex = selectIndex;//上一次触发的坐标
int newSelectIndex = (int) (ypos / getHeight() * BAR_LETTERS.length);//根据触摸的位置确定点的哪个字母
switch(action) {caseMotionEvent.ACTION_DOWN:
isShowBkg= true;if (onTouchLetterListener != null && oldSelectIndex !=newSelectIndex&& newSelectIndex >= 0 && newSelectIndex <=BAR_LETTERS.length) {
onTouchLetterListener.onTouchLetterChange(BAR_LETTERS[newSelectIndex],true);//注册点击事件并传入字母
oldSelectIndex =newSelectIndex;
selectIndex=newSelectIndex;
invalidate();//重新绘制
}break;caseMotionEvent.ACTION_UP:
isShowBkg= false;
selectIndex= -1;
invalidate();
onTouchLetterListener.onTouchLetterChange(BAR_LETTERS[newSelectIndex],false);//注册点击事件并传入字母
break;caseMotionEvent.ACTION_MOVE:if (onTouchLetterListener != null && oldSelectIndex !=newSelectIndex&& newSelectIndex >= 0 && newSelectIndex <=BAR_LETTERS.length) {
onTouchLetterListener.onTouchLetterChange(BAR_LETTERS[newSelectIndex],true);//注册点击事件并传入字母
oldSelectIndex =newSelectIndex;
selectIndex=newSelectIndex;
invalidate();//重新绘制
}break;
}return true;
}
@Overridepublic booleanonTouchEvent(MotionEvent event) {return super.onTouchEvent(event);
}
}