栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Java

Android 自定义View 26字母侧边栏效果

Java 更新时间:发布时间: 百科书网 趣学号

上效果  先看效果不浪费时间  

侧边栏字母视频-CSDN直播

直接上代码 

public class LetterView  extends View {
    private int letterSize ;
    private int letterColor = Color.RED ;
    private int selectColor = Color.GREEN ;
    private String []  mLetterList = 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 LetterView(Context context) {
        this(context,null);
    }
    public LetterView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }
    public LetterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LetterView);
        letterSize = (int) array.getDimension(R.styleable.LetterView_letterSize,letterSize);
        letterColor = array.getColor(R.styleable.LetterView_letterColor,letterColor);
        selectColor = array.getColor(R.styleable.LetterView_selectColor,selectColor);
        mPaint = new Paint();
        mPaint.setColor(letterColor);
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(letterSize);
        mSelectPaint = new Paint();
        mSelectPaint.setColor(selectColor);
        mSelectPaint.setAntiAlias(true);
        mSelectPaint.setTextSize(letterSize);
    }
    private Paint mPaint,mSelectPaint ;
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = (int) (mPaint.measureText("M")+getPaddingLeft()+getPaddingRight());
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(width,height);
    }
    private  int  itemHeght ;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = (int) (mPaint.measureText("M")+getPaddingLeft()+getPaddingRight());
        itemHeght =  (getHeight()-getPaddingTop()-getPaddingBottom())/mLetterList.length;
        for (int i = 0; i < mLetterList.length; i++) {
            int x = (int) (width/2-mPaint.measureText(mLetterList[i])/2);
            int dy =i*itemHeght+itemHeght/2;
            Paint.FontMetricsInt fontMetricsInt = mPaint.getFontMetricsInt();
            int baseLine = (fontMetricsInt.bottom-fontMetricsInt.top)/2-fontMetricsInt.bottom ;
            int y = dy+ baseLine;

            if(number==i){
                canvas.drawText(mLetterList[i],x,y,mSelectPaint);
            }else {
                canvas.drawText(mLetterList[i],x,y,mPaint);
            }


        }
    }

    private int number=-1 ;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                float  slidingLength =    event.getY() ;
                int   pos   = Math.round(slidingLength/itemHeght);  // 向上取整
                if(pos<0){
                    pos = 0 ;
                }
                if(pos>mLetterList.length-1){
                    pos = mLetterList.length-1 ;
                }
                if(number == pos){
                    return true ;
                }
                if(listener!=null){
                    listener.getStr(mLetterList[pos]);
                }
                number =pos ;
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                if(listener!=null){     // 抬起的时候 设置成空字符串
                    listener.getStr("");
                }
                break;
        }
        return true;
    }
    private LetterViewListener listener ;
    public void setListener(LetterViewListener listener) {
        this.listener = listener;
    }
    public interface  LetterViewListener{
        void getStr(String str);
    }
}

算高度可以自己实现 ,我写的是自己的想法

需要的自定义属性


    
    
    

MainActivity 具体实现 

let_view.setListener(str -> {
    if(str.equals("")){
        tv.setVisibility(View.GONE);
    }else {
        tv.setVisibility(View.VISIBLE);
    }
    tv.setText(str);
});

 xml文件  





转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/1077832.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号