欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义View仿微信LetterView效果

程序员文章站 2023-12-09 18:21:46
废话不多说了,具体代码如下所示: public class letterview extends view { private string tag =...

废话不多说了,具体代码如下所示:

 public class letterview extends view {
  private string tag = letterview.class.getsimplename();
  //a,b,c....z,#
  public list<string> letters;
  private paint mpaint;
  private int selectposition = -1;
  private textview mletter;
  public void setmletter(textview mletter) {
    this.mletter = mletter;
  }
  public letterview(context context) {
    this(context,null);
  }
  public letterview(context context, attributeset attrs) {
    this(context, attrs,0);
  }
  public letterview(context context, attributeset attrs, int defstyleattr) {
    super(context, attrs, defstyleattr);
    //生产字母
    letters = new arraylist<>();
    for (int i = 65; i < 91; i++) {
      letters.add(string.format(locale.china,"%c",i));
      log.e(tag, "letterview: "+ string.format(locale.china,"%c",i));
    }
    letters.add("#");//追加一个#
    //初始化画笔
    mpaint = new paint();
    mpaint.setantialias(true);
    mpaint.setcolor(color.blue);
    mpaint.settextsize(30);
  }
  @override
  protected void ondraw(canvas canvas) {
    super.ondraw(canvas);
    /**
     * 获取view的宽度
     * 获取view的高度
     */
    int width = getmeasuredwidth();
    int height = getmeasuredheight();
    //测量字的宽度
    int size = letters.size();
    for (int i = 0; i < size; i++) {
      float textwidth = mpaint.measuretext(letters.get(i));
      int singleheight = height / size;
      if (selectposition == i){//被选中的
        mpaint.setcolor(color.red);
      }else{
        mpaint.setcolor(color.blue);
      }
      canvas.drawtext(letters.get(i),(width - textwidth)/2, singleheight * (i + 1),mpaint);
      /**
       * drawtext() x y \_ 为基准线
       */
    invalidate();
    }
  }
  /**
   * android将触摸事件封装,包装了动作,位置信息;onclick也是一种motionevent
   * onclick 事件 实际上是 ontouchevent事件
   * @param event
   * @return
   */
  @override
  public boolean ontouchevent(motionevent event) {
    //包装了动作,位置信息
//    event.gety();//相对于view本身的坐标值
//    event.getrawy();//返回的是相对于屏幕的坐标值
    float y = event.gety();
    log.e(tag, "ontouchevent: y:"+ y);
//    log.e(tag, "ontouchevent: rawy:"+event.getrawy() );
    int measuredheight = getmeasuredheight();
    int singleheight = measuredheight / letters.size();
    int position = (int) (y / singleheight);
    log.e(tag, "ontouchevent: " + position );
    switch (event.getaction()) {
      case motionevent.action_move:
        log.e(tag, "ontouchevent: action_move" );
      case motionevent.action_down:
        log.e(tag, "ontouchevent: action_down" );
        selectposition = position;
        if (mletter != null) {
          mletter.setvisibility(view.visible);
          //极限情况有可能下标越界,需要判断一下
          if (position < letters.size() && position >= 0){
            mletter.settext(letters.get(position));
          }
        }
        break;
      case motionevent.action_up:
        log.e(tag, "ontouchevent: action_up" );
        selectposition = -1;
        if (mletter != null) {
          mletter.setvisibility(view.gone);
        }
        break;
    }
    //返回true代表事件被处理了
    return true;
  }
}