您的位置:首页 > 其它

自定义view-横向快速索引

2018-02-22 09:08 197 查看
自定义view类:

public class MyCustomSearch extends View {
private static final String[] WORDS = {"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 static final String[] WORDSTWO = {"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"};
private float cellheight;
private float cellwidth;
int lastpositon = -1;
private int measuredHeight;
private int measuredWidth;
private MyOnClickListener myOnClickListener;
private Paint paint;
private String word;
private float x;
private float y;
protected static final int DEFAULT_RADIUS = 150;
private float dimension;

public MyCustomSearch(Context paramContext) {
this(paramContext, null);
}

public MyCustomSearch(Context paramContext, @Nullable AttributeSet paramAttributeSet) {
this(paramContext, paramAttributeSet, 0);
}

public MyCustomSearch(Context paramContext, @Nullable AttributeSet paramAttributeSet, int paramInt) {
super(paramContext, paramAttributeSe
11e69
t, paramInt);
TypedArray a = paramContext.getTheme().obtainStyledAttributes(paramAttributeSet, R.styleable.mycustomatrrs, paramInt, 0);
dimension = a.getDimension(R.styleable.mycustomatrrs_textSize,
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_RADIUS,
getResources().getDisplayMetrics()));
Log.d("TAG", dimension + "");
//初始化
initView();
}

private void initView() {
paint = new Paint();
paint.setAntiAlias(true);//设置抗锯齿
paint.setColor(Color.BLACK);//设置字体颜色
paint.setTextSize(dimension);//设置字体大小
paint.setFakeBoldText(true);
}

@Override
protected void onMeasure(int paramInt1, int paramInt2) {
super.onMeasure(paramInt1, paramInt2);
//获取该控件的宽高
measuredWidth = getMeasuredWidth();
measuredHeight = getMeasuredHeight();
//获取单元格的宽高
cellwidth = (this.measuredWidth * 1.0F / 16);
cellheight = (this.measuredHeight * 0.5f);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//定义一个变量用来绘制第二行,让其从第一个开始依次绘制
int a = 0;
for (int i = 0; i < WORDS.length; i++) {
word = WORDS[i];
if (i >= 16) {
x = (cellwidth / 2 - paint.measureText(word) / 2 + a * cellwidth);
y = (cellheight + cellheight / 2 + paint.descent() / 2);
a++;
} else {
x = (cellwidth / 2 - paint.measureText(word) / 2 + i * cellwidth);
y = (cellheight / 2 + paint.descent() / 2);
}

canvas.drawText(word, x, y, paint);

}
}

@Override
public boolean onTouchEvent(MotionEvent event) {
String word = null;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = (int) event.getX();
y = (int) event.getY();
int touchposition = (int) (x / cellwidth);
//为了防止多次点击字母重复
if (touchposition != lastpositon) {
//为了防止下标越界,将可点击的范围设置在数组长度之内
if (touchposition >= 0 && touchposition < WORDS.length) {
if (y > cellheight && touchposition < WORDSTWO.length) {
word = WORDSTWO[touchposition];
} else {
word = WORDS[touchposition];
}
}
if (myOnClickListener != null) {
myOnClickListener.mOnClick(word);
}
lastpositon = touchposition;
}
break;
case MotionEvent.ACTION_MOVE:
x = (int) event.getX();
y = (int) event.getY();
int touchposition1 = (int) (x / cellwidth);
//为了防止多次点击字母重复
if (touchposition1 != lastpositon) {
//为了防止下标越界,将可点击的范围设置在数组长度之内
if (touchposition1 >= 0 && touchposition1 < WORDS.length) {
if (y > cellheight && touchposition1 < WORDSTWO.length) {
word = WORDSTWO[touchposition1];
} else {
word = WORDS[touchposition1];
}
}
if (myOnClickListener != null) {
myOnClickListener.mOnClick(word);
}
lastpositon = touchposition1;
}
break;
}
return true;
}

public void setMyOnClickListener(MyOnClickListener paramMyOnClickListener) {
this.myOnClickListener = paramMyOnClickListener;
}

public interface MyOnClickListener {
void mOnClick(String paramString);
}
}

使用:
<com.bawie.yodo.custom.MyCustomSearch
android:id="@+id/brand_custom"
mycustomatrrs:textSize="@dimen/x14"
android:layout_width="match_parent"
android:layout_height="75dp" />
自定义属性atrrs:
<resources>
<declare-styleable name="mycustomatrrs">
<attr name="textSize" format="dimension"></attr>
</declare-styleable>
</resources>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: