Android快速索引條控件
效果展示
代碼實現
自定義view,畫出靜態的圖形
在canvas上指定好位置,用各種畫筆Paint,重要的是分析的過程。
private Paint paint;
private int cellWidth, cellHeight;
private void init() {
paint = new Paint();
paint.setTextSize(40);
paint.setColor(Color.WHITE);
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (cellHeight == 0) cellHeight = getMeasuredHeight() / indexs.length;
if (cellWidth == 0) cellWidth = getMeasuredWidth();
for (int i = 0; i < indexs.length; i++) {
Rect bounds = new Rect();
paint.getTextBounds(indexs[i], 0, indexs[i].length(), bounds);
float x = cellWidth / 2 - paint.measureText(indexs[i]) / 2;
float y = cellHeight / 2 + bounds.height() / 2 + i * cellHeight;
canvas.drawText(indexs[i], x, y, paint);
}
}
實現onTouch事件,完成動態效果
private int lastIndex = -1;
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
if (isSameIndex(y / cellHeight)) break;
String word = indexs[((int) (y / cellHeight))];
lastIndex = y / cellHeight;
if (mIndexChangedListener != null) {
mIndexChangedListener.indexChanged(word);
}
break;
case MotionEvent.ACTION_UP:
lastIndex = -1;
break;
default:
break;
}
return true;
}
/**
* 當前的索引位置是否和上一個相等
*
* @return
*/
private boolean isSameIndex(int currIndex) {
return lastIndex == currIndex;
}
private IndexChangedListener mIndexChangedListener;
public void setIndexChangedListener(IndexChangedListener indexChangedListener) {
mIndexChangedListener = indexChangedListener;
}
public interface IndexChangedListener {
void indexChanged(String word);
}
給觸摸事件添加更加人性化的交互
QuickIndexBar交互效果的實現
在處理onTouch事件時,改變畫筆的設置,畫出不同的圖案。
在onTouch中調用
invalidate();
在onDraw中增加下面的邏輯:
畫每個元素之前先設置不同的畫筆。
if (i == lastIndex) {
paint.setTextSize(60);
paint.setColor(Color.GRAY);
} else {
paint.setTextSize(40);
paint.setColor(Color.WHITE);
}
中間放大框的實現
當前Activity接受到QuickIndexBar的變化事件後,調用showIndexLabel方法,通過handler完成延遲操作。
private Handler mHandler = new Handler();
private void showIndexLabel(String word) {
mLabel.setVisibility(View.VISIBLE);
mLabel.setText(word);
mHandler.removeCallbacksAndMessages(null);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mLabel.setVisibility(View.GONE);
}
}, DEFAULT_DELAY);
}
完整代碼
github