Android自定义侧滑字母控件

public class LetterSlideBarView extends View {

    private Paint mPaint;

    // 定义26个字母
    public static String[] mLetters = {"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 int itemHeight;
    private int mSelectPostion = -1;
    private String mSelectLetter = "";

    public void setOnLetterListener(OnLetterListener mLetterListener) {
        this.mLetterListener = mLetterListener;
    }

    private OnLetterListener mLetterListener;

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

    public LetterSlideBarView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LetterSlideBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(PxUtils.sp2px(getContext(), 20));
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int textWidth = (int) mPaint.measureText("A");
        int width = getPaddingLeft() + getPaddingRight() + textWidth;
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(width, height);
    }


    @Override
    protected void onDraw(Canvas canvas) {
  /*      int textHeight = (getHeight() - getPaddingBottom() - getPaddingTop()) / mLetters.length;
        for (int i = 0; i < mLetters.length; i++) {

            String text = mLetters[i];
            Rect bound = new Rect();
            mPaint.getTextBounds(text, 0, text.length(), bound);
            int x = getWidth() / 2 - bound.width() / 2;

            Paint.FontMetricsInt fontMetricsInt = mPaint.getFontMetricsInt();
            int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;
            int baseLine = getPaddingTop() + textHeight * i + dy + textHeight / 2;
            canvas.drawText(text, x, baseLine, mPaint);
        }*/
        itemHeight = (getHeight() - getPaddingTop() - getPaddingBottom()) / mLetters.length;

        for (int i = 0; i < mLetters.length; i++) {
            // 知道每个字母的中心位置  1  字母的高度一半   2 字母高度一般+前面字符的高度
            int letterCenterY = i * itemHeight + itemHeight / 2 + getPaddingTop();
            // 基线,基于中心位置, 知道中心位置
            Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
            int dy = (int) ((fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom);
            int baseLine = letterCenterY + dy;
            // x 绘制在最中间 = 宽度/2 - 文字/2
            int textWidth = (int) mPaint.measureText(mLetters[i]);
            int x = getWidth() / 2 - textWidth / 2;

            if (i == mSelectPostion) {
                mPaint.setColor(Color.RED);
                canvas.drawText(mLetters[i], x, baseLine, mPaint);
            } else {
                mPaint.setColor(Color.BLACK);
                canvas.drawText(mLetters[i], x, baseLine, mPaint);
            }
        }
        Log.e("jc", "onDraw");

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.e("jc", "getY" + event.getY());
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_MOVE:

                int moveY = (int) event.getY();
                int postion = moveY / itemHeight;
                if (postion < 0) {
                    postion = 0;
                }
                if (postion > mLetters.length) {
                    postion = mLetters.length - 1;
                }
                if (mSelectPostion != postion) {
                    mSelectPostion = postion;
                    invalidate();
                    mSelectLetter = mLetters[mSelectPostion];
                    if (null != mLetterListener) {
                        mLetterListener.onLetterChange(mSelectPostion, mSelectLetter);
                    }
                }

                break;
            case MotionEvent.ACTION_UP:
                if (null != mLetterListener) {
                    mLetterListener.onActionUp();
                }
                break;

            default:

                break;
        }


        return true;
    }

    public interface OnLetterListener {
        void onLetterChange(int postion, String s);

        void onActionUp();
    }
}

/  使用

letterSliderBar.setOnLetterListener(new LetterSlideBarView.OnLetterListener() {
    @Override
    public void onLetterChange(int postion, String s) {
        if (!TextUtils.isEmpty(s)) {
            tvLetter.setVisibility(View.VISIBLE);
            tvLetter.setText(s);
        }
    }

    @Override
    public void onActionUp() {
        tvLetter.setVisibility(View.GONE);
    }
});

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值