前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android开发之 仿微信通讯录 (二) 侧边首字母导航控件

Android开发之 仿微信通讯录 (二) 侧边首字母导航控件

作者头像
Xiaolei123
发布2019-03-20 11:32:21
1.7K0
发布2019-03-20 11:32:21
举报
文章被收录于专栏:肖蕾的博客肖蕾的博客

Android开发之 仿微信通讯录 (二)侧边首字母导航控件

代码语言:javascript
复制
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.support.annotation.ColorInt
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.xiaolei.library.Exts.dp2px
import com.xiaolei.library.Exts.measureHeight
import com.xiaolei.library.Exts.measureWidth
import com.xiaolei.library.Exts.sp2px
import java.util.*

class LatterNaviBar @JvmOverloads constructor(  
  context: Context,    
  attrs: AttributeSet? = null,    
  defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr)
{  
  private var mHeight = 0    
  private var mWidth = 0    
  private var wordRect = Rect()    
  private val paint = Paint()    
  private val words = LinkedList<Word>()    
  
  private val worldSpace by lazy { dp2px(11) }    
  private var selectListener: ((word: String) -> Unit)? = null    
  private var downListener: (() -> Unit)? = null    
  private var upListener: (() -> Unit)? = null    
  
  init    
  {      
    paint.color = Color.parseColor("#40e0d6")        
    paint.textSize = sp2px(10).toFloat()        
    paint.isAntiAlias = true    
  }    
  
  override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)    
  {      
    mHeight = measureHeight(heightMeasureSpec)        
    mWidth = measureWidth(widthMeasureSpec)        
    super.onMeasure(widthMeasureSpec, heightMeasureSpec)    
  }    
  
  /**    
   * 设置文字颜色     
   */    
  fun setTextColor(@ColorInt color: Int)    
  {      
    paint.color = color        
    invalidate()    
  }    
  
  /**    
   * 设置文字大小,单位 sp     
   */    
  fun setTextSize(sp: Int)    
  {      
    paint.textSize = sp2px(sp).toFloat()        
    invalidate()    
  }    
  
  /**    
   * 设置要显示的文字     
   */    
  fun setWords(words: Array<String>)    
  {      
    this.words.clear()        
    val tmp = words.map { Word(it, Rect()) }        
    this.words.addAll(tmp)        
    invalidate()    
  }    
  
  /**    
   * 设置选择监听     
   */    
 fun setOnSelect(listener: (word: String) -> Unit)    
 {      
   selectListener = listener    
 }    
 
 /**    
  * 设置按下去的监听     
  */    
 fun setOnDown(listener: () -> Unit)    
 {      
   downListener = listener    
 }    
 
 /**     
  * 设置弹起的监听     
  */    
 fun setOnUp(listener: () -> Unit)    
 {      
   upListener = listener    
 }  
   
 override fun onTouchEvent(event: MotionEvent): Boolean    
 {      
   val y = event.y        
   when (event.action)        
   {          
     MotionEvent.ACTION_DOWN,            
     MotionEvent.ACTION_MOVE ->            
     {              
       if (event.action == MotionEvent.ACTION_DOWN)                
       {                  
         downListener?.invoke()                
       }               
       for (w in words)                
       {                  
         if (w.rect.top <= y && w.rect.bottom >= y)                    
         {                      
           selectListener?.invoke(w.word)                        
           break                    
         }                
       }            
     }            
     MotionEvent.ACTION_UP,            
     MotionEvent.ACTION_CANCEL ->            
     {              
       upListener?.invoke()            
     }        
   }        
   return super.onTouchEvent(event)    
 }    
 override fun onDraw(canvas: Canvas)    
 {      
   super.onDraw(canvas)        
   var countHeight = 0        
   for (w in words)        
   {          
     val word = w.word            
     val rect = w.rect            
     wordRect.setEmpty()            
     paint.getTextBounds(word, 0, word.length, wordRect)            
     val worldHeight = wordRect.height()            
     val worldWidth = wordRect.width()            
     rect.setEmpty()            
     rect.top = countHeight            
     countHeight += worldHeight + worldSpace            
     rect.bottom = countHeight            
     canvas.drawText(word, (mWidth / 2f) - (worldWidth / 2), countHeight.toFloat(), paint)        
   }    
 }    
 
 private class Word(var word: String, val rect: Rect)
 
}       
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-3-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Android开发之 仿微信通讯录 (二)侧边首字母导航控件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档