自定义控件:IndexBar

这里写图片描述

技术点

如何绘制text?

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

如何计算x y?

文字的xy 坐标是文字左下角为(0,0)
x坐标:单元格宽度/2-文字宽度的/2
y坐标:单元格高度/2+文字高度/2

float x = cellHeight / 2 - paint.measureText(letter) / 2;

Rect rect = new Rect();
paint.getTextBounds(letter, 0, letter.length(), rect);
float y = cellHeight / 2 + rect.height() / 2 + i * cellHeight;

计算单元格的宽高

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    cellWidth = getMeasuredWidth();
    height = getMeasuredHeight();
    cellHeight = height / 26;
}

按下字母时弹出toast

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:

            break;
        case MotionEvent.ACTION_MOVE:

            break;
        case MotionEvent.ACTION_UP:
            touchIndex = (int) (event.getY() / cellHeight);
            if(touchIndex!=-1 && touchIndex<LETTERS.length){
                if(onClickListener !=null){
                    onClickListener.update(LETTERS[touchIndex]);
                }
            }
            break;
    }
    return true;
}

完整代码

public class IndexBar extends View {

    private Paint paint;
    private int cellWidth;
    private int cellHeight;
    private int height;

    private int touchIndex = -1;


    public static final String[] LETTERS = new String[]{
            "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"};


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

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

    public IndexBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        paint.setTypeface(Typeface.DEFAULT_BOLD);
        paint.setTextSize(30);
        paint.setColor(Color.WHITE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        cellWidth = getMeasuredWidth();
        height = getMeasuredHeight();
        cellHeight = height / 26;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        for (int i = 0; i < LETTERS.length; i++) {
            String letter = LETTERS[i];
            float x = cellHeight / 2 - paint.measureText(letter) / 2;

            Rect rect = new Rect();
            paint.getTextBounds(letter, 0, letter.length(), rect);
            float y = cellHeight / 2 + rect.height() / 2 + i * cellHeight;
            canvas.drawText(letter, x, y, paint);
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:

                break;
            case MotionEvent.ACTION_MOVE:

                break;
            case MotionEvent.ACTION_UP:
                touchIndex = (int) (event.getY() / cellHeight);
                if(touchIndex!=-1 && touchIndex<LETTERS.length){
                    if(onClickListener !=null){
                        onClickListener.update(LETTERS[touchIndex]);
                    }
                }
                break;
        }
        return true;
    }


    public interface OnClickListener {
        void update(String letter);
    }

    private OnClickListener onClickListener;

    public void setOnClickListener(OnClickListener onClickListener){
        this.onClickListener = onClickListener;
    }
}
IndexBar indexBar = findViewById(R.id.indexBar);
indexBar.setOnClickListener(new IndexBar.OnClickListener() {
    @Override
    public void update(String letter) {
        Toast.makeText(MainActivity.this,letter,Toast.LENGTH_SHORT).show();
    }
});

demo:https://gitee.com/customView/IndexBar01.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值