HTML5技术

【无私分享】干货!!!一个炫酷的自定义日历控件,摆脱日历时间选择烦恼,纯福利~ - 南尘(3)

字号+ 作者:H5之家 来源:H5之家 2016-08-12 12:00 我要评论( )

1 package com.example.nanchen.mydateviewdemo.view; android.annotation.SuppressLint; 4 import android.content.Context; 5 import android.graphics.Color; 6 import android.text.TextUtils.TruncateAt; 7 im

1 package com.example.nanchen.mydateviewdemo.view; android.annotation.SuppressLint; 4 import android.content.Context; 5 import android.graphics.Color; 6 import android.text.TextUtils.TruncateAt; 7 import android.util.AttributeSet; 8 import android.view.GestureDetector; 9 import android.view.GestureDetector.OnGestureListener; 10 import android.view.Gravity; 11 import android.view.MotionEvent; 12 import android.view.View; 13 import android.view.View.OnTouchListener; 14 import android.view.animation.Animation; 15 import android.view.animation.Animation.AnimationListener; 16 import android.view.animation.TranslateAnimation; 17 import android.widget.GridView; 18 import android.widget.ImageButton; 19 import android.widget.LinearLayout; 20 import android.widget.RelativeLayout; 21 import android.widget.TextView; 22 import android.widget.ViewFlipper; com.example.nanchen.mydateviewdemo.R; java.util.ArrayList; 27 import java.util.Calendar; 28 import java.util.Date; 29 import java.util.List; nanchen 33 * @date 16-8-10 上午11:40 CalendarView extends LinearLayout implements OnTouchListener, AnimationListener, OnGestureListener { * 点击日历 OnCalendarViewListener { 41 void onCalendarItemClick(CalendarView view, Date date); 42 } * 顶部控件所占高度 TOP_HEIGHT = 40; * 日历item中默认id从0xff0000开始 DEFAULT_ID = 0xff0000; SWIPE_MIN_DISTANCE = 120; SWIPE_MAX_OFF_PATH = 250; SWIPE_THRESHOLD_VELOCITY = 200; screenWidth; Animation slideLeftIn; 63 private Animation slideLeftOut; 64 private Animation slideRightIn; 65 private Animation slideRightOut; 66 private ViewFlipper viewFlipper; 67 private GestureDetector mGesture = null; * 上一月 GridView gView1; * 当月 GridView gView2; * 下一月 GridView gView3; bIsSelection = Calendar calStartDate = Calendar.getInstance();Calendar calSelected = Calendar.getInstance(); CalendarGridViewAdapter gAdapter; 86 private CalendarGridViewAdapter gAdapter1; 87 private CalendarGridViewAdapter gAdapter3; LinearLayout mMainLayout; iMonthViewCurrentMonth = 0; iMonthViewCurrentYear = 0; caltitleLayoutID = 66; calLayoutID = 55; Context mContext; * 标注日期 List<Date> markDates; OnCalendarViewListener mListener; CalendarView(Context context) { 107 this(context, null); 108 } CalendarView(Context context, AttributeSet attrs) { 111 super(context, attrs); mContext = context; 114 markDates = new ArrayList<>(); 115 init(); 116 } init() { screenWidth = mContext.getResources().getDisplayMetrics().widthPixels; slideLeftIn = new TranslateAnimation(screenWidth, 0, 0, 0); 125 slideLeftIn.setDuration(400); 126 slideLeftIn.setAnimationListener(this); 127 slideLeftOut = new TranslateAnimation(0, -screenWidth, 0, 0); 128 slideLeftOut.setDuration(400); 129 slideLeftOut.setAnimationListener(this); 130 slideRightIn = new TranslateAnimation(-screenWidth, 0, 0, 0); 131 slideRightIn.setDuration(400); 132 slideRightIn.setAnimationListener(this); 133 slideRightOut = new TranslateAnimation(0, screenWidth, 0, 0); 134 slideRightOut.setDuration(400); 135 slideRightOut.setAnimationListener(this); mGesture = new GestureDetector(mContext, this); UpdateStartDateForMonth(); setOrientation(LinearLayout.HORIZONTAL); 144 mMainLayout = new LinearLayout(mContext); 145 LinearLayout.LayoutParams main_params = new LinearLayout.LayoutParams( 146 LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 147 mMainLayout.setLayoutParams(main_params); 148 mMainLayout.setGravity(Gravity.CENTER_HORIZONTAL); 149 mMainLayout.setOrientation(LinearLayout.VERTICAL); 150 addView(mMainLayout); generateTopView(); generateWeekGirdView(); viewFlipper = new ViewFlipper(mContext); 160 RelativeLayout.LayoutParams fliper_params = new RelativeLayout.LayoutParams( 161 LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 162 fliper_params.addRule(RelativeLayout.BELOW, caltitleLayoutID); 163 mMainLayout.addView(viewFlipper, fliper_params); 164 generateClaendarGirdView(); LinearLayout br = new LinearLayout(mContext); 168 br.setBackgroundColor(Color.argb(0xff, 0xe3, 0xee, 0xf4)); 169 LinearLayout.LayoutParams params_br = new LinearLayout.LayoutParams( 170 LayoutParams.MATCH_PARENT, 3); 171 mMainLayout.addView(br, params_br); 172 } * 生成顶部控件 @SuppressWarnings("deprecation") generateTopView() { RelativeLayout top = new RelativeLayout(mContext); 181 top.setBackgroundColor(Color.argb(0xff, 0x0e, 0x6b, 0xc2)); 182 LinearLayout.LayoutParams top_params = new LinearLayout.LayoutParams( 183 LayoutParams.MATCH_PARENT, 184 ViewUtil.dip2px(mContext, TOP_HEIGHT)); 185 top.setLayoutParams(top_params); 186 mMainLayout.addView(top); mTitle = new TextView(mContext); 189 android.widget.RelativeLayout.LayoutParams title_params = new android.widget.RelativeLayout.LayoutParams( 190 android.widget.RelativeLayout.LayoutParams.MATCH_PARENT, 191 android.widget.RelativeLayout.LayoutParams.MATCH_PARENT); 192 mTitle.setLayoutParams(title_params); 193 mTitle.setTextColor(Color.WHITE); 194 mTitle.setTextSize(18); 195 mTitle.setFocusableInTouchMode(true); 196 mTitle.setMarqueeRepeatLimit(-1); 197 mTitle.setEllipsize(TruncateAt.MARQUEE); 198 mTitle.setSingleLine(true); 199 mTitle.setGravity(Gravity.CENTER); 200 mTitle.setHorizontallyScrolling(true); 201 mTitle.setText("2014年9月"); 202 top.addView(mTitle); ImageButton mLeftView = new ImageButton(mContext); mLeftView.setBackgroundResource(R.drawable.month_pre_selector); 220 android.widget.RelativeLayout.LayoutParams leftPP = new android.widget.RelativeLayout.LayoutParams( 221 ViewUtil.dip2px(mContext, 25), ViewUtil.dip2px(mContext, 22)); 222 leftPP.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 223 leftPP.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); 224 leftPP.setMargins(20, 0, 0, 0); 225 mLeftView.setLayoutParams(leftPP); 226 mLeftView.setOnClickListener(new OnClickListener() { 227 228 @Override onClick(View v) { viewFlipper.setInAnimation(slideRightIn); 232 viewFlipper.setOutAnimation(slideRightOut); 233 viewFlipper.showPrevious(); 234 setPrevViewItem(); 235 } 236 }); 237 top.addView(mLeftView); ImageButton mRightView = new ImageButton(mContext); mRightView.setBackgroundResource(R.drawable.month_next_selector); 255 256 android.widget.RelativeLayout.LayoutParams rightPP = new android.widget.RelativeLayout.LayoutParams( 257 ViewUtil.dip2px(mContext, 25), ViewUtil.dip2px(mContext, 22)); 258 rightPP.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 259 rightPP.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); 260 rightPP.setMargins(0, 0, 20, 0); 261 mRightView.setLayoutParams(rightPP); 262 mRightView.setOnClickListener(new OnClickListener() { 263 264 @Override onClick(View v) { viewFlipper.setInAnimation(slideLeftIn); 268 viewFlipper.setOutAnimation(slideLeftOut); 269 viewFlipper.showNext(); 270 setNextViewItem(); 271 } 272 }); 273 top.addView(mRightView); 274 } * 生成中间显示week generateWeekGirdView() { 280 GridView gridView = new GridView(mContext); 281 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( 282 LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 283 gridView.setLayoutParams(params); gridView.setGravity(Gravity.CENTER_VERTICAL);gridView.setVerticalSpacing(1);gridView.setHorizontalSpacing(1);gridView.setBackgroundColor(Color.argb(0xff, 0xe3, 0xee, 0xf4)); i = screenWidth / 7; 291 int j = screenWidth - (i * 7); 292 int x = j / 2; WeekGridAdapter weekAdapter = new WeekGridAdapter(mContext); mMainLayout.addView(gridView); 297 } * 生成底部日历 generateClaendarGirdView() { Calendar tempSelected2 = Calendar.getInstance(); Calendar tempSelected3 = Calendar.getInstance(); tempSelected1.setTime(calStartDate.getTime()); 307 tempSelected2.setTime(calStartDate.getTime()); 308 tempSelected3.setTime(calStartDate.getTime()); 309 310 gView1 = new CalendarGridView(mContext); 311 tempSelected1.add(Calendar.MONTH, -1); 312 gAdapter1 = new CalendarGridViewAdapter(mContext, tempSelected1, 313 markDates); gView1.setId(calLayoutID); 316 317 gView2 = new CalendarGridView(mContext); 318 gAdapter = new CalendarGridViewAdapter(mContext, tempSelected2, 319 markDates); gView2.setId(calLayoutID); 322 323 gView3 = new CalendarGridView(mContext); 324 tempSelected3.add(Calendar.MONTH, 1); 325 gAdapter3 = new CalendarGridViewAdapter(mContext, tempSelected3, 326 markDates); gView3.setId(calLayoutID); 329 330 gView2.setOnTouchListener(this); 331 gView1.setOnTouchListener(this); 332 gView3.setOnTouchListener(this); (viewFlipper.getChildCount() != 0) { 335 viewFlipper.removeAllViews(); 336 } 337 338 viewFlipper.addView(gView2); 339 viewFlipper.addView(gView3); 340 viewFlipper.addView(gView1); 341 342 String title = calStartDate.get(Calendar.YEAR) 343 + "年" 344 + NumberHelper.LeftPad_Tow_Zero(calStartDate 345 .get(Calendar.MONTH) + 1) + "月"; 346 mTitle.setText(title); 347 } setPrevViewItem() { 351 iMonthViewCurrentMonth--;// 当前选择月-- (iMonthViewCurrentMonth == -1) { 354 iMonthViewCurrentMonth = 11; 355 iMonthViewCurrentYear--; 356 } calStartDate.set(Calendar.MONTH, iMonthViewCurrentMonth); calStartDate.set(Calendar.YEAR, iMonthViewCurrentYear); } setNextViewItem() { 364 iMonthViewCurrentMonth++; 365 if (iMonthViewCurrentMonth == 12) { 366 iMonthViewCurrentMonth = 0; 367 iMonthViewCurrentYear++; 368 } 369 calStartDate.set(Calendar.DAY_OF_MONTH, 1); 370 calStartDate.set(Calendar.MONTH, iMonthViewCurrentMonth); 371 calStartDate.set(Calendar.YEAR, iMonthViewCurrentYear); 372 } 根据改变的日期更新日历 UpdateStartDateForMonth() { iMonthViewCurrentMonth = calStartDate.get(Calendar.MONTH);iMonthViewCurrentYear = calStartDate.get(Calendar.YEAR);// 得到当前日历显示的年 iDay = 0; 383 int iFirstDayOfWeek = Calendar.MONDAY; 384 int iStartDay = iFirstDayOfWeek; 385 if (iStartDay == Calendar.MONDAY) { 386 iDay = calStartDate.get(Calendar.DAY_OF_WEEK) - Calendar.MONDAY; 387 if (iDay < 0) 388 iDay = 6; 389 } 390 if (iStartDay == Calendar.SUNDAY) { 391 iDay = calStartDate.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY; 392 if (iDay < 0) 393 iDay = 6; 394 } 395 calStartDate.add(Calendar.DAY_OF_WEEK, -iDay); 396 } * 设置标注的日期 400 * markDates setMarkDates(List<Date> markDates) { 404 this.markDates.clear(); 405 this.markDates.addAll(markDates); 406 gAdapter.notifyDataSetChanged(); 407 gAdapter1.notifyDataSetChanged(); 408 gAdapter3.notifyDataSetChanged(); 409 } * 设置点击日历监听 413 * listener setOnCalendarViewListener(OnCalendarViewListener listener) { 417 this.mListener = listener; 418 } 419 420 @Override onDown(MotionEvent e) { ; 424 } 425 426 @SuppressLint("ClickableViewAccessibility") 427 @Override onTouch(View v, MotionEvent event) { 429 return mGesture.onTouchEvent(event); 430 } 431 432 @Override onFling(MotionEvent e1, MotionEvent e2, float velocityX, 434 float velocityY) { { 437 if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) ; (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE 441 && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 442 viewFlipper.setInAnimation(slideLeftIn); 443 viewFlipper.setOutAnimation(slideLeftOut); 444 viewFlipper.showNext(); 445 setNextViewItem(); ; (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE 449 && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 450 viewFlipper.setInAnimation(slideRightIn); 451 viewFlipper.setOutAnimation(slideRightOut); 452 viewFlipper.showPrevious(); 453 setPrevViewItem(); ; 455 456 } 457 } catch (Exception e) { } ; 461 } 462 463 @Override onLongPress(MotionEvent e) { } 468 469 @Override onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 471 float distanceY) { ; 474 } 475 476 @Override onShowPress(MotionEvent e) { } 481 482 @Override onSingleTapUp(MotionEvent e) { 484 // TODO Auto-generated method stub pos = gView2.pointToPosition((int) e.getX(), (int) e.getY()); 487 LinearLayout txtDay = (LinearLayout) gView2.findViewById(pos 488 + DEFAULT_ID); 489 if (txtDay != null) { 490 if (txtDay.getTag() != null) { 491 Date date = (Date) txtDay.getTag(); 492 calSelected.setTime(date); 493 494 gAdapter.setSelectedDate(calSelected); 495 gAdapter.notifyDataSetChanged(); 496 497 gAdapter1.setSelectedDate(calSelected); 498 gAdapter1.notifyDataSetChanged(); 499 500 gAdapter3.setSelectedDate(calSelected); 501 gAdapter3.notifyDataSetChanged(); 502 if (mListener != null) 503 mListener.onCalendarItemClick(this, date); 504 } 505 } ; 507 } 508 509 @Override onAnimationEnd(Animation animation) { generateClaendarGirdView(); 513 } 514 515 @Override onAnimationRepeat(Animation animation) { } 520 521 @Override onAnimationStart(Animation animation) { } 526 }

 

5)

GregorianUtil

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目 (14)附数据库、发布项目 - 果冻布丁喜

    【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目

    2016-07-30 12:00

  • 分享自己的超轻量级高性能ORM数据访问组件Deft,比dapper快20%以上 - 陌城心灵捕手

    分享自己的超轻量级高性能ORM数据访问组件Deft,比dapper快20%以上 -

    2016-07-29 14:00

  • 【S】【S】【S】一大波前端干货整合(一) - Van小时

    【S】【S】【S】一大波前端干货整合(一) - Van小时

    2016-07-28 15:00

  • 独立开发 一个社交 APP 的架构分享 (已实现) - 指尖下的幽灵

    独立开发 一个社交 APP 的架构分享 (已实现) - 指尖下的幽灵

    2016-07-19 16:00

网友点评