该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
贴部分代码
private void init(){
setBackgroundColor(unTouchBackground);
//得到矩形的长宽
rectHeight = getHeight()/codes.length;
rectWidth = getWidth();
//字母大小根据长宽,为短的的一半
codeSize = rectHeight < rectWidth ? rectHeight / 2 : rectWidth / 2;
//设置画笔属性
paint = new Paint();
paint.setColor(codeColor);
paint.setTextSize(codeSize);
//没选择时为-1
selectedCode = -1;
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(onCodeChangeListener != null) {
onCodeChangeListener.codeChange(codes[msg.what]);
}
}
};
setOnTouchListener(this);
}
protected void onDraw(Canvas canvas) {
this.init();
for(int i = 0;i < codes.length;i++){
canvas.drawText(codes[i],(rectWidth - codeSize) / 2,
rectHeight * i + (rectHeight - codeSize), paint);
}
Log.e("aaa","viewHeight = " + getHeight() + ",codeHeight = " + (codeSize + rectHeight * codes.length));
}
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
//移动或者按下的时候
if(action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE){
float y = event.getY();
if(!isPressed){
setBackgroundColor(touchBackground);
}
isPressed = true;
nowSelectedCode = Float.valueOf(y / rectHeight).intValue();
if(nowSelectedCode < 0){
nowSelectedCode = 0;
}
if(nowSelectedCode > codes.length - 1){
nowSelectedCode = codes.length - 1;
}
if(selectedCode == nowSelectedCode){
return true;
}else{
selectedCode = nowSelectedCode;
handler.sendEmptyMessage(nowSelectedCode);
}
}
if(action == MotionEvent.ACTION_UP){
setBackgroundColor(unTouchBackground);
isPressed = false;
selectedCode = -1;
if(onCodeChangeListener != null) {
onCodeChangeListener.codeSelected();
}
}
return true;
}