Android开发自定义View的简单尝试 自定义日历视图

自定义日历视图

  1. 自定义属性。分别是日期的背景颜色,日期表头的背景颜色,正常的日期字体颜色,当前日期的颜色,当前日期的背景颜色,选择的日期的背景颜色,日期的字体大小,日期表头的字体大小。
    [html]  view plain  copy
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <resources>  
    3.   
    4.   
    5.     <declare-styleable name="SunshineView">  
    6.         <attr name="android:background"/>  
    7.         <attr name="titleBackgroundColor" format="color"/>  
    8.         <attr name="normalDateColor" format="color"/>  
    9.         <attr name="currentDateColor" format="color"/>  
    10.         <attr name="currentDateBackgroundColor" format="color"/>  
    11.         <attr name="selectedDateBackgroundColor" format="color"/>  
    12.         <attr name="dateSize" format="dimension"/>  
    13.         <attr name="titleSize" format="dimension"/>  
    14.   
    15.     </declare-styleable>  
    16.   
    17.   
    18. </resources>  

  2. 获取自定义的属性
    [java]  view plain  copy
    1. //默认的字体大小  
    2.    defaultSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 19, getResources().getDisplayMetrics());  
    3.    /** 
    4.     * 获取自定义属性 
    5.     */  
    6.    TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.SunshineView,defStyleAttr,0);  
    7.   
    8.    int attrCount = typedArray.getIndexCount();  
    9.    for(int i=0; i<attrCount; i++) {  
    10.        int attr = typedArray.getIndex(i);  
    11.        switch (attr){  
    12.   
    13.            case R.styleable.SunshineView_android_background:  
    14.                background = typedArray.getColor(attr,Color.parseColor("#ffec00"));  
    15.                break;  
    16.   
    17.            case R.styleable.SunshineView_titleBackgroundColor:  
    18.                titleBackgroundColor = typedArray.getColor(attr,0xff0000);  
    19.                break;  
    20.   
    21.            case R.styleable.SunshineView_normalDateColor:  
    22.                normalColor = typedArray.getColor(attr, 0x232323);  
    23.                break;  
    24.   
    25.            case R.styleable.SunshineView_currentDateColor:  
    26.                currentColor = typedArray.getColor(attr, 0xffffff);  
    27.                break;  
    28.   
    29.            case R.styleable.SunshineView_currentDateBackgroundColor:  
    30.                currentBgColor = typedArray.getColor(attr, 0xFFDA4336);  
    31.                break;  
    32.   
    33.            case R.styleable.SunshineView_selectedDateBackgroundColor:  
    34.                selectedBgColor = typedArray.getColor(attr, 0x7fd2d2d2);  
    35.                break;  
    36.   
    37.            case R.styleable.SunshineView_dateSize:  
    38.                dateSize = typedArray.getDimensionPixelSize(attr, defaultSize);  
    39.                break;  
    40.   
    41.            case R.styleable.SunshineView_titleSize:  
    42.                titleSize = typedArray.getDimensionPixelSize(attr,defaultSize);  
    43.                break;  
    44.   
    45.        }  
    46.    }  
    47.    //记得要调用,回收原来的属性  
    48.    typedArray.recycle();  

  3. 初始化画笔
    [java]  view plain  copy
    1. private void initPaint(){  
    2.   
    3.       //星期几标题字体大小  
    4.       titleSize = (int1.5*defaultSize;  
    5.   
    6.       mTitlePaint = new TextPaint();  
    7.       mTitlePaint.setColor(Color.parseColor("#ffffff"));  
    8.       mTitlePaint.setTextSize(titleSize);  
    9.       mTitlePaint.setStyle(Paint.Style.STROKE);  
    10.       mTitlePaint.setTextAlign(Paint.Align.CENTER);  
    11.       mTitlePaint.setFlags(Paint.ANTI_ALIAS_FLAG);  
    12.       mTitlePaint.setAntiAlias(true);  
    13.       mTitlePaint.getTextBounds("22"02new Rect());  
    14.   
    15.   
    16.       //日期的字体大小  
    17.       mPaint = new TextPaint();  
    18.       mPaint.setColor(normalColor);  
    19.       mPaint.setTextSize(dateSize);  
    20.       mPaint.setStyle(Paint.Style.STROKE);  
    21.       mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);  
    22.       mPaint.setTextAlign(Paint.Align.CENTER);  
    23.       mPaint.setAntiAlias(true);  
    24.       mPaint.getTextBounds("22"02new Rect());  
    25.   }  

  4. 测量View的大小
    [java]  view plain  copy
    1. @Override  
    2. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
    3.     super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
    4.   
    5.     /** 
    6.      * 设置宽度 
    7.      */  
    8.     int widthMode = MeasureSpec.getMode(widthMeasureSpec);  
    9.     int widthSize = MeasureSpec.getSize(widthMeasureSpec);  
    10.   
    11.     //match_parent  
    12.     if (widthMode == MeasureSpec.EXACTLY){  
    13.         width = widthSize;  
    14.     } else if(widthMode == MeasureSpec.AT_MOST){  
    15.         width = getResources().getDisplayMetrics().widthPixels - getPaddingLeft() - getPaddingRight();  
    16.     }  
    17.   
    18.     /*** 
    19.      * 设置高度 
    20.      */  
    21.   
    22.     int heightMode = MeasureSpec.getMode(heightMeasureSpec);  
    23.     int heightSize = MeasureSpec.getSize(heightMeasureSpec);  
    24.   
    25.     //match_parent  
    26.     if(heightMode == MeasureSpec.EXACTLY){  
    27.         height = heightSize;  
    28.     }else if(heightMode == MeasureSpec.AT_MOST){  
    29.         height = getResources().getDisplayMetrics().heightPixels - getPaddingTop() - getPaddingBottom();  
    30.     }  
    31.   
    32.     setMeasuredDimension(width, height);  
    33. }  

  5. 接着就是开始画了
    [java]  view plain  copy
    1. @Override  
    2.    protected void onDraw(Canvas canvas) {  
    3.        super.onDraw(canvas);  
    4.      
    5.        drawTitle(canvas);  
    6.        drawDate(canvas);  
    7.    }  
    8.   
    9.    /** 
    10.     * 画表头 
    11.     * @param canvas 
    12.     */  
    13.    private void drawTitle(Canvas canvas){  
    14.        float titleHeight =  mTitlePaint.measureText("二");  
    15.        //画背景  
    16.        Paint bgPaint = new Paint();  
    17.        bgPaint.setColor(titleBackgroundColor);  
    18.        bgPaint.setStyle(Paint.Style.FILL);  
    19.        canvas.drawRect(getPaddingLeft(), getPaddingTop(), width, height / 7, bgPaint);  
    20.   
    21.        //日期表头  
    22.        for(int i=0; i<7; i++){  
    23.            canvas.drawText(mTitles[i],(2*i+1)*width/14,height/14+titleHeight/2,mTitlePaint);  
    24.        }  
    25.    }  
    26.   
    27.    /** 
    28.     * 画日期 
    29.     * @param canvas 
    30.     */  
    31.    private void drawDate(Canvas canvas){  
    32.   
    33.        /** 
    34.         * 画背景 
    35.         */  
    36.        Paint paint = new Paint();  
    37.        paint.setColor(background);  
    38.        paint.setTextSize(titleSize);  
    39.        paint.setStyle(Paint.Style.STROKE);  
    40.        paint.setFlags(Paint.ANTI_ALIAS_FLAG);  
    41.        paint.setAntiAlias(true);  
    42.   
    43.        canvas.drawRect(getPaddingLeft(),height /7,getPaddingRight(),height,paint);  
    44.   
    45.        //当前的日期  
    46.        Calendar calendar = Calendar.getInstance();  
    47.        int mCurrentDay = calendar.get(Calendar.DAY_OF_MONTH);  
    48.        //当前天是星期几  
    49.        int currentDayIndex = calendar.get(Calendar.DAY_OF_WEEK)-1;  
    50.   
    51.        //获取当月的第一天是星期几  
    52.        int firstDayOfMonthIndex = currentDayIndex - (mCurrentDay-1)%7;  
    53.        if(firstDayOfMonthIndex<=0){  
    54.            firstDayOfMonthIndex = firstDayOfMonthIndex + 7;  
    55.        }  
    56.   
    57.        //标记所描绘的日期是星期几  
    58.        int dayWeekIndex = firstDayOfMonthIndex;  
    59.        //画到了第几行  
    60.        int rowIndex = 1;  
    61.   
    62.        int dateHeight = (int) (mPaint.getFontMetrics().ascent+mPaint.getFontMetrics().descent);  
    63.        int paddingTop =  height/14+height /7;  
    64.   
    65.        int monthDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);  
    66.        for(int i=1; i<=monthDays; i++){  
    67.   
    68.            if(dayWeekIndex>7){  
    69.                dayWeekIndex=1;  
    70.                rowIndex= rowIndex+1;  
    71.            }  
    72.   
    73.   
    74.            //先计算这天所要显示的位置  
    75.            int positionX = (2*dayWeekIndex-1)*width/14;  
    76.            int positionY = (2*rowIndex-1)*height/12 + dateHeight/2 + paddingTop;  
    77.   
    78.            //判断日期是否是当前日期,是的话则画背景的圆,并且设置相应的日期颜色  
    79.            if(i!=mCurrentDay){  
    80.                //选择其它日期时的背景颜色  
    81.                if(i==Integer.parseInt(selectedDate)){  
    82.                    drawCircle(canvas, positionX, positionY+dateHeight/2, height / 13, selectedBgColor);  
    83.                    mPaint.setColor(Color.parseColor("#232323"));  
    84.                }  
    85.            }else {  
    86.                drawCircle(canvas, positionX, positionY+dateHeight/2, height / 13, currentBgColor);  
    87.                mPaint.setColor(currentColor);  
    88.            }  
    89.            //画日期和重置字体颜色  
    90.            canvas.drawText(i+"",positionX,positionY,mPaint);  
    91.            mPaint.setColor(Color.parseColor("#232323"));  
    92.   
    93.            //星期几递增1  
    94.            dayWeekIndex++;  
    95.   
    96.            //将日期的信息添加进Map,用来监听点击事件的触发条件  
    97.            String dateKey = rowIndex+"_"+dayWeekIndex;  
    98.            datePositionMap.put(dateKey, i + "");  
    99.   
    100.        }  
    101.    }  
    102.   
    103.    /** 
    104.     * 画圆形的背景 
    105.     * @param canvas 
    106.     * @param positionX 
    107.     * @param positionY 
    108.     * @param radius 
    109.     * @param color 
    110.     */  
    111.    private void drawCircle(Canvas canvas,int positionX, int positionY, int radius,int color){  
    112.        mPaint.setStyle(Paint.Style.FILL);  
    113.        mPaint.setColor(color);  
    114.        canvas.drawCircle(positionX, positionY, radius,mPaint);  
    115.    }  

  6. 事件监听
    [java]  view plain  copy
    1. /**用来监听点击事件 
    2.  * 
    3.  * @param event 
    4.  * @return 
    5.  */  
    6. @Override  
    7. public boolean onTouchEvent(MotionEvent event) {  
    8.     int action = event.getAction();  
    9.     if(action==MotionEvent.ACTION_DOWN){  
    10.   
    11.         selectedDate = selectedDate(event);  
    12.         if(selectedDate!=null){  
    13.             invalidate();  
    14.         }else {  
    15.             selectedDate="0";  
    16.         }  
    17.     }  
    18.     return true;  
    19.   
    20. }  
    21.   
    22. /** 
    23.  * 处理单击事件,重绘选中的日期的背景 
    24.  * @param event 
    25.  * @return 
    26.  */  
    27. private String selectedDate(MotionEvent event){  
    28.   
    29.     int paddingTop =  height/14+height /7;  
    30.     int column = (int)Math.ceil(event.getX()/(width/7))+1;  
    31.     int row = (int)Math.ceil((event.getY()-paddingTop)/(height/6));  
    32.   
    33.     String key = row+"_"+column;  
    34.     String clickDay = datePositionMap.get(key);  
    35.   
    36.     return clickDay;  
    37. }  

  7. 布局文件
    [html]  view plain  copy
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     xmlns:sunshine="http://schemas.android.com/apk/res-auto"  
    4.     android:layout_width="match_parent"  
    5.     android:layout_height="match_parent"  
    6.     android:orientation="vertical">  
    7.   
    8.     <com.sharemebook.myapplication.SunshineView  
    9.         android:layout_width="match_parent"  
    10.         android:layout_height="282dp"  
    11.         android:background="#ffec00"  
    12.         sunshine:titleBackgroundColor="#ff0000"  
    13.         sunshine:dateSize="15sp"  
    14.         sunshine:normalDateColor="#232323"  
    15.         sunshine:currentDateColor="#ffffff"  
    16.         sunshine:selectedDateBackgroundColor="#7fd2d2d2"  
    17.         sunshine:currentDateBackgroundColor="#ff0000"/>  
    18. </LinearLayout>  

