Android 自定义日历

好久没来写博客了,这半年多发生了好多的事情,废话不多说,今天在公司里比较闲在,写一篇最近写的公司用到的控件——日历控件。

控件的功能比较少,根据需求只有选择开始时间和结束时间并返回时间段。

效果图如下:

第一张图是正常状态下,第二张图是选中了一个开始日期的状态 第三张图片是选择了结束日期。该日历记录的是当前时间的前一个月的时间。

下面为代码:

  1 import android.content.Context;
  2 import android.content.res.TypedArray;
  3 import android.graphics.Canvas;
  4 import android.graphics.Paint;
  5 import android.graphics.Paint.Align;
  6 import android.graphics.Point;
  7 import android.graphics.PorterDuff;
  8 import android.graphics.PorterDuffXfermode;
  9 import android.graphics.Rect;
 10 import android.text.TextUtils;
 11 import android.util.AttributeSet;
 12 import android.util.Log;
 13 import android.view.MotionEvent;
 14 import android.view.View;
 15
 16 import java.text.SimpleDateFormat;
 17 import java.util.ArrayList;
 18 import java.util.Calendar;
 19 import java.util.Date;
 20 import java.util.List;
 21
 22 import playrtc.huanhong.com.test.R;
 23
 24 public class CustomDateView extends View {
 25
 26     private static final float TITLE_HEIGHT = 200;
 27     private float cellWidth;
 28     private float cellHeight;
 29     private int date[][] = new int[6][7];
 30     private int measuredHeight;
 31     private Point mStartDatePoint;
 32     private Point mEndDatePoint;
 33     private Point mShowMonthPoint;
 34     private Point mLastDayPoint;
 35
 36     private Paint mNormalTextPaint;
 37     private Paint mTitlePaint;
 38     private Paint mTitleTextPaint;
 39     private Paint mToadyTextPaint;
 40     private Paint mIndicatorPaint;
 41     private Paint mCirclePaintBg;
 42     private Paint mRectPaintBg;
 43     private Paint mCheckedPaint;
 44
 45     private float mTitleSize = 60;
 46     private float mDateSize = 50;
 47     private float mIndicatorSize = 30;
 48
 49     private int mTitleBackgroundColor = 0xff047dfe;
 50     private int mTitleTextColor = 0xffffffff;
 51     private int mDateTextColor = 0xff000000;
 52     private int mCheckedTextColor = 0xffffffff;
 53     private int mTodayColor = 0xff047dfe;
 54     private int mIndicatorColor = 0xff000000;
 55     private int mCircleColor = 0xFFff8900;
 56     private int mRectColor = 0xFFffedd9;
 57
 58     private float mWeekHeight;
 59     private String mDateStr;
 60     private String mCurrentMonthStr;
 61
 62     private String[] mWeeks = {"日", "一", "二", "三", "四", "五", "六"};
 63     private String[] mMonths = {"一月", "二月", "三月", "四月", "" +
 64             "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"};
 65     private int mCurrentYear;
 66     private int mCurrentMonth;
 67     private int mCurrentWeek;
 68     private int mCurrentDay;
 69
 70     private OnCheckedListener mOnCheckedListener;
 71
 72     public CustomDateView(Context context) {
 73         this(context, null);
 74     }
 75
 76     public CustomDateView(Context context, AttributeSet attrs) {
 77         super(context, attrs);
 78 //        setCurrentDate(System.currentTimeMillis());
 79         init(attrs);
 80     }
 81
 82     private void init(AttributeSet attr) {
 83
 84         TypedArray typedArray = getContext().obtainStyledAttributes(attr, R.styleable.CustomDateView);
 85
 86         try {
 87             mTitleSize = typedArray.getDimension(R.styleable.CustomDateView_title_text_size, mTitleSize);
 88             mDateSize = typedArray.getDimension(R.styleable.CustomDateView_date_text_size, mDateSize);
 89             mIndicatorSize = typedArray.getDimension(R.styleable.CustomDateView_indicator_text_size, mIndicatorSize);
 90
 91             mTitleBackgroundColor = typedArray.getColor(R.styleable.CustomDateView_title_background_color, mTitleBackgroundColor);
 92             mTitleTextColor = typedArray.getColor(R.styleable.CustomDateView_title_text_color, mTitleTextColor);
 93             mDateTextColor = typedArray.getColor(R.styleable.CustomDateView_date_text_color, mDateTextColor);
 94             mCheckedTextColor = typedArray.getColor(R.styleable.CustomDateView_checked_text_color, mCheckedTextColor);
 95             mTodayColor = typedArray.getColor(R.styleable.CustomDateView_today_text_color, mTodayColor);
 96             mIndicatorColor = typedArray.getColor(R.styleable.CustomDateView_indicator_text_color, mIndicatorColor);
 97             mCircleColor = typedArray.getColor(R.styleable.CustomDateView_checked_circle_color, mCircleColor);
 98             mRectColor = typedArray.getColor(R.styleable.CustomDateView_checked_background_color, mRectColor);
 99         } catch (Exception e) {
100             e.printStackTrace();
101         } finally {
102             typedArray.recycle();
103         }
104
105
106         //上部蓝色
107         mTitlePaint = new Paint();
108         mTitlePaint.setAntiAlias(true);
109         mTitlePaint.setColor(mTitleBackgroundColor);
110
111         //上部文字
112         mTitleTextPaint = new Paint();
113         mTitleTextPaint.setColor(mTitleTextColor);
114         mTitleTextPaint.setAntiAlias(true);
115         mTitleTextPaint.setTextSize(mTitleSize);
116         mTitleTextPaint.setTextAlign(Align.CENTER);
117
118         //其他文字
119         mNormalTextPaint = new Paint();
120         mNormalTextPaint.setColor(mDateTextColor);
121         mNormalTextPaint.setTextAlign(Align.CENTER);
122         mNormalTextPaint.setAntiAlias(true);
123         mNormalTextPaint.setTextSize(mDateSize);
124
125         //选中文字
126         mCheckedPaint = new Paint();
127         mCheckedPaint.setColor(mCheckedTextColor);
128         mCheckedPaint.setTextAlign(Align.CENTER);
129         mCheckedPaint.setAntiAlias(true);
130         mCheckedPaint.setTextSize(mDateSize);
131
132         //今天显示的字体
133         mToadyTextPaint = new Paint();
134         mToadyTextPaint.setAntiAlias(true);
135         mToadyTextPaint.setTextAlign(Align.CENTER);
136         mToadyTextPaint.setColor(mTodayColor);
137         mToadyTextPaint.setTextSize(mDateSize);
138
139         //交界处显示的字体
140         mIndicatorPaint = new Paint();
141         mIndicatorPaint.setAntiAlias(true);
142         mIndicatorPaint.setTextAlign(Align.CENTER);
143         mIndicatorPaint.setColor(mIndicatorColor);
144         mIndicatorPaint.setTextSize(mIndicatorSize);
145
146         //圆形
147         mCirclePaintBg = new Paint();
148         mCirclePaintBg.setColor(mCircleColor);
149         mCirclePaintBg.setAntiAlias(true);
150
151         //矩形
152         mRectPaintBg = new Paint();
153         mRectPaintBg.setColor(mRectColor);
154         mRectPaintBg.setAntiAlias(true);
155     }
156
157     @Override
158     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
159         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
160         cellWidth = (getMeasuredWidth() - getPaddingLeft() - getPaddingRight()) / date[0].length;
161         mWeekHeight = cellWidth;
162         cellHeight = (cellWidth + mIndicatorSize);
163         measuredHeight = (int) (cellHeight * (date.length + 1) + TITLE_HEIGHT + getPaddingBottom());
164         setMeasuredDimension(widthMeasureSpec, measuredHeight);
165     }
166
167     @Override
168     protected void onDraw(Canvas canvas) {
169         drawRect(canvas);
170         drawCircle(canvas);
171
172         mTitlePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
173         canvas.drawRect(0, 0, getMeasuredWidth(), TITLE_HEIGHT, mTitlePaint);
174
175         Rect rect = new Rect();
176         Rect rectWeek = new Rect();
177
178         if (!TextUtils.isEmpty(mDateStr)) {
179             mTitleTextPaint.getTextBounds(mDateStr, 0, mDateStr.length(), rect);
180             canvas.drawText(mDateStr, getMeasuredWidth() / 2, TITLE_HEIGHT / 2 + rect.height() / 2,
181                     mTitleTextPaint);
182         }
183
184         drawWeekText(canvas, rectWeek);
185
186         drawDateText(canvas, rect);
187
188         if (!TextUtils.isEmpty(mCurrentMonthStr)) {
189             mIndicatorPaint.getTextBounds(mCurrentMonthStr, 0, mCurrentMonthStr.length(), rect);
190         }
191
192         if (mShowMonthPoint != null) {
193             canvas.drawText(mCurrentMonthStr,
194                     mShowMonthPoint.x * cellWidth + cellWidth / 2 + getPaddingLeft(),
195                     (mShowMonthPoint.y + 1) * cellHeight + mWeekHeight + rect.height() / 4 + TITLE_HEIGHT, mIndicatorPaint);
196         }
197 //        //竖线
198 //        for (int i = 0; i <= date[0].length; i++) {
199 //            canvas.drawLine(i * cellWidth + getPaddingLeft(), TITLE_HEIGHT, i * cellWidth + getPaddingLeft(), measuredHeight - getPaddingBottom(), mNormalTextPaint);
200 //        }
201 //
202 //        //横线
203 //        for (int i = 0; i < date.length; i++) {
204 //            canvas.drawLine(getPaddingLeft(), i * cellHeight + TITLE_HEIGHT + mWeekHeight, getMeasuredWidth() - getPaddingRight(),
205 //                    i * cellHeight + mWeekHeight + TITLE_HEIGHT, mNormalTextPaint);
206 //        }
207     }
208
209     /**
210      * @param canvas   画布
211      * @param rectWeek 测量文字长宽矩形
212      */
213     private void drawWeekText(Canvas canvas, Rect rectWeek) {
214         int maxHeight = 0;
215         //获得最高的文字高度
216         for (int k = 0; k < mWeeks.length; k++) {
217             mNormalTextPaint.getTextBounds(mWeeks[k], 0, mWeeks[k].length(), rectWeek);
218             if (rectWeek.height() > maxHeight) {
219                 maxHeight = rectWeek.height();
220             }
221         }
222
223         for (int k = 0; k < mWeeks.length; k++) {
224             canvas.drawText(mWeeks[k],
225                     k * cellWidth + cellWidth / 2 + getPaddingLeft(),
226                     mWeekHeight / 2 + maxHeight / 2 + TITLE_HEIGHT, mNormalTextPaint);
227         }
228     }
229
230     /**
231      * @param canvas 画布
232      * @param rect   测量文字长宽矩形
233      */
234     private void drawDateText(Canvas canvas, Rect rect) {
235         for (int i = 0; i < date.length; i++) {
236             for (int j = 0; j < date[0].length; j++) {
237
238                 mNormalTextPaint.getTextBounds(date[i][j] + "", 0, String.valueOf(date[i][j]).length(), rect);
239
240                 if (date[i][j] != 0) {
241
242                     if (i == mLastDayPoint.x && j == mLastDayPoint.y) {
243                         canvas.drawText(date[i][j] + "",
244                                 j * cellWidth + cellWidth / 2 + getPaddingLeft(),
245                                 i * cellHeight + cellHeight / 2 + mWeekHeight + rect.height() / 2 + TITLE_HEIGHT, mToadyTextPaint);
246                     } else {
247
248                         if ((mStartDatePoint != null && i == mStartDatePoint.y && j == mStartDatePoint.x) || (mEndDatePoint != null && i == mEndDatePoint.y && j == mEndDatePoint.x)) {
249                             canvas.drawText(date[i][j] + "",
250                                     j * cellWidth + cellWidth / 2 + getPaddingLeft(),
251                                     i * cellHeight + cellHeight / 2 + mWeekHeight + rect.height() / 2 + TITLE_HEIGHT, mCheckedPaint);
252                         } else {
253                             canvas.drawText(date[i][j] + "",
254                                     j * cellWidth + cellWidth / 2 + getPaddingLeft(),
255                                     i * cellHeight + cellHeight / 2 + mWeekHeight + rect.height() / 2 + TITLE_HEIGHT, mNormalTextPaint);
256                         }
257                     }
258                 }
259
260             }
261         }
262     }
263
264     @Override
265     public boolean onTouchEvent(MotionEvent event) {
266
267         switch (event.getAction()) {
268             case MotionEvent.ACTION_DOWN:
269
270                 if (mStartDatePoint != null && mEndDatePoint != null) {
271                     break;
272                 }
273
274                 if (mStartDatePoint == null) {
275                     if (getPoint(event.getX(), event.getY()) != null) {
276                         mStartDatePoint = getPoint(event.getX(), event.getY());
277                     }
278                 } else {
279                     Point point1 = getPoint(event.getX(), event.getY());
280                     if (point1 != null) {
281                         if (point1.y < mStartDatePoint.y) {
282                             mStartDatePoint = point1;
283                         } else if (point1.x <= mStartDatePoint.x && point1.y == mStartDatePoint.y) {
284                             mStartDatePoint = point1;
285                         } else {
286                             mEndDatePoint = point1;
287
288                             if (mOnCheckedListener != null) {
289                                 mOnCheckedListener.onChecked();
290                             }
291
292                         }
293                     }
294                 }
295
296                 invalidate();
297                 break;
298
299             default:
300                 break;
301         }
302         return true;
303     }
304
305     private Point getPoint(float x, float y) {
306         Point point = new Point();
307         point.x = (int) ((x - getPaddingLeft()) / cellWidth);
308         point.y = (int) ((y - TITLE_HEIGHT - mWeekHeight) / cellHeight);
309
310         Log.d("fxxk", "进入getPoint x = " + point.x + "y = " + point.y);
311         if (point.y < date.length && point.x < date[0].length && point.y >= 0 && point.x >= 0) {
312             if (date[point.y][point.x] == 0) {
313                 return null;
314             }
315         } else {
316             return null;
317         }
318
319         return point;
320
321     }
322
323     private void drawCircle(Canvas canvas) {
324         if (mStartDatePoint != null) {
325             canvas.drawCircle(mStartDatePoint.x * cellWidth + cellWidth / 2 + getPaddingLeft(), mStartDatePoint.y * cellHeight + cellHeight / 2 + mWeekHeight + TITLE_HEIGHT,
326                     (float) (cellHeight * 0.3), mCirclePaintBg);
327         }
328
329         if (mEndDatePoint != null) {
330             canvas.drawCircle(mEndDatePoint.x * cellWidth + cellWidth / 2 + getPaddingLeft(),
331                     mEndDatePoint.y * cellHeight + cellHeight / 2 + mWeekHeight + TITLE_HEIGHT, (float) (cellHeight * 0.3), mCirclePaintBg);
332         }
333     }
334
335     private void drawRect(Canvas canvas) {
336         if (mEndDatePoint != null && mStartDatePoint != null) {
337
338             int total = date[0].length - mStartDatePoint.x + 1 + (mEndDatePoint.y - mStartDatePoint.y) * date[0].length - date[0].length + mEndDatePoint.x;
339             int beginX = mStartDatePoint.x;
340             int beginY = mStartDatePoint.y;
341             for (int i = 0; i < total; i++) {
342
343                 if (i == 0) {
344                     canvas.drawRect(beginX * cellWidth + cellWidth / 2 + getPaddingLeft(), (float) (beginY * cellHeight + TITLE_HEIGHT + mWeekHeight + 0.21 * cellHeight),
345                             beginX * cellWidth + cellWidth + getPaddingLeft(), (float) (beginY * cellHeight + cellHeight + mWeekHeight + TITLE_HEIGHT - 0.21 * cellHeight), mRectPaintBg);
346                 } else if (i == total - 1) {
347                     canvas.drawRect(beginX * cellWidth + getPaddingLeft(), (float) (beginY * cellHeight + TITLE_HEIGHT + mWeekHeight + 0.21 * cellHeight),
348                             beginX * cellWidth + cellWidth / 2 + getPaddingLeft(), (float) (beginY * cellHeight + cellHeight + mWeekHeight + TITLE_HEIGHT - 0.21 * cellHeight),
349                             mRectPaintBg);
350                 } else {
351                     canvas.drawRect(beginX * cellWidth + getPaddingLeft(), (float) (beginY * cellHeight + TITLE_HEIGHT + mWeekHeight + 0.21 * cellHeight),
352                             beginX * cellWidth + cellWidth + getPaddingLeft(), (float) (beginY * cellHeight + cellHeight + mWeekHeight + TITLE_HEIGHT - 0.21 * cellHeight), mRectPaintBg)
353                     ;
354                 }
355                 beginX++;
356                 if (beginX % date[0].length == 0) {
357                     beginX = 0;
358                     beginY++;
359                 }
360
361             }
362         }
363     }
364
365     private int distance(Point point1, Point point2) {
366         int count = 0;
367
368         if (point2.y > point1.y) {
369             count = date.length - point1.x + 1 + (point2.y - point1.y) * date.length - date.length + point2.x;
370         } else if (point2.y == point1.y) {
371             count = Math.abs(point2.x - point1.x);
372         } else if (point2.y < point1.y) {
373             count = date.length - point2.x + 1 + (point1.y - point2.y) * date.length - date.length + point1.x;
374         }
375
376         return count;
377     }
378
379     /**
380      * 设置当前时间
381      */
382     public void setCurrentDate(long time) {
383         Date date1 = new Date(time);
384
385         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
386         mDateStr = dateFormat.format(date1);
387
388         Calendar calendar = Calendar.getInstance();
389         calendar.setTime(date1);
390         mCurrentYear = calendar.get(Calendar.YEAR);
391         mCurrentMonth = calendar.get(Calendar.MONTH);
392         mCurrentWeek = calendar.get(Calendar.DAY_OF_WEEK);
393         mCurrentDay = calendar.get(Calendar.DAY_OF_MONTH);
394         mCurrentMonthStr = mMonths[mCurrentMonth];
395
396         if (mCurrentWeek <= 2) {
397             date = new int[6][7];
398         } else {
399             date = new int[5][7];
400         }
401
402         int currentNoWeek = date.length;
403
404         date[currentNoWeek - 1][mCurrentWeek - 1] = mCurrentDay;
405
406         mLastDayPoint = new Point(currentNoWeek - 1, mCurrentWeek - 1);
407
408         for (int i = 30; i > 0; i--) {
409             mCurrentWeek--;
410             if (mCurrentWeek == 0) {
411                 currentNoWeek--;
412                 mCurrentWeek = 7;
413             }
414
415
416             mCurrentDay--;
417
418             if (mCurrentDay == 0) {
419
420                 if ((mCurrentYear % 4 == 0 && mCurrentYear % 100 != 0)
421                         || (mCurrentYear % 400 == 0)) {
422                     switch (mCurrentMonth + 1) {
423                         case 3:
424                             mCurrentDay = 29;
425                             break;
426                         case 5:
427                         case 7:
428                         case 10:
429                         case 12:
430                             mCurrentDay = 30;
431                             break;
432                         case 1:
433                         case 2:
434                         case 4:
435                         case 6:
436                         case 9:
437                         case 8:
438                         case 11:
439                             mCurrentDay = 31;
440                             break;
441                         default:
442                             break;
443                     }
444                 } else {
445                     switch (mCurrentMonth + 1) {
446                         case 3:
447                             mCurrentDay = 28;
448                             break;
449                         case 5:
450                         case 7:
451                         case 10:
452                         case 12:
453                             mCurrentDay = 30;
454                             break;
455                         case 1:
456                         case 2:
457                         case 4:
458                         case 6:
459                         case 9:
460                         case 8:
461                         case 11:
462                             mCurrentDay = 31;
463                             break;
464                         default:
465                             break;
466                     }
467                 }
468             }
469
470             if (mCurrentDay == 1) {
471                 mShowMonthPoint = new Point(mCurrentWeek - 1, currentNoWeek - 1);
472                 Log.d("fxxk", (currentNoWeek - 1) + "   " + (mCurrentWeek - 1));
473             }
474
475             date[currentNoWeek - 1][mCurrentWeek - 1] = mCurrentDay;
476         }
477
478         invalidate();
479     }
480
481     public Calendar getStartDate() {
482         Calendar calendar = Calendar.getInstance();
483         if ((mStartDatePoint.x >= mShowMonthPoint.x && mStartDatePoint.y == mShowMonthPoint.y) || mStartDatePoint.y > mShowMonthPoint.y) {
484             calendar.set(mCurrentYear, mCurrentMonth, date[mStartDatePoint.y][mStartDatePoint.x]);
485         } else if ((mStartDatePoint.x < mShowMonthPoint.x && mStartDatePoint.y == mShowMonthPoint.y) || mStartDatePoint.y < mShowMonthPoint.y) {
486             calendar.set(mCurrentYear, mCurrentMonth - 1, date[mStartDatePoint.y][mStartDatePoint.x]);
487         }
488
489         return calendar;
490     }
491
492     public Calendar getEndDate() {
493         Calendar calendar = Calendar.getInstance();
494         if ((mEndDatePoint.x >= mShowMonthPoint.x && mEndDatePoint.y == mShowMonthPoint.y) || mEndDatePoint.y > mShowMonthPoint.y) {
495             calendar.set(mCurrentYear, mCurrentMonth, date[mEndDatePoint.y][mEndDatePoint.x]);
496         } else if ((mEndDatePoint.x < mShowMonthPoint.x && mEndDatePoint.y == mShowMonthPoint.y) || mEndDatePoint.y < mShowMonthPoint.y) {
497             calendar.set(mCurrentYear, mCurrentMonth - 1, date[mEndDatePoint.y][mEndDatePoint.x]);
498         }
499
500         return calendar;
501     }
502
503     public List<Integer> getDurationNumbers() {
504
505         List<Integer> integerList = new ArrayList<>();
506
507         if (mEndDatePoint != null && mStartDatePoint != null) {
508
509             int total = date[0].length - mStartDatePoint.x + 1 + (mEndDatePoint.y - mStartDatePoint.y) * date[0].length - date[0].length + mEndDatePoint.x;
510             int beginX = mStartDatePoint.x;
511             int beginY = mStartDatePoint.y;
512             for (int i = 0; i < total; i++) {
513
514                 integerList.add(date[beginY][beginX]);
515
516                 beginX++;
517                 if (beginX % date[0].length == 0) {
518                     beginX = 0;
519                     beginY++;
520                 }
521
522             }
523         }
524
525         return integerList;
526     }
527
528     public void setOnCheckedListener(OnCheckedListener onCheckedListener) {
529         this.mOnCheckedListener = onCheckedListener;
530     }
531
532
533     public interface OnCheckedListener {
534         void onChecked();
535     }
536 }

