日历控件的绘制

/**
 * 
 * Copyright (c) 2012 All rights reserved
 * 描述:日历控件单元格绘制类
 * @author LiuZiwei
 * @date:2014-4-28
 */
@SuppressLint("ViewConstructor")
public class CalendarCell extends View {
	
	// 字体大小
	/** The text size. */
	private int textSize = 22;
	
	// 基本元素
	/** The m on item click listener. */
	private AbOnItemClickListener mOnItemClickListener;
	
	/** The pt. */
	private Paint pt = new Paint();
	
	/** The rect. */
	private RectF rect = new RectF();
	
	//显示的文字
	/** The text date value. */
	private String textDateValue = "";

	// 当前日期
	/** The i date year. */
	private int iDateYear = 0;
	
	/** The i date month. */
	private int iDateMonth = 0;
	
	/** The i date day. */
	private int iDateDay = 0;

	// 布尔变量
	/** The is selected. */
	private boolean isSelected = false;
	
	/** The is active month. */
	private boolean isActiveMonth = false;
	
	/** The is today. */
	private boolean isToday = false;
	
	/** The b touched down. */
	private boolean bTouchedDown = false;
	
	/** The has record. */
	private boolean hasRecord = false;
	
	//当前cell的序号
	/** The position. */
	private int position = 0;

	/** The anim alpha duration. */
	public static int ANIM_ALPHA_DURATION = 100;
	
	/*被选中的cell颜色*/
	/** The select cell color. */
	private int selectCellColor = Color.rgb(150, 195, 70);
	
	/*最大背景颜色*/
	/** The bg color. */
	
	private int bgColor =Color.rgb(255,255,255);
	
	/*本月可选的背景色*/
	
	private int iMonthColor=Color.rgb(228,228,228);
	
	/*数字颜色*/
	/** The number color. */
	private int numberColor = Color.rgb(86, 86, 86);

	/*非本月的数字颜色,白色*/
	/** The cell color. */
	private int notActiveMonthColor =  Color.rgb(255,255,255);//Color.rgb(228,228,228);

	/*不可选的表格的背景色*/
	/** The not active month color. */
	private int cellColor = Color.rgb(248,248,248);
	
	/*今天的表格颜色,蓝色*/
	/** The today color. */
	private int todayColor = Color.rgb(150, 200, 220);
	
	private int orager=Color.rgb(252, 124, 26);
	
	private boolean isLatDay=false;
	private boolean isOrange=false;
	
	// 构造函数
	/**
	 * Instantiates a new calendar cell.
	 *
	 * @param context the context
	 * @param position the position
	 * @param iWidth the i width
	 * @param iHeight the i height
	 */
	public CalendarCell(Context context, int position,int iWidth, int iHeight) {
		super(context);
		setFocusable(true);
		setLayoutParams(new LayoutParams(iWidth, iHeight));
		this.position = position;
		if(MY.PHONEWIDTH>1000){
			textSize=30;
		}
	}

	/**
	 * 描述:获取这个Cell的日期.
	 *
	 * @return the this cell date
	 */
	public Calendar getThisCellDate() {
		Calendar calDate = Calendar.getInstance();
		calDate.clear();
		calDate.set(Calendar.YEAR, iDateYear);
		calDate.set(Calendar.MONTH, iDateMonth);
		calDate.set(Calendar.DAY_OF_MONTH, iDateDay);
		return calDate;
	}

	/**
	 * 描述:设置这个Cell的日期.
	 *
	 * @param iYear the i year
	 * @param iMonth the i month
	 * @param iDay the i day
	 * @param isToday the is today
	 * @param isSelected the is selected
	 * @param isHoliday the is holiday
	 * @param isActiveMonth the is active month
	 * @param hasRecord the has record
	 */
	public void setThisCellDate(int iYear, int iMonth, int iDay, Boolean isToday,Boolean isSelected,
			Boolean isHoliday, int isActiveMonth, boolean isLatDay,boolean isOrange,boolean hasRecord) {
		iDateYear = iYear;
		iDateMonth = iMonth;
		iDateDay = iDay;
		
		this.isLatDay=isLatDay;
		this.isOrange=isOrange;
		
		this.textDateValue = Integer.toString(iDateDay);
		this.isActiveMonth = (iDateMonth == isActiveMonth);		
		this.isToday = isToday;
		this.hasRecord = hasRecord;
		this.isSelected = isSelected;
		
	}
	
	public int getiDateDay() {
		return iDateDay;
	}

	public void setiDateDay(int iDateDay) {
		this.iDateDay = iDateDay;
	}
	
	public int getiDateMonth() {
		return iDateMonth;
	}

	public void setiDateMonth(int iDateMonth) {
		this.iDateMonth = iDateMonth;
	}

	/**
	 * 描述:重载绘制方法.
	 *
	 * @param canvas the canvas
	 * @see android.view.View#onDraw(android.graphics.Canvas)
	 */
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
        Log.e("从新绘制", "重新绘制");
		canvas.drawColor(bgColor);
		rect.set(0, 0, this.getWidth(), this.getHeight());
		rect.inset(0.5f, 0.5f);

		final boolean bFocused = IsViewFocused();

