/*
* Copyright (C) 2011 Chris Gao <chris@exina.net>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.exina.android.calendar;
import java.util.Calendar;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.MonthDisplayHelper;
import android.view.MotionEvent;
import android.widget.ImageView;
public class CalendarView extends ImageView {
private static int WEEK_TOP_MARGIN = 74;
private static int WEEK_LEFT_MARGIN = 40;
private static int CELL_WIDTH = 58;
private static int CELL_HEIGH = 53;
private static int CELL_MARGIN_TOP = 0;
private static int CELL_MARGIN_LEFT = 39;
private static float CELL_TEXT_SIZE;
public static final int CURRENT_MOUNT = 0;
public static final int NEXT_MOUNT = 1;
public static final int PREVIOUS_MOUNT = -1;
private static final String TAG = "CalendarView";
private Calendar mRightNow = null;
private Drawable mWeekTitle = null;
private Cell mToday = null;
private Cell[][] mCells = new Cell[6][7];
private OnCellTouchListener mOnCellTouchListener = null;
MonthDisplayHelper mHelper;
Drawable mDecoration = null;
Drawable mDecoraClick = null;
private Context context;
public interface OnCellTouchListener {
public void onTouch(Cell cell);
}
public CalendarView(Context context) {
this(context, null);
}
public CalendarView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CalendarView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
mDecoration = context.getResources().getDrawable(R.drawable.typeb_calendar_today);
mDecoraClick = context.getResources().getDrawable(R.drawable.typeb_calendar_today);
initCalendarView();
}
private void initCalendarView() {
mRightNow = Calendar.getInstance();
// prepare static vars
Resources res = getResources();
WEEK_TOP_MARGIN = (int) res.getDimension(R.dimen.week_top_margin);
WEEK_LEFT_MARGIN = (int) res.getDimension(R.dimen.week_left_margin);
CELL_WIDTH = (int) res.getDimension(R.dimen.cell_width);
CELL_HEIGH = (int) res.getDimension(R.dimen.cell_heigh);
CELL_MARGIN_TOP = (int) res.getDimension(R.dimen.cell_margin_top);
CELL_MARGIN_LEFT = (int) res.getDimension(R.dimen.cell_margin_left);
CELL_TEXT_SIZE = res.getDimension(R.dimen.cell_text_size);
// set background
setImageResource(R.drawable.background);
mWeekTitle = res.getDrawable(R.drawable.calendar_week);
mHelper =
new MonthDisplayHelper(
mRightNow.get(Calendar.YEAR),
mRightNow.get(Calendar.MONTH),
mRightNow.getFirstDayOfWeek()
);
}
private void initCells() {
class _calendar {
public int day;
public int whichMonth; // -1 为上月 1为下月 0为此月
public _calendar(int d, int b) {
day = d;
whichMonth = b;
}
public _calendar(int d) { // 上个月 默认为
this(d, PREVIOUS_MOUNT);
}
};
_calendar tmp[][] = new _calendar[6][7];
for(int i=0; i<tmp.length; i++) {
int n[] = mHelper.getDigitsForRow(i);
for(int d=0; d<n.length; d++) {
if(mHelper.isWithinCurrentMonth(i,d))
tmp[i][d] = new _calendar(n[d], CURRENT_MOUNT);
else if(i == 0) {
tmp[i][d] = new _calendar(n[d]);
} else {
tmp[i][d] = new _calendar(n[d], NEXT_MOUNT);
}
}
}
Calendar today = Calendar.getInstance();
int thisDay = 0;
mToday = null;
if(mHelper.getYear()==today.get(Calendar.YEAR) && mHelper.getMonth()==today.get(Calendar.MONTH)) {
thisDay = today.get(Calendar.DAY_OF_MONTH);
}
// build cells
Rect Bound = new Rect(CELL_MARGIN_LEFT, CELL_MARGIN_TOP, CELL_WIDTH+CELL_MARGIN_LEFT, CELL_HEIGH+CELL_MARGIN_TOP);
for(int week=0; week<mCells.length; week++) {
for(int day=0; day<mCells[week].length; day++) {
if(tmp[week][day].whichMonth == CURRENT_MOUNT) { // 此月 开始设置cell
if(day==0 || day==6 )
mCells[week][day] = new RedCell(tmp[week][day].day, new Rect(Bound), CELL_TEXT_SIZE);
else
mCells[week][day] = new Cell(tmp[week][day].day, new Rect(Bound), CELL_TEXT_SIZE);
} else if(tmp[week][day].whichMonth == PREVIOUS_MOUNT) { // 上月为gray
mCells[week][day] = new GrayCell(tmp[week][day].day, new Rect(Bound), CELL_TEXT_SIZE);
} else { // 下月为LTGray
mCells[week][day] = new LTGrayCell(tmp[week][day].day, new Rect(Bound), CELL_TEXT_SIZE);
}
Bound.offset(CELL_WIDTH, 0); // move to next column
// get today
if(tmp[week][day].day==thisDay && tmp[week][day].whichMonth == 0) {
mToday = mCells[week][day];
mDecoration.setBounds(mToday.getBound());
}
}
Bound.offset(0, CELL_HEIGH); // move to next row and first column
Bound.left = CELL_MARGIN_LEFT;
Bound.right = CELL_MARGIN_LEFT+CELL_WIDTH;
}
}
@Override
public void onLayout(boolean changed, int left, int top, int right, int bottom) {
android.util.Log.d(TAG, "left="+left);
Rect re = getDrawable().getBounds();
WEEK_LEFT_MARGIN = CELL_MARGIN_LEFT = (right-left - re.width()) / 2;
mWeekTitle.setBounds(WEEK_LEFT_MARGIN, WEEK_TOP_MARGIN, WEEK_LEFT_MARGIN+mWeekTitle.getMinimumWidth(), WEEK_TOP_MARGIN+mWeekTitle.getMinimumHeight());
initCells();
super.onLayout(changed, left, top, right, bottom);
}
public void setTimeInMillis(long milliseconds) {
mRightNow.setTimeInMillis(milliseconds);
initCells();
this.invalidate();
}
public int getYear() {
return mHelper.getYear();
}
public int getMonth() {
return mHelper.getMonth();
}
public void nextMonth() {
mHelper.nextMonth();
initCells();
invalidate();
}
public void previousMonth() {
mHelper.previousMonth();
initCells();
invalidate();
}
public boolean firstDay(int day) {
return day==1;
}
public boolean lastDay(int day) {
return mHelper.getNumberOfDaysInMonth()==day;
}
public void goToday() {
Calendar cal = Calendar.getInstance();
mHelper = new MonthDisplayHelper(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH));
initCells();
invalidate();
}
public Calendar getDate() {
return mRightNow;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(mOnCellTouchListener!=null){
for(Cell[] week : mCells) {
for(Cell day : week) {
if(day.hitTest((int)event.getX(), (int)event.getY())) {
mOnCellTouchListener.onTouch(day);
}
}
}
}
return super.onTouchEvent(event);
}
public void setOnCellTouchListener(OnCellTouchListener p) {
mOnCellTouchListener = p;
}
@Override
protected void onDraw(Canvas canvas) {
// draw background
super.onDraw(canvas);
mWeekTitle.draw(canvas);
// draw cells
for(Cell[] week : mCells) {
for(Cell day : week) {
day.draw(canvas);
}
}
// draw today
if
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
android 日历的 demo 项目 (114个子文件)
all-wcprops 606B
all-wcprops 412B
all-wcprops 408B
all-wcprops 202B
all-wcprops 182B
all-wcprops 180B
all-wcprops 180B
all-wcprops 180B
all-wcprops 180B
all-wcprops 180B
all-wcprops 172B
all-wcprops 166B
all-wcprops 160B
all-wcprops 67B
all-wcprops 67B
resources.ap_ 69KB
CalendarV.0.4.2.apk 79KB
AndroidManifest.xml.bak 2KB
proguard.cfg 1KB
CalendarView.class 8KB
CalendarActivity.class 4KB
TestActivity.class 3KB
Cell.class 2KB
CalendarActivity$3.class 2KB
CalendarActivity$1.class 2KB
CalendarActivity$2.class 2KB
CalendarView$1_calendar.class 840B
CalendarView$LTGrayCell.class 828B
CalendarView$GrayCell.class 822B
CalendarView$RedCell.class 819B
R.class 695B
R$dimen.class 651B
R$string.class 644B
R$drawable.class 574B
R$id.class 532B
R$layout.class 418B
R$xml.class 409B
R$attr.class 364B
CalendarView$OnCellTouchListener.class 307B
.classpath 364B
classes.dex 17KB
.DS_Store 6KB
entries 961B
entries 837B
entries 788B
entries 462B
entries 439B
entries 434B
entries 423B
entries 423B
entries 413B
entries 413B
entries 413B
entries 411B
entries 408B
entries 403B
entries 244B
CalendarView.java 9KB
CalendarActivity.java 4KB
TestActivity.java 3KB
R.java 2KB
Cell.java 2KB
background.png 9KB
background.png 9KB
calendar_week.png 9KB
calendar_week.png 9KB
typeb_calendar_today.png 6KB
icon.png 4KB
ic_launcher.png 4KB
icon.png 4KB
icon.png 3KB
ic_launcher.png 3KB
icon.png 2KB
ic_launcher.png 2KB
org.eclipse.ltk.core.refactoring.prefs 134B
.project 818B
project.properties 359B
background.png.svn-base 9KB
background.png.svn-base 9KB
calendar_week.png.svn-base 9KB
calendar_week.png.svn-base 9KB
CalendarView.java.svn-base 7KB
typeb_calendar_today.png.svn-base 6KB
icon.png.svn-base 4KB
icon.png.svn-base 4KB
icon.png.svn-base 3KB
TestActivity.java.svn-base 3KB
CalendarActivity.java.svn-base 2KB
Cell.java.svn-base 2KB
icon.png.svn-base 2KB
main.xml.svn-base 693B
test.xml.svn-base 562B
strings.xml.svn-base 560B
dimens.xml.svn-base 368B
dimens.xml.svn-base 362B
Dimens.xml.svn-base 358B
dimens.xml.svn-base 356B
icon.png.svn-base 53B
typeb_calendar_today.png.svn-base 53B
icon.png.svn-base 53B
共 114 条
- 1
- 2
xx291579699
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页