完整的代码

[java]  view plain  copy
  1. /** 
  2.  * Created by Sanisy on 2016/3/22. 
  3.  */  
  4. public class SunshineView extends View{  
  5.   
  6.     private Paint mTitlePaint;  
  7.     private Paint mPaint;  
  8.   
  9.     private int normalColor;  
  10.     private int currentColor;  
  11.     private int currentBgColor;  
  12.     private int selectedBgColor;  
  13.     private int dateSize;  
  14.   
  15.     private int background;  
  16.   
  17.     //星期几标签字体设定的大小和默认的字体大小  
  18.     private int titleBackgroundColor;  
  19.     private int titleSize;  
  20.     private int defaultSize;  
  21.     private String mTitles[] ={"一","二","三","四","五","六","日"};  
  22.   
  23.     //日历整体的宽度和高度  
  24.     private int width;  
  25.     private int height;  
  26.   
  27.     //被点击的日期  
  28.     private String selectedDate = "0";  
  29.     private Map<String,String> datePositionMap = new HashMap<>();  
  30.   
  31.     public SunshineView(Context context) {  
  32.         this(context, null);  
  33.     }  
  34.   
  35.     public SunshineView(Context context, AttributeSet attrs) {  
  36.         this(context, attrs, 0);  
  37.     }  
  38.   
  39.     public SunshineView(Context context, AttributeSet attrs, int defStyleAttr) {  
  40.         super(context, attrs, defStyleAttr);  
  41.         //默认的字体大小  
  42.         defaultSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 19, getResources().getDisplayMetrics());  
  43.         /** 
  44.          * 获取自定义属性 
  45.          */  
  46.         TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.SunshineView,defStyleAttr,0);  
  47.   
  48.         int attrCount = typedArray.getIndexCount();  
  49.         for(int i=0; i<attrCount; i++) {  
  50.             int attr = typedArray.getIndex(i);  
  51.             switch (attr){  
  52.   
  53.                 case R.styleable.SunshineView_android_background:  
  54.                     background = typedArray.getColor(attr,Color.parseColor("#ffec00"));  
  55.                     break;  
  56.   
  57.                 case R.styleable.SunshineView_titleBackgroundColor:  
  58.                     titleBackgroundColor = typedArray.getColor(attr,0xff0000);  
  59.                     break;  
  60.   
  61.                 case R.styleable.SunshineView_normalDateColor:  
  62.                     normalColor = typedArray.getColor(attr, 0x232323);  
  63.                     break;  
  64.   
  65.                 case R.styleable.SunshineView_currentDateColor:  
  66.                     currentColor = typedArray.getColor(attr, 0xffffff);  
  67.                     break;  
  68.   
  69.                 case R.styleable.SunshineView_currentDateBackgroundColor:  
  70.                     currentBgColor = typedArray.getColor(attr, 0xFFDA4336);  
  71.                     break;  
  72.   
  73.                 case R.styleable.SunshineView_selectedDateBackgroundColor:  
  74.                     selectedBgColor = typedArray.getColor(attr, 0x7fd2d2d2);  
  75.                     break;  
  76.   
  77.                 case R.styleable.SunshineView_dateSize:  
  78.                     dateSize = typedArray.getDimensionPixelSize(attr, defaultSize);  
  79.                     break;  
  80.   
  81.                 case R.styleable.SunshineView_titleSize:  
  82.                     titleSize = typedArray.getDimensionPixelSize(attr,defaultSize);  
  83.                     break;  
  84.   
  85.             }  
  86.         }  
  87.         //记得要调用,回收原来的属性  
  88.         typedArray.recycle();  
  89.   
  90.         initPaint();  
  91.     }  
  92.   
  93.   
  94.     private void initPaint(){  
  95.   
  96.         //星期几标题字体大小  
  97.         titleSize = (int1.5*defaultSize;  
  98.   
  99.         mTitlePaint = new TextPaint();  
  100.         mTitlePaint.setColor(Color.parseColor("#ffffff"));  
  101.         mTitlePaint.setTextSize(titleSize);  
  102.         mTitlePaint.setStyle(Paint.Style.STROKE);  
  103.         mTitlePaint.setTextAlign(Paint.Align.CENTER);  
  104.         mTitlePaint.setFlags(Paint.ANTI_ALIAS_FLAG);  
  105.         mTitlePaint.setAntiAlias(true);  
  106.         mTitlePaint.getTextBounds("22"02new Rect());  
  107.   
  108.   
  109.         //日期的字体大小  
  110.         mPaint = new TextPaint();  
  111.         mPaint.setColor(normalColor);  
  112.         mPaint.setTextSize(dateSize);  
  113.         mPaint.setStyle(Paint.Style.STROKE);  
  114.         mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);  
  115.         mPaint.setTextAlign(Paint.Align.CENTER);  
  116.         mPaint.setAntiAlias(true);  
  117.         mPaint.getTextBounds("22"02new Rect());  
  118.     }  
  119.   
  120.     @Override  
  121.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  122.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  123.   
  124.         /** 
  125.          * 设置宽度 
  126.          */  
  127.         int widthMode = MeasureSpec.getMode(widthMeasureSpec);  
  128.         int widthSize = MeasureSpec.getSize(widthMeasureSpec);  
  129.   
  130.         //match_parent  
  131.         if (widthMode == MeasureSpec.EXACTLY){  
  132.             width = widthSize;  
  133.         } else if(widthMode == MeasureSpec.AT_MOST){  
  134.             width = getResources().getDisplayMetrics().widthPixels - getPaddingLeft() - getPaddingRight();  
  135.         }  
  136.   
  137.         /*** 
  138.          * 设置高度 
  139.          */  
  140.   
  141.         int heightMode = MeasureSpec.getMode(heightMeasureSpec);  
  142.         int heightSize = MeasureSpec.getSize(heightMeasureSpec);  
  143.   
  144.         //match_parent  
  145.         if(heightMode == MeasureSpec.EXACTLY){  
  146.             height = heightSize;  
  147.         }else if(heightMode == MeasureSpec.AT_MOST){  
  148.             height = getResources().getDisplayMetrics().heightPixels - getPaddingTop() - getPaddingBottom();  
  149.         }  
  150.   
  151.         setMeasuredDimension(width, height);  
  152.     }  
  153.   
  154.     @Override  
  155.     protected void onDraw(Canvas canvas) {  
  156.         super.onDraw(canvas);  
  157.       
  158.         drawTitle(canvas);  
  159.         drawDate(canvas);  
  160.     }  
  161.   
  162.     /** 
  163.      * 画表头 
  164.      * @param canvas 
  165.      */  
  166.     private void drawTitle(Canvas canvas){  
  167.         float titleHeight =  mTitlePaint.measureText("二");  
  168.         //画背景  
  169.         Paint bgPaint = new Paint();  
  170.         bgPaint.setColor(titleBackgroundColor);  
  171.         bgPaint.setStyle(Paint.Style.FILL);  
  172.         canvas.drawRect(getPaddingLeft(), getPaddingTop(), width, height / 7, bgPaint);  
  173.   
  174.         //日期表头  
  175.         for(int i=0; i<7; i++){  
  176.             canvas.drawText(mTitles[i],(2*i+1)*width/14,height/14+titleHeight/2,mTitlePaint);  
  177.         }  
  178.     }  
  179.   
  180.     /** 
  181.      * 画日期 
  182.      * @param canvas 
  183.      */  
  184.     private void drawDate(Canvas canvas){  
  185.   
  186.         /** 
  187.          * 画背景 
  188.          */  
  189.         Paint paint = new Paint();  
  190.         paint.setColor(background);  
  191.         paint.setTextSize(titleSize);  
  192.         paint.setStyle(Paint.Style.STROKE);  
  193.         paint.setFlags(Paint.ANTI_ALIAS_FLAG);  
  194.         paint.setAntiAlias(true);  
  195.   
  196.         canvas.drawRect(getPaddingLeft(),height /7,getPaddingRight(),height,paint);  
  197.   
  198.         //当前的日期  
  199.         Calendar calendar = Calendar.getInstance();  
  200.         int mCurrentDay = calendar.get(Calendar.DAY_OF_MONTH);  
  201.         //当前天是星期几  
  202.         int currentDayIndex = calendar.get(Calendar.DAY_OF_WEEK)-1;  
  203.   
  204.         //获取当月的第一天是星期几  
  205.         int firstDayOfMonthIndex = currentDayIndex - (mCurrentDay-1)%7;  
  206.         if(firstDayOfMonthIndex<=0){  
  207.             firstDayOfMonthIndex = firstDayOfMonthIndex + 7;  
  208.         }  
  209.   
  210.         //标记所描绘的日期是星期几  
  211.         int dayWeekIndex = firstDayOfMonthIndex;  
  212.         //画到了第几行  
  213.         int rowIndex = 1;  
  214.   
  215.         int dateHeight = (int) (mPaint.getFontMetrics().ascent+mPaint.getFontMetrics().descent);  
  216.         int paddingTop =  height/14+height /7;  
  217.   
  218.         int monthDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);  
  219.         for(int i=1; i<=monthDays; i++){  
  220.   
  221.             if(dayWeekIndex>7){  
  222.                 dayWeekIndex=1;  
  223.                 rowIndex= rowIndex+1;  
  224.             }  
  225.   
  226.   
  227.             //先计算这天所要显示的位置  
  228.             int positionX = (2*dayWeekIndex-1)*width/14;  
  229.             int positionY = (2*rowIndex-1)*height/12 + dateHeight/2 + paddingTop;  
  230.   
  231.             //判断日期是否是当前日期,是的话则画背景的圆,并且设置相应的日期颜色  
  232.             if(i!=mCurrentDay){  
  233.                 //选择其它日期时的背景颜色  
  234.                 if(i==Integer.parseInt(selectedDate)){  
  235.                     drawCircle(canvas, positionX, positionY+dateHeight/2, height / 13, selectedBgColor);  
  236.                     mPaint.setColor(Color.parseColor("#232323"));  
  237.                 }  
  238.             }else {  
  239.                 drawCircle(canvas, positionX, positionY+dateHeight/2, height / 13, currentBgColor);  
  240.                 mPaint.setColor(currentColor);  
  241.             }  
  242.             //画日期和重置字体颜色  
  243.             canvas.drawText(i+"",positionX,positionY,mPaint);  
  244.             mPaint.setColor(Color.parseColor("#232323"));  
  245.   
  246.             //星期几递增1  
  247.             dayWeekIndex++;  
  248.   
  249.             //将日期的信息添加进Map,用来监听点击事件的触发条件  
  250.             String dateKey = rowIndex+"_"+dayWeekIndex;  
  251.             datePositionMap.put(dateKey, i + "");  
  252.   
  253.         }  
  254.     }  
  255.   
  256.     /** 
  257.      * 画圆形的背景 
  258.      * @param canvas 
  259.      * @param positionX 
  260.      * @param positionY 
  261.      * @param radius 
  262.      * @param color 
  263.      */  
  264.     private void drawCircle(Canvas canvas,int positionX, int positionY, int radius,int color){  
  265.         mPaint.setStyle(Paint.Style.FILL);  
  266.         mPaint.setColor(color);  
  267.         canvas.drawCircle(positionX, positionY, radius,mPaint);  
  268.     }  
  269.   
  270.     /**用来监听点击事件 
  271.      * 
  272.      * @param event 
  273.      * @return 
  274.      */  
  275.     @Override  
  276.     public boolean onTouchEvent(MotionEvent event) {  
  277.         int action = event.getAction();  
  278.         if(action==MotionEvent.ACTION_DOWN){  
  279.   
  280.             selectedDate = selectedDate(event);  
  281.             if(selectedDate!=null){  
  282.                 invalidate();  
  283.             }else {  
  284.                 selectedDate="0";  
  285.             }  
  286.         }  
  287.         return true;  
  288.   
  289.     }  
  290.   
  291.     /** 
  292.      * 处理单击事件,重绘选中的日期的背景 
  293.      * @param event 
  294.      * @return 
  295.      */  
  296.     private String selectedDate(MotionEvent event){  
  297.   
  298.         int paddingTop =  height/14+height /7;  
  299.         int column = (int)Math.ceil(event.getX()/(width/7))+1;  
  300.         int row = (int)Math.ceil((event.getY()-paddingTop)/(height/6));  
  301.   
  302.         String key = row+"_"+column;  
  303.         String clickDay = datePositionMap.get(key);  
  304.   
  305.         return clickDay;  
  306.     }  
  307. }  

猜你喜欢

转载自blog.csdn.net/qq_36946026/article/details/80759578
今日推荐