		drawDayView(canvas, bFocused);
		drawDayNumber(canvas);
	}

	/**
	 * Checks if is view focused.
	 *
	 * @return true, if successful
	 */
	public boolean IsViewFocused() {
		return (this.isFocused() || bTouchedDown);
	}

	/**
	 * 描述:绘制日历方格.
	 *
	 * @param canvas the canvas
	 * @param bFocused the b focused
	 */
	private void drawDayView(Canvas canvas, boolean bFocused) {
        Log.e("绘制", "绘制");
		pt.setColor(getCellColor());
		canvas.drawRect(rect, pt);

	}

	/**
	 * 描述:绘制日历中的数字.
	 *
	 * @param canvas the canvas
	 */
	public void drawDayNumber(Canvas canvas) {
		// draw day number
		pt.setTypeface(null);
		pt.setAntiAlias(true);
		pt.setShader(null);
		pt.setFakeBoldText(true);
		pt.setTextSize(textSize);
		pt.setColor(numberColor);
		pt.setUnderlineText(false);
		
		if (!isActiveMonth || isLatDay){
			
			pt.setColor(notActiveMonthColor);
			
		}
		if(isOrange){

			pt.setColor(orager);
			
		}

		final int iPosX = (int) rect.left + ((int) rect.width() >> 1) - ((int) pt.measureText(textDateValue) >> 1);
		final int iPosY = (int) (this.getHeight() - (this.getHeight() - getTextHeight()) / 2 - pt.getFontMetrics().bottom);
		canvas.drawText(textDateValue, iPosX, iPosY, pt);
	}

	/**
	 * 描述:得到字体高度.
	 *
	 * @return the text height
	 */
	private int getTextHeight() {
		return (int) (-pt.ascent() + pt.descent());
	}

	/**
	 * 描述:根据条件返回不同颜色值.
	 *
	 * @return the cell color
	 */
	public int getCellColor() {
		if (isToday){
			return todayColor;
		}
		
		if (isSelected){
			return selectCellColor;
		}
		if(!isActiveMonth || isLatDay){
			
			return iMonthColor;
		}
		//如需周末有特殊背景色
		/*if (isHoliday){
			
		   return cellColor;
		}
		*/
		//默认是白色的单元格
		return cellColor;
	}

	/**
	 * 描述:设置是否被选中.
	 *
	 * @param selected the new selected
	 */
	@Override
	public void setSelected(boolean selected) {
		
		 
		if (this.isSelected != selected) {
			
			this.isSelected = selected;
			
			String date=CalendarView.getStrDateAtPosition(position);

			if(selected){
				if(!PublishWiFi.listPostion.contains(date)){

					PublishWiFi.listPostion.add(date);
					
				}else{
					
					for(int i=0;i<PublishWiFi.listPostion.size();i++){
						
						if(PublishWiFi.listPostion.get(i).equals(date)){
							
							PublishWiFi.listPostion.remove(i);
							
							setSelected(false);
							
							break;
						}
						
					}
				}	
		}
			
	}
}

	
	/**
	 * 描述:设置是否有数据.
	 *
	 * @param hasRecord the new checks for record
	 */
	public void setHasRecord(boolean hasRecord) {
		if (this.hasRecord != hasRecord) {
			this.hasRecord = hasRecord;
			this.invalidate();
		}
	}

	/**
	 * 描述:设置点击事件.
	 *
	 * @param onItemClickListener the new on item click listener
	 */
	public void setOnItemClickListener(AbOnItemClickListener onItemClickListener) {
		this.mOnItemClickListener = onItemClickListener;
	}

	/**
	 * 描述:执行点击事件.
	 */
	public void doItemClick() {
		if (mOnItemClickListener != null){
			mOnItemClickListener.onClick(position);
	    }
	}

	/**
	 * 描述:TODO.
	 *
	 * @param event the event
	 * @return true, if successful
	 * @see android.view.View#onTouchEvent(android.view.MotionEvent)
	 * @author: zhaoqp
	 * @date:2013-7-19 下午4:31:18
	 * @version v1.0
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		boolean bHandled = false;
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			bHandled = true;
			bTouchedDown = true;
			invalidate();
			startAlphaAnimIn(CalendarCell.this);
		}
		if (event.getAction() == MotionEvent.ACTION_CANCEL) {
			bHandled = true;
			bTouchedDown = false;
			invalidate();
		}
		if (event.getAction() == MotionEvent.ACTION_UP) {
			bHandled = true;
			bTouchedDown = false;
			invalidate();
			doItemClick();
		}
		return bHandled;
	}

	/**
	 * 描述:TODO.
	 *
	 * @param keyCode the key code
	 * @param event the event
	 * @return true, if successful
	 * @see android.view.View#onKeyDown(int, android.view.KeyEvent)
	 * @author: zhaoqp
	 * @date:2013-7-19 下午4:31:18
	 * @version v1.0
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean bResult = super.onKeyDown(keyCode, event);
		if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
				|| (keyCode == KeyEvent.KEYCODE_ENTER)) {
			doItemClick();
		}
		return bResult;
	}

	/**
	 * 描述:动画不透明度渐变.
	 *
	 * @param view the view
	 */
	public static void startAlphaAnimIn(View view) {
		AlphaAnimation anim = new AlphaAnimation(0.5F, 1);
		anim.setDuration(ANIM_ALPHA_DURATION);
		anim.startNow();
		view.startAnimation(anim);
	}

	/**
	 * 描述:有记录时的样子.
	 *
	 * @param canvas the canvas
	 * @param Color the color
	 */
	public void createReminder(Canvas canvas, int Color) {
		
		pt.setUnderlineText(true);
		pt.setStyle(Paint.Style.FILL_AND_STROKE);
		pt.setColor(Color);
		Path path = new Path();
		path.moveTo(rect.right - rect.width() / 4, rect.top);
		path.lineTo(rect.right, rect.top);
		path.lineTo(rect.right, rect.top + rect.width() / 4);
		path.lineTo(rect.right - rect.width() / 4, rect.top);
		path.close();
		canvas.drawPath(path, pt);
		pt.setUnderlineText(true);
		
	}
	
	/**
	 * 描述:是否为活动的月.
	 *
	 * @return true, if is active month
	 */
	public boolean isActiveMonth() {
		return isActiveMonth;
	}
	
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值