QuickIndex 通讯录快速检索
生活随笔
收集整理的這篇文章主要介紹了
QuickIndex 通讯录快速检索
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
QuickIndex 通訊錄快速檢索
歡迎入群交流
public class QuickIndexBar extends View {private String[] indexArr = {"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"};Paint paint = null;int ColorDefault = Color.GRAY;//默認顏色int ColorPressed = Color.BLUE;//按下顏色public QuickIndexBar(Context context) {this(context, null);}public QuickIndexBar(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public QuickIndexBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);paint = new Paint(Paint.ANTI_ALIAS_FLAG);//設置抗鋸齒paint.setColor(ColorDefault);int size = getResources().getDimensionPixelSize(R.dimen.text_size);paint.setTextSize(size);//文字繪制的默認起點是文字的左下角,CENTER表示底邊的中心,而不是正中心paint.setTextAlign(Paint.Align.CENTER);//Baseline:基準線}float cellHeight;//一個格子的高度@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);cellHeight = getMeasuredHeight() * 1f / indexArr.length;}@Overrideprotected void onDraw(Canvas canvas) {//遍歷繪制26個字母for (int i = 0; i < indexArr.length; i++) {String text = indexArr[i];float x = getMeasuredWidth() / 2;//整個寬的一半//格子高度一半+文字高度一半+i*格子高度int textHeight = getTextHeight(text);float y = cellHeight / 2 + textHeight / 2 + i * cellHeight;//變色paint.setColor(i == index ? ColorPressed : ColorDefault);canvas.drawText(text, x, y, paint);}}int index = -1;//用來記錄觸摸的索引的@Overridepublic boolean onTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:case MotionEvent.ACTION_MOVE:int temp = (int) (event.getY() / cellHeight);if (temp != index) {index = temp;//對index進行一下安全性的檢查if (index >= 0 && index < indexArr.length) {String word = indexArr[index];if (listener != null) {listener.onLetterChange(word);}}}break;case MotionEvent.ACTION_UP://抬起重置變量index = -1;break;}//重繪invalidate();return true;}/*** 獲取文字的高度** @param text* @return*/private int getTextHeight(String text) {Rect bounds = new Rect();paint.getTextBounds(text, 0, text.length(), bounds);//對bounds進行賦值return bounds.height();}private OnLetterChangeListener listener;public void setOnLetterChangeListener(OnLetterChangeListener listener) {this.listener = listener;}public interface OnLetterChangeListener {void onLetterChange(String word);}}微信公眾號(他曉),關注并轉發,謝謝
總結
以上是生活随笔為你收集整理的QuickIndex 通讯录快速检索的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 生产跟踪(生产过程跟单)
- 下一篇: nginx-proxy-manager在