zoukankan      html  css  js  c++  java
  • 自定义一个字母Button

      1 package com.example.administrator.yunstore.widget;
      2 
      3 import android.content.Context;
      4 import android.graphics.Canvas;
      5 import android.graphics.Color;
      6 import android.graphics.Paint;
      7 import android.graphics.Typeface;
      8 import android.util.AttributeSet;
      9 import android.view.MotionEvent;
     10 import android.widget.Button;
     11 
     12 /**
     13  * Created by Administrator on 2016/10/19.
     14  * 自定义的一个字母button
     15  */
     16 
     17 public class LetterButton extends Button {
     18 
     19     // 分类字母
     20     private String[] assort = { "A", "B", "C", "D", "E", "F", "G",
     21             "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
     22             "U", "V", "W", "X", "Y", "Z" };
     23 
     24     private Paint paint = new Paint();
     25 
     26     private int selectIndex = -1;
     27 
     28     public LetterButton(Context context) {
     29         super(context);
     30     }
     31 
     32     public LetterButton(Context context, AttributeSet attrs) {
     33         super(context, attrs);
     34     }
     35 
     36     public LetterButton(Context context, AttributeSet attrs, int defStyleAttr) {
     37         super(context, attrs, defStyleAttr);
     38     }
     39 
     40     /**
     41      * @param canvas
     42      * 对按钮进行绘制
     43      */
     44     @Override
     45     protected void onDraw(Canvas canvas) {
     46         super.onDraw(canvas);
     47 
     48         int height=getHeight();
     49         int width=getWidth();
     50         int interval = height / assort.length;
     51 
     52         for (int i = 0, length = assort.length; i < length; i++) {
     53             // 抗锯齿
     54             paint.setAntiAlias(true);
     55             // 默认粗体
     56             paint.setTypeface(Typeface.DEFAULT_BOLD);
     57             // 白色
     58             paint.setColor(Color.BLUE);
     59             if (i == selectIndex) {
     60                 // 被选择的字母改变颜色和粗体
     61                 paint.setColor(Color.parseColor("#33bb99"));
     62                 paint.setFakeBoldText(true);
     63                 paint.setTextSize(30);
     64             }
     65             // 计算字母的X坐标
     66             float xPos = width / 2 - paint.measureText(assort[i]) / 2;
     67             // 计算字母的Y坐标
     68             float yPos = interval * i + interval;
     69             canvas.drawText(assort[i], xPos, yPos, paint);
     70             paint.reset();
     71         }
     72     }
     73 
     74     @Override
     75     public boolean dispatchTouchEvent(MotionEvent event) {
     76         float y = event.getY();
     77         int index = (int) (y / getHeight() * assort.length);
     78         if (index >= 0 && index < assort.length) {
     79 
     80             switch (event.getAction()) {
     81                 case MotionEvent.ACTION_MOVE:
     82                     // 如果滑动改变
     83                     if (selectIndex != index) {
     84                         selectIndex = index;
     85                         if (onTouch != null) {
     86                             onTouch.onTouchAssortListener(assort[selectIndex]);
     87                         }
     88 
     89                     }
     90                     break;
     91                 case MotionEvent.ACTION_DOWN:
     92                     selectIndex = index;
     93                     if (onTouch != null) {
     94                         onTouch.onTouchAssortListener(assort[selectIndex]);
     95                     }
     96 
     97                     break;
     98                 case MotionEvent.ACTION_UP:
     99                     if (onTouch != null) {
    100                         onTouch.onTouchAssortUP();
    101                     }
    102                     selectIndex = -1;
    103                     break;
    104             }
    105         } else {
    106             selectIndex = -1;
    107             if (onTouch != null) {
    108                 onTouch.onTouchAssortUP();
    109             }
    110         }
    111         invalidate();
    112 
    113         return true;
    114     }
    115 
    116     @Override
    117     public boolean onTouchEvent(MotionEvent event) {
    118         return super.onTouchEvent(event);
    119     }
    120     //字母监听器
    121     private OnTouchAssortListener onTouch;
    122 
    123     public interface OnTouchAssortListener {
    124          void onTouchAssortListener(String s);
    125           void onTouchAssortUP();
    126     }
    127 
    128 
    129     public void setOnTouchAssortListener(OnTouchAssortListener onTouch) {
    130         this.onTouch = onTouch;
    131     }
    132 }
  • 相关阅读:
    可持续化线段树(主席树)
    2016-06-19 NOIP模拟赛
    0618图的整理
    1536 海战
    1005 生日礼物
    3280 easyfinding
    2594 解药还是毒药
    2919 选择题
    1845 二叉查找树
    1174 靶形数独
  • 原文地址:https://www.cnblogs.com/yoyohong/p/5979960.html
Copyright © 2011-2022 走看看