1 先來張圖
2 原理
2.1系統原理
實作了會議室的預定功能,且通過月曆時間軸等等控件來控制會議室的使用。
2.2資料庫
使用了郭霖的LitePal,具體請移步http://blog.csdn.net/guolin_blog。
2.3TimeLine控件源碼
package com.example.shijianzhou;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import android.content.Context;
import android.os.Handler;
import android.os.SystemClock;
import android.text.format.Time;
import android.util.AttributeSet;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
/**
* 用來實作會議預定的時間軸。
*
* @version 0.1
* @author zhou
*
*/
public class TimeLine extends LinearLayout {
private Runnable mTicker;
private Handler mHandler;
/**
* 控制定時線程是否運作的flag。
*/
private boolean mTickerStopped;
private Calendar mCalendar;
private String mFormat = "hh:mm:ss";
private ImageView pasttime;
private int Width;
private int Height;
private LinearLayout mainline;
private ArrayList<HashMap<String, Integer>> huiyitimelist;
public TimeLine(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
LayoutInflater.from(context).inflate(R.layout.timeline, this, true);
pasttime = (ImageView) findViewById(R.id.pasttime);
mainline = (LinearLayout) findViewById(R.id.mainline);
initTimeLine(context);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
setmTickerstart();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mTickerStopped = true;
}
private void initTimeLine(Context context) {
if (mCalendar == null) {
mCalendar = Calendar.getInstance();
}
huiyitimelist = new ArrayList<HashMap<String, Integer>>();
// 取得螢幕的寬度和高度
WindowManager windowManager = (WindowManager) getContext()
.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Width = display.getWidth();
Height = display.getHeight();
}
/**
*
* 移動遊标到指定位置。
*
* @param position
* 位置參數。
*/
public void movePoint(int position) {
android.view.ViewGroup.LayoutParams params = pasttime.getLayoutParams();
params.width = position;
pasttime.setLayoutParams(params);
}
/**
*
* 使時間軸上指定的一段變紅,即該段時間會議室已經被預定。
*
* @param start
* 起始時間(秒)
* @param end
* 結束時間(秒)
*
* @return -1 失敗 1 成功。
*/
public int setSegmentRed(int start, int end) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("starttime", start);
map.put("endtime", end);
start = start * Width / (24 * 60 * 60);
end = end * Width / (24 * 60 * 60);
map.put("start", start);
map.put("end", end);
if (end <= start) {
return -1;// 代表會議時間錯誤。
}
for (int i = 0; i < huiyitimelist.size(); i++) {
int startlocal = huiyitimelist.get(i).get("start");
int endlocal = huiyitimelist.get(i).get("end");
if ((start > startlocal & start < endlocal)
| (end > startlocal & end < endlocal)) {
return -2;// 代表會議時間沖突。
}
}
huiyitimelist.add(map);
mainline.removeAllViews();
for (int i = 0; i < huiyitimelist.size(); i++) {
int startlocal = huiyitimelist.get(i).get("start");
int endlocal = huiyitimelist.get(i).get("end");
LinearLayout layout_sub_Lin = new LinearLayout(getContext());
LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
LP_WW.width = (endlocal - startlocal);
if (i == 0) {
LP_WW.leftMargin = startlocal;
} else {
LP_WW.leftMargin = startlocal
- huiyitimelist.get(i - 1).get("end");
}
layout_sub_Lin.setLayoutParams(LP_WW);
layout_sub_Lin.setBackgroundColor(0x88ffff00);
mainline.addView(layout_sub_Lin);
}
return 1;
}
/**
* 清除所有時間軸上的會議占用時間标記。
*/
public void clearAllsegment() {
mainline.removeAllViews();
huiyitimelist.clear();
}
/**
* 控制定時器是否繼續工作。
*
* @param mTickerStopped
* true則時鐘停止。
*/
public void setmTickerStopped() {
mTickerStopped = true;
movePoint(0);
}
/**
* 時間軸動起來。
*/
public void setmTickerstart() {
// TODO Auto-generated method stub
mTickerStopped = false;
mHandler = new Handler();
mTicker = new Runnable() {
public void run() {
if (mTickerStopped) {
return;
} else {
mCalendar.setTimeInMillis(System.currentTimeMillis());
Time t = new Time(); // or Time t=new Time("GMT+8"); 加上Time
// Zone資料。
t.setToNow(); // 取得系統時間。
int hour = t.hour; // 0-23
int minute = t.minute;
int second = t.second;
int allseconds = hour * 60 * 60 + minute * 60 + second;
int point = allseconds * Width / (24 * 60 * 60);
movePoint(point);
invalidate();
// 周遊是否有會議開始或者結束。
// Log.v("znz", "size --> " + huiyitimelist.size());
// Log.v("znz",
// "starttime --> "
// + huiyitimelist.get(0).get("starttime"));
// Log.v("znz", "allseconds ---> " + allseconds);
// Log.v("znz",
// "endtime --> " + huiyitimelist.get(0).get("endtime"));
for (int i = 0; i < huiyitimelist.size(); i++) {
if (allseconds >= huiyitimelist.get(i).get("starttime")
& allseconds <= huiyitimelist.get(i).get(
"endtime")) {
if (timelinecallback != null) {
timelinecallback.huiyiStarted(huiyitimelist
.get(i).get("starttime"), huiyitimelist
.get(i).get("endtime"));
break;
}
}
if (i == (huiyitimelist.size() - 1)) {
if (timelinecallback != null) {
timelinecallback.huiyiStoped();
}
}
}
long now = SystemClock.uptimeMillis();
long next = now
+ (1000 - System.currentTimeMillis() % 1000);
mHandler.postAtTime(mTicker, next);
}
}
};
mTicker.run();
}
private TimelineCallback timelinecallback;
/**
* @param timelinecallback
* the timelinecallback to set
*/
public void setTimelinecallback(TimelineCallback timelinecallback) {
this.timelinecallback = timelinecallback;
}
}
2.4項目下載下傳
http://download.csdn.net/detail/zhounanzhaode/8902951