自定义快速导航条

/*自定义快速导航条,实现微信通讯录点击字幕跳转到联系人效果/
package com.woojn.quickindexdemo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

/**
* Created by woojn on 2016/11/8.
*/

public class QuickIndexBar extends View {

/**
 * 索引字母颜色
 */
private static final int LETTER_COLOR = 0xff595959;

/**
 * 字母索引条背景颜色
 */
private static final int BG_COLOR = 0xffB0B0B0;

/**
 * 26个字母
 */
public static final String[] LETTERS = {
        "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 mPaint;
private int mHeight;
private int mWidth;
private float mCellHeight;



public QuickIndexBar(Context context) {
    this(context, null);
}

public QuickIndexBar(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

private void init() {
    mPaint = new Paint();
    mPaint.setTextSize(dp2px(16));
    mPaint.setColor(LETTER_COLOR);
    mPaint.setAntiAlias(true);//防锯齿
}

@Override
protected void onDraw(Canvas canvas) {
    //绘制26个之母
    for (int i = 0; i < LETTERS.length; i++) {
        String letter = LETTERS[i];
        // x = mWidth/2 - 字母宽度/2
        // y = mCellHeight/2 + 字母高度/2
        //          + mCellHeight * k(k的值为0,1,2...)
        float x = mWidth / 2 - getTextWidth(letter) / 2;
        float y = mCellHeight / 2 + getTextHeight(letter) / 2 + mCellHeight * i;
        canvas.drawText(letter, x, y, mPaint);

    }
}

/**
 * 获取字符串的宽高
 */
public int getTextWidth(String text) {
    Rect bounds = new Rect();
    mPaint.getTextBounds(text, 0, text.length(), bounds);
    return bounds.width();
}

public int getTextHeight(String text) {
    Rect bounds = new Rect();
    mPaint.getTextBounds(text, 0, text.length(), bounds);
    return bounds.height();
}

/**
 * 获取控件的宽高
 */
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    mHeight = getMeasuredHeight();
    mWidth = getMeasuredWidth();
    mCellHeight = (1.0f * mHeight) / 26;
}

private int index;

/**
 * 点击触摸字母逻辑处理
 */
@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            //按下时颜色改变
            setBackgroundDrawable(new ColorDrawable(BG_COLOR));
            index = (int) (event.getY() / mCellHeight);//获取第几个字母
            if (index >= 0 && index < LETTERS.length) {
                String letter = LETTERS[index];
                //通知选择的字母
                if (mOnLetterSelectedListener != null) {
                    mOnLetterSelectedListener.onLetterSelected(index,letter);
                }
                //显示首字母
                mTextView.setVisibility(View.VISIBLE);
                mTextView.setText(letter);
            }
            return true;
        case MotionEvent.ACTION_MOVE:
            setBackgroundDrawable(new ColorDrawable(BG_COLOR));
            int tempIndex = (int) (event.getY() / mCellHeight);//获取第几个字母
            if (tempIndex >= 0 && tempIndex < LETTERS.length) {
                // 用户当前选中的字母与上一次不一样时
                if (tempIndex != index) {

                    String letter = LETTERS[tempIndex];
                    //通知选择的字母
                    if (mOnLetterSelectedListener != null) {
                        mOnLetterSelectedListener.onLetterSelected(tempIndex,letter);
                    }
                    //显示首字母
                    mTextView.setVisibility(View.VISIBLE);
                    mTextView.setText(letter);
                }
                index = tempIndex;
            }
            break;
        case MotionEvent.ACTION_UP:
            //抬起时颜色变透明
            setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            mTextView.setVisibility(View.GONE);
            break;

    }
    return super.onTouchEvent(event);
}

private OnLetterSelectedListener mOnLetterSelectedListener;

public void setOnLetterSelectedListener(OnLetterSelectedListener onLetterSelectedListener) {

    mOnLetterSelectedListener = onLetterSelectedListener;
}

/**
 * 定义接口通知字母选择
 */
public interface OnLetterSelectedListener {
    void onLetterSelected(int position,String letter);
}

private TextView mTextView;
/**
 * 设置首字母显示
 */

public void setTextView(TextView textView){
    mTextView = textView;
}

public int dp2px(int dp){
    float density = getResources().getDisplayMetrics().density;
    return (int) (dp*density+0.5f);
}

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值