1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
| import java.lang.reflect.Array;
import java.util.Calendar;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
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 = 50;
private static int CELL_HEIGH = 48;
private static int CELL_MARGIN_TOP = 92;
private static int CELL_MARGIN_LEFT = 39;
private static float CELL_TEXT_SIZE;
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;
int mPeriod ;
int mCycle ;
int mYear;
int mMonth;
int mDay;
int clBlood;
int clBlue;
int clFertile;
int clGreen;
int clLtBlue;
int clLtGreen;
int clLtOrange;
int clLtRed;
int clMagenta;
int clNotFertile;
int clOrange;
int clRed;
boolean dayInThisMonth;
int pickedDayOfMonth;
Calendar givenDate;
Calendar pickedDate;
class _calendar {
public int day;
public boolean thisMonth;
public _calendar(int d, boolean b) {
day = d;
thisMonth = b;
}
public _calendar(CalendarView calendarView, int d) {
this(d, false);
}
};
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);
mDecoration = context.getResources().getDrawable(R.drawable.today);
initCalendarView();
this.mRightNow = null;
this.mWeekTitle = null;
this.mToday = null;
this.mCells = (Cell[][]) Array.newInstance(Cell.class, new int[]{6, 7});
this.mOnCellTouchListener = null;
this.mDecoration = null;
this.mPeriod = 29;
this.mCycle = 5;
this.mYear = -1;
this.mMonth = -1;
this.mDay = -1;
this.clBlood = 0xffff0000;
this.clFertile = Color.rgb(0, 155, 0); //vert foncé//
this.clNotFertile = Color.rgb(220, 220,220); //gris//
this.clLtRed = Color.rgb(255, 100, 100); //rose//
this.clRed = Color.rgb(255, 55, 55); //rouge//
this.clLtGreen = Color.rgb(100, 255, 100); //vert//
this.clGreen = Color.rgb(55, 255, 55); //vert//
this.clLtBlue = -65281;
this.clBlue = Color.rgb(55, 55, 255); //bleu//
this.clMagenta = Color.rgb(255, 55, 255); //violet//
this.pickedDate = Calendar.getInstance();
this.givenDate = Calendar.getInstance();
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));
}
private void initCells() {
_calendar tmp[][] = (_calendar[][]) Array.newInstance (_calendar.class , new int[]{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], true);
} else {
tmp[i][d] = new _calendar(this, n[d]);
}
}
}
if (this.dayInThisMonth) {
this.pickedDate.set(this.mHelper.getYear(), this.mHelper.getMonth(), this.pickedDayOfMonth);
} else {
int i2;
int i3 = this.mYear >= 0 ? 1 : 0;
if (this.mMonth >= 0) {
i2 = 1;
} else {
i2 = 0;
}
i2 &= i3;
if (this.mDay >= 0) {
i3 = 1;
} else {
i3 = 0;
}
if ((i3 & i2) != 0) {
this.pickedDate.set(this.mYear, this.mMonth, this.mDay);
}
}
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);
int week = 0;
while (week < this.mCells.length) {
int day = 0;
while (day < this.mCells[week].length) {
if (tmp[week][day].thisMonth) {
if(day==0) {
mCells[week][day] = new WhiteCell(tmp[week][day].day, new Rect(Bound), CELL_TEXT_SIZE, 0xffff0000,0xff888888,0);
}
else {
mCells[week][day] = new WhiteCell(tmp[week][day].day, new Rect(Bound), CELL_TEXT_SIZE, 0xff000000,0xff888888,0);
}
} else {
mCells[week][day] = new GrayCell(tmp[week][day].day, new Rect(Bound), CELL_TEXT_SIZE, 0xcecece, 0xffffffff);
}
Bound.offset(CELL_WIDTH, 0); // move to next column
// get today
if(tmp[week][day].day==thisDay && tmp[week][day].thisMonth) {
mToday = mCells[week][day];
mDecoration.setBounds(mToday.getBound());
}
day++;
}
Bound.offset(0, CELL_HEIGH); // move to next row and first column
Bound.left = CELL_MARGIN_LEFT;
Bound.right = CELL_MARGIN_LEFT+CELL_WIDTH;
week++;
}
}
@Override
public void onLayout(boolean changed, int left, int top, int right, int bottom) {
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(mDecoration!=null && mToday!=null) {
mDecoration.draw(canvas);
}
}
public class GrayCell extends Cell {
public GrayCell(int dayOfMon, Rect rect, float s, int txtCol, int bkgCol) {
super(dayOfMon, rect, s,bkgCol,txtCol);
this.mPaint.setStyle(Paint.Style.FILL);
CalendarView.this.invalidate();
}
}
private class WhiteCell extends Cell {
public WhiteCell(int dayOfMon, Rect rect, float s, int txtCol, int bkgCol, int stroke) {
super(dayOfMon, rect, s,bkgCol,txtCol);
this.mStroke = stroke;
this.mPaint.setStyle(Paint.Style.FILL);
}
}
public long DateDifference(Calendar selectedDay, Calendar givenDay) {
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.set(2016, 12, 30);
cal2.set(2017, 5, 3);
long diff = givenDay.getTimeInMillis() - selectedDay.getTimeInMillis();
if (diff < 0) {
diff -= 3600000;
}
long diffSeconds = diff / 1000;
long diffMinutes = diff / 60000;
long diffHours = diff / 3600000;
long diffDays = diff / 86400000;
System.out.println("In milliseconds: " + diff + " milliseconds.");
System.out.println("In seconds: " + diffSeconds + " seconds.");
System.out.println("In minutes: " + diffMinutes + " minutes.");
System.out.println("In hours: " + diffHours + " hours.");
System.out.println("In days: " + diffDays + " days.");
return diffDays;
}
public void setParams(int period, int bleeding, int year, int month, int day) {
this.mPeriod = period;
this.mCycle = bleeding;
this.mYear = year;
this.mMonth = month;
this.mDay = day;
}
} |
Partager