主要代码如上,最主要的还是canvas这个画布,以及使用canvas进行绘制。其中还是用了

Paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));这个用于设置相交模式,便于圆角的实现。下面是自定义属性:
<resources>
    <declare-styleable name="CustomDateView">
        <attr name="date_text_size" format="dimension" />
        <attr name="indicator_text_size" format="dimension" />
        <attr name="title_text_size" format="dimension" />
        <attr name="checked_circle_color" format="color" />
        <attr name="checked_background_color" format="color" />
        <attr name="title_background_color" format="color" />
        <attr name="title_text_color" format="color" />
        <attr name="checked_text_color" format="color" />
        <attr name="date_text_color" format="color" />
        <attr name="today_text_color" format="color" />
        <attr name="indicator_text_color" format="color" />
    </declare-styleable>
</resources>

自定义属性主要设置了背景色以及所有字体大小等等属性,这里不进行赘述。

使用方法:

layout布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:orientation="vertical">

    <com.test.view.CustomDateView xmlns:date="http://schemas.android.com/apk/res-auto"
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_corner"
        android:paddingLeft="10dp"
        android:paddingRight="10dp" />

</LinearLayout>

activity:

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;

import playrtc.huanhong.com.test.R;
import playrtc.huanhong.com.test.view.CustomDateView;

