package com.eage.tbw.view;
import com.eage.tbw.R;
import com.eage.tbw.R.color;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
/**
A-Z字母的View
lxm
*/
public class LetterView extends View {
private static final String[] LETTER=new String[]{"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;
private int textSize;
private int choice;
private TextView outerTV;
private LetterOnTouchCallBack letterOnTouchCallBack;
public void setTextView(TextView textView){
outerTV=textView;
}
public LetterView(Context context) {
this(context, null);
}
public LetterView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LetterView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray=context.getTheme().obtainStyledAttributes(attrs, R.styleable.LetterView, defStyleAttr, 0);
textSize=typedArray.getInt(R.styleable.LetterView_text, 27);
typedArray.recycle();
initView();
}
private void initView() {
paint=new Paint();
paint.setTextSize(textSize);
paint.setColor(color.custom_yellow);
paint.setAntiAlias(true);
choice=-1;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int height=getHeight();
int width=getWidth();
int length=LETTER.length;
int itemHeigth=height/length;
for(int i=0;i<length;i++){
if(i==choice-1){
paint.setColor(color.custom_yellow);
paint.setTextSize(32);
}else{
paint.setColor(color.custom_yellow);
paint.setTextSize(textSize);
}
float y=(i+1)*itemHeigth;
float x=(width-paint.measureText(LETTER[i]))/2;
canvas.drawText(LETTER[i], x, y, paint);
}
}
//重写触摸事件
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
final LetterOnTouchCallBack callBack = letterOnTouchCallBack;
float y=event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
choice=-1;
if(outerTV!=null){
outerTV.setVisibility(View.GONE);
}
setBackgroundColor(Color.WHITE);
invalidate();
break;
default:
int length=LETTER.length;
int position= (int) (y/getHeight()*LETTER.length);
setBackgroundColor(Color.parseColor("#E5E5E5"));
if(1<=position&&position<=length){
//将选中的字母传回到MainActivity中
if(callBack!=null){
callBack.onTouchingLetterChanged(LETTER[position-1]);
}
if(outerTV!=null){
outerTV.setText(LETTER[position-1]);
outerTV.setVisibility(View.VISIBLE);
}
choice=position;
invalidate();
}
break;
}
return true;
}
// 设置监听
public void setOnLetterCallBack(LetterOnTouchCallBack letterOnTouchCallBack) {
this.letterOnTouchCallBack = letterOnTouchCallBack;
}
//接口
public interface LetterOnTouchCallBack {
public void onTouchingLetterChanged(String s);
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。