/**
 * Created by nick on 2016/6/14.
 */
public class DateActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_date);

        final Dialog dialog = new Dialog(this, R.style.dialog);
        View view = View.inflate(this, R.layout.layout_date, null);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(view);
        dialog.show();

//        Rotate3DAnimation rotate3DAnimation = new Rotate3DAnimation(0, 360);
//        rotate3DAnimation.setDuration(3000);

        final CustomDateView customDateView = (CustomDateView) view.findViewById(R.id.date);
        customDateView.setCurrentDate(System.currentTimeMillis());
        customDateView.setOnCheckedListener(new CustomDateView.OnCheckedListener() {
            @Override
            public void onChecked() {
                customDateView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        dialog.dismiss();
                    }
                }, 1000);
            }
        });

//        customDateView.startAnimation(rotate3DAnimation);
        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
    }

    @Override
    public void onBackPressed() {
        moveTaskToBack(false);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(false);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

其中用到了一个dialog的style

<style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <!--边框-->
        <item name="android:windowIsFloating">true</item>
        <!--是否浮现在activity之上-->
        <item name="android:windowIsTranslucent">false</item>
        <!--半透明-->
        <item name="android:windowNoTitle">true</item>
        <!--无标题-->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!--背景透明-->
        <item name="android:backgroundDimEnabled">true</item>
        <!--模糊-->
    </style>

好了,主要就是这样。

时间: 2024-10-10 19:19:27

Android 自定义日历的相关文章

android自定义日历

前几天闲来无事,变想做一些小工具玩玩.花了一天多的时间,弄出一个简单日历的View.分为月份模式和星期模式.滚动查看,先上图看看: 上面的是显示的是月份的模式.下面是星期的模式: 一个很简单的自定义View,然后通过Viewpager的OnpageChangeListener进行刷新View的数据.Viewpager通过轮回使用View.我默认设置是5个.可以左右无限切换. 下面是自定义CalendarView: package com.example.calendar; import andr

android自定义日历的使用方法

子夜枯灯123提供了一个自定义的android日历,他的博客地址如下: http://blog.csdn.net/huangyanbin123/article/details/38350213 他提供的代码除了自定义日历外,还有一个demo,对于大部分人来说,仅仅需要展示一个日历,并响应日历的click事件即可.因此我简化了他的demo,给出了一个更加简单的demo和使用方法,方法分为三步: 第一步:导入 http://download.csdn.net/detail/huangyanbin12

Android自定义日历,可以点击、标注日期、节气、旧历等

1. [图片] 9A59974C-47D4-47E3-8136-3F873EB9BBDC.jpg 2. [图片] left_arrow_pre.png 3. [图片] left_arrow.png 4. [图片] right_arrow_pre.png 5. [图片] right_arrow.png /****************从此出开始将代码拷贝到一个文件中*******************/ package cc.util.android.view; import java.tex

android 自定义日历控件

日历控件View: [java] view plaincopyprint? /** * 日历控件 功能:获得点选的日期区间 * */ public class CalendarView extends View implements View.OnTouchListener { private final static String TAG = "anCalendar"; private Date selectedStartDate; private Date selectedEndD

Android自定义可标记日历

"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> Android自定义可标记日历 - 会飞的徐公明 - 博客频道 - CSDN.NET 会飞的徐公明 目录视图 摘要视图 订阅 [活动]2017 CSDN博客专栏评选 &nbsp [5月书讯]流畅的Python,终于等到你!    &nbsp CSDN日报201

Android自定义View(CustomCalendar-定制日历控件)

转载请标明出处: http://blog.csdn.net/xmxkf/article/details/54020386 本文出自:[openXu的博客] 目录: 1分析 2自定义属性 3onMeasure 4onDraw 绘制月份 绘制星期 绘制日期及任务 5事件处理 源码下载 ??应项目需求,需要做一个日历控件,效果图如下: ???? ??接到需求后,没有立即查找是否有相关开源日历控件可用.系统日历控件是否能满足 ,第一反应就是这个控件该怎么画?谁叫咱自定义控件技术牛逼呢O(∩_∩)O哈哈~

Android自定义组件之日历控件-精美日历实现(内容、样式可扩展)

需求 我们知道,Android系统本身有自带的日历控件,网络上也有很多开源的日历控件资源,但是这些日历控件往往样式较单一,API较多,不易于在实际项目中扩展并实现出符合具体样式风格的,内容可定制的效果.本文通过自定义日历控件,实现了在内容和样式上可高度扩展的精美日历demo,有需要的Android应用开发人员可迅速移植并按需扩展实现. 在某个应用中,需要查询用户的历史考勤记录,根据实际考勤数据在日历中标记出不同的状态(如正常出勤.请假.迟到等),并在页面中显示相应的说明文字. 效果 实现的效果如

[Android]自定义简易版日历控件

先来看看效果图,看看是不是各位大佬想要的: 特别的功能并不多,重点是讲解简易日历该如何构造,假若是项目着急要用的话,最好还是找一下其它人写好的日历(附加滑动改变日历日期等功能) ---------------------------------------------------------------------------------------华丽的分割线--------------------------------------------------------------------

自定义日历控件-CalendarView

转载请注明出处: http://blog.csdn.net/forwardyzk/article/details/43056675 我们在开发中会遇到使用到日历控件,下面就介绍一个自定义日历控件. 思路: 1.自定义类CalendarView继承LinearLayout,使用布局文件,显示布局. 2.使用ViewFlipper,里面添加GridView,当月的日期. 3.使用手势GestureDetector,控制ViewFlipper的滑动. calen_calendar.xml <span