`
雨花台舞水
  • 浏览: 11414 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

android日历实现

 
阅读更多

android日历组件,代码见附件.

效果图:

 

 

代码:

 

package com.test.calendar;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

import com.test.calendar.DateWidgetDayCell;
import com.test.calendar.DateWidgetDayHeader;
import com.test.calendar.SymbolButton;
import com.test.calendar.DayStyle;

public class DateWidget extends Activity {
    private ArrayList<DateWidgetDayCell> days = new ArrayList<DateWidgetDayCell>();
    // private SimpleDateFormat dateMonth = new SimpleDateFormat("MMMM yyyy");
    private Calendar calStartDate = Calendar.getInstance();
    private Calendar calToday = Calendar.getInstance();
    private Calendar calCalendar = Calendar.getInstance();
    private Calendar calSelected = Calendar.getInstance();
    LinearLayout layContent = null;
    Button btnPrev = null;
    Button btnToday = null;
    Button btnNext = null;
    private int iFirstDayOfWeek = Calendar.MONDAY;
    private int iMonthViewCurrentMonth = 0;
    private int iMonthViewCurrentYear = 0;
    public static final int SELECT_DATE_REQUEST = 111;
    private static final int iDayCellSize = 38;
    private static final int iDayHeaderHeight = 24;
    private static final int iTotalWidth = (iDayCellSize * 7);
    private TextView tv;
    private int mYear;
    private int mMonth;
    private int mDay;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        iFirstDayOfWeek = Calendar.MONDAY;
        mYear = calSelected.get(Calendar.YEAR);
        mMonth = calSelected.get(Calendar.MONTH);
        mDay = calSelected.get(Calendar.DAY_OF_MONTH);
        setContentView(generateContentView());
        calStartDate = getCalendarStartDate();
        DateWidgetDayCell daySelected = updateCalendar();
        updateControlsState();
        if (daySelected != null)
            daySelected.requestFocus();
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    private LinearLayout createLayout(int iOrientation) {
        LinearLayout lay = new LinearLayout(this);
        lay.setLayoutParams(new LayoutParams(
                android.view.ViewGroup.LayoutParams.FILL_PARENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
        lay.setOrientation(iOrientation);
        return lay;
    }

    private Button createButton(String sText, int iWidth, int iHeight) {
        Button btn = new Button(this);
        btn.setText(sText);
        btn.setLayoutParams(new LayoutParams(iWidth, iHeight));
        return btn;
    }

    private void generateTopButtons(LinearLayout layTopControls) {
        final int iHorPadding = 24;
        final int iSmallButtonWidth = 60;
        btnToday = createButton("", iTotalWidth - iSmallButtonWidth
                - iSmallButtonWidth,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        btnToday.setPadding(iHorPadding, btnToday.getPaddingTop(), iHorPadding,
                btnToday.getPaddingBottom());
        btnToday.setBackgroundResource(android.R.drawable.btn_default_small);

        SymbolButton btnPrev = new SymbolButton(this,
                SymbolButton.symbol.arrowLeft);
        btnPrev.setLayoutParams(new LayoutParams(iSmallButtonWidth,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
        btnPrev.setBackgroundResource(android.R.drawable.btn_default_small);

        SymbolButton btnNext = new SymbolButton(this,
                SymbolButton.symbol.arrowRight);
        btnNext.setLayoutParams(new LayoutParams(iSmallButtonWidth,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
        btnNext.setBackgroundResource(android.R.drawable.btn_default_small);

        // set events
        btnPrev.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View arg0) {
                setPrevViewItem();
            }
        });
        btnToday.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View arg0) {

                setTodayViewItem();
                String s = calToday.get(Calendar.YEAR) + "/"
                        + (calToday.get(Calendar.MONTH) + 1);
                btnToday.setText(s);
            }
        });
        btnNext.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View arg0) {
                setNextViewItem();
            }
        });

        layTopControls.setGravity(Gravity.CENTER_HORIZONTAL);
        layTopControls.addView(btnPrev);
        layTopControls.addView(btnToday);
        layTopControls.addView(btnNext);
    }

    private View generateContentView() {
        LinearLayout layMain = createLayout(LinearLayout.VERTICAL);
        layMain.setPadding(8, 8, 8, 8);
        LinearLayout layTopControls = createLayout(LinearLayout.HORIZONTAL);

        layContent = createLayout(LinearLayout.VERTICAL);
        layContent.setPadding(20, 0, 20, 0);
        generateTopButtons(layTopControls);
        generateCalendar(layContent);
        layMain.addView(layTopControls);
        layMain.addView(layContent);

        tv = new TextView(this);
        layMain.addView(tv);
        return layMain;
    }

    private View generateCalendarRow() {
        LinearLayout layRow = createLayout(LinearLayout.HORIZONTAL);
        for (int iDay = 0; iDay < 7; iDay++) {
            DateWidgetDayCell dayCell = new DateWidgetDayCell(this,
                    iDayCellSize, iDayCellSize);
            dayCell.setItemClick(mOnDayCellClick);
            days.add(dayCell);
            layRow.addView(dayCell);
        }
        return layRow;
    }

    private View generateCalendarHeader() {
        LinearLayout layRow = createLayout(LinearLayout.HORIZONTAL);
        for (int iDay = 0; iDay < 7; iDay++) {
            DateWidgetDayHeader day = new DateWidgetDayHeader(this,
                    iDayCellSize, iDayHeaderHeight);
            final int iWeekDay = DayStyle.getWeekDay(iDay, iFirstDayOfWeek);
            day.setData(iWeekDay);
            layRow.addView(day);
        }
        return layRow;
    }

    private void generateCalendar(LinearLayout layContent) {
        layContent.addView(generateCalendarHeader());
        days.clear();
        for (int iRow = 0; iRow < 6; iRow++) {
            layContent.addView(generateCalendarRow());
        }
    }

    private Calendar getCalendarStartDate() {
        calToday.setTimeInMillis(System.currentTimeMillis());
        calToday.setFirstDayOfWeek(iFirstDayOfWeek);

        if (calSelected.getTimeInMillis() == 0) {
            calStartDate.setTimeInMillis(System.currentTimeMillis());
            calStartDate.setFirstDayOfWeek(iFirstDayOfWeek);
        } else {
            calStartDate.setTimeInMillis(calSelected.getTimeInMillis());
            calStartDate.setFirstDayOfWeek(iFirstDayOfWeek);
        }

        UpdateStartDateForMonth();

        return calStartDate;
    }

    private DateWidgetDayCell updateCalendar() {
        DateWidgetDayCell daySelected = null;
        boolean bSelected = false;
        final boolean bIsSelection = (calSelected.getTimeInMillis() != 0);
        final int iSelectedYear = calSelected.get(Calendar.YEAR);
        final int iSelectedMonth = calSelected.get(Calendar.MONTH);
        final int iSelectedDay = calSelected.get(Calendar.DAY_OF_MONTH);
        calCalendar.setTimeInMillis(calStartDate.getTimeInMillis());
        for (int i = 0; i < days.size(); i++) {
            final int iYear = calCalendar.get(Calendar.YEAR);
            final int iMonth = calCalendar.get(Calendar.MONTH);
            final int iDay = calCalendar.get(Calendar.DAY_OF_MONTH);
            final int iDayOfWeek = calCalendar.get(Calendar.DAY_OF_WEEK);
            DateWidgetDayCell dayCell = days.get(i);
            // check today
            boolean bToday = false;
            if (calToday.get(Calendar.YEAR) == iYear)
                if (calToday.get(Calendar.MONTH) == iMonth)
                    if (calToday.get(Calendar.DAY_OF_MONTH) == iDay)
                        bToday = true;
            // check holiday
            boolean bHoliday = false;
            if ((iDayOfWeek == Calendar.SATURDAY)
                    || (iDayOfWeek == Calendar.SUNDAY))
                bHoliday = true;
            if ((iMonth == Calendar.JANUARY) && (iDay == 1))
                bHoliday = true;

            dayCell.setData(iYear, iMonth, iDay, bToday, bHoliday,
                    iMonthViewCurrentMonth);
            bSelected = false;
            if (bIsSelection)
                if ((iSelectedDay == iDay) && (iSelectedMonth == iMonth)
                        && (iSelectedYear == iYear)) {
                    bSelected = true;
                }
            dayCell.setSelected(bSelected);
            if (bSelected)
                daySelected = dayCell;
            calCalendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        layContent.invalidate();
        return daySelected;
    }

    private void UpdateStartDateForMonth() {
        iMonthViewCurrentMonth = calStartDate.get(Calendar.MONTH);
        iMonthViewCurrentYear = calStartDate.get(Calendar.YEAR);
        calStartDate.set(Calendar.DAY_OF_MONTH, 1);
        UpdateCurrentMonthDisplay();
        // update days for week
        int iDay = 0;
        int iStartDay = iFirstDayOfWeek;
        if (iStartDay == Calendar.MONDAY) {
            iDay = calStartDate.get(Calendar.DAY_OF_WEEK) - Calendar.MONDAY;
            if (iDay < 0)
                iDay = 6;
        }
        if (iStartDay == Calendar.SUNDAY) {
            iDay = calStartDate.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY;
            if (iDay < 0)
                iDay = 6;
        }
        calStartDate.add(Calendar.DAY_OF_WEEK, -iDay);
    }

    private void UpdateCurrentMonthDisplay() {
        String s = calCalendar.get(Calendar.YEAR) + "/"
                + (calCalendar.get(Calendar.MONTH) + 1);// dateMonth.format(calCalendar.getTime());
        btnToday.setText(s);
        mYear = calCalendar.get(Calendar.YEAR);
    }

    private void setPrevViewItem() {
        iMonthViewCurrentMonth--;
        if (iMonthViewCurrentMonth == -1) {
            iMonthViewCurrentMonth = 11;
            iMonthViewCurrentYear--;
        }
        calStartDate.set(Calendar.DAY_OF_MONTH, 1);
        calStartDate.set(Calendar.MONTH, iMonthViewCurrentMonth);
        calStartDate.set(Calendar.YEAR, iMonthViewCurrentYear);
        UpdateStartDateForMonth();
        updateCalendar();

    }

    private void setTodayViewItem() {
        calToday.setTimeInMillis(System.currentTimeMillis());
        calToday.setFirstDayOfWeek(iFirstDayOfWeek);
        calStartDate.setTimeInMillis(calToday.getTimeInMillis());
        calStartDate.setFirstDayOfWeek(iFirstDayOfWeek);
        UpdateStartDateForMonth();
        updateCalendar();
    }

    private void setNextViewItem() {
        iMonthViewCurrentMonth++;
        if (iMonthViewCurrentMonth == 12) {
            iMonthViewCurrentMonth = 0;
            iMonthViewCurrentYear++;
        }
        calStartDate.set(Calendar.DAY_OF_MONTH, 1);
        calStartDate.set(Calendar.MONTH, iMonthViewCurrentMonth);
        calStartDate.set(Calendar.YEAR, iMonthViewCurrentYear);
        UpdateStartDateForMonth();
        updateCalendar();

    }

    private DateWidgetDayCell.OnItemClick mOnDayCellClick = new DateWidgetDayCell.OnItemClick() {
        public void OnClick(DateWidgetDayCell item) {
            calSelected.setTimeInMillis(item.getDate().getTimeInMillis());
            item.setSelected(true);
            updateCalendar();
            updateControlsState();
        }
    };

    private void updateControlsState() {
        SimpleDateFormat dateFull = new SimpleDateFormat("d MMMM yyyy");
        mYear = calSelected.get(Calendar.YEAR);
        mMonth = calSelected.get(Calendar.MONTH);
        mDay = calSelected.get(Calendar.DAY_OF_MONTH);
        tv.setText(new StringBuilder().append(mYear).append("/").append(
                format(mMonth + 1)).append("/").append(format(mDay)).append(
                "-----").append(dateFull.format(calSelected.getTime())));
    }

    private String format(int x) {
        String s = "" + x;
        if (s.length() == 1)
            s = "0" + s;
        return s;
    }

}

 

-----------------------------------------------------------------

package com.test.calendar;

import java.util.Calendar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.LinearLayout.LayoutParams;

public class DateWidgetDayCell extends View {
    // types
    public interface OnItemClick {
        public void OnClick(DateWidgetDayCell item);
    }

    public static int ANIM_ALPHA_DURATION = 100;
    // fields
    private final static float fTextSize = 22;
    private final static int iMargin = 1;
    private final static int iAlphaInactiveMonth = 0x88;

    // fields
    private int iDateYear = 0;
    private int iDateMonth = 0;
    private int iDateDay = 0;

    // fields
    private OnItemClick itemClick = null;
    private Paint pt = new Paint();
    private RectF rect = new RectF();
    private String sDate = "";

    // fields
    private boolean bSelected = false;
    private boolean bIsActiveMonth = false;
    private boolean bToday = false;
    private boolean bHoliday = false;
    private boolean bTouchedDown = false;

    // methods
    public DateWidgetDayCell(Context context, int iWidth, int iHeight) {
        super(context);
        setFocusable(true);
        setLayoutParams(new LayoutParams(iWidth, iHeight));
    }

    public boolean getSelected() {
        return this.bSelected;
    }

    @Override
    public void setSelected(boolean bEnable) {
        if (this.bSelected != bEnable) {
            this.bSelected = bEnable;
            this.invalidate();
        }
    }

    public void setData(int iYear, int iMonth, int iDay, boolean bToday,
            boolean bHoliday, int iActiveMonth) {
        iDateYear = iYear;
        iDateMonth = iMonth;
        iDateDay = iDay;

        this.sDate = Integer.toString(iDateDay);
        this.bIsActiveMonth = (iDateMonth == iActiveMonth);
        this.bToday = bToday;
        this.bHoliday = bHoliday;
    }

    public void setItemClick(OnItemClick itemClick) {
        this.itemClick = itemClick;
    }

    private int getTextHeight() {
        return (int) (-pt.ascent() + pt.descent());
    }

    @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;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        boolean bResult = super.onKeyUp(keyCode, event);
        return bResult;
    }

    public void doItemClick() {
        if (itemClick != null)
            itemClick.OnClick(this);
    }

    @Override
    protected void onFocusChanged(boolean gainFocus, int direction,
            Rect previouslyFocusedRect) {
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
        invalidate();
    }

    public Calendar getDate() {
        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;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // init rectangles
        rect.set(0, 0, this.getWidth(), this.getHeight());
        rect.inset(1, 1);

        // drawing
        final boolean bFocused = IsViewFocused();

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

    private void drawDayView(Canvas canvas, boolean bFocused) {
        if (bSelected || bFocused) {
            LinearGradient lGradBkg = null;

            if (bFocused) {
                lGradBkg = new LinearGradient(rect.left, 0, rect.right, 0,
                        DayStyle.iColorBkgFocusDark,
                        DayStyle.iColorBkgFocusLight, Shader.TileMode.CLAMP);
            }

            if (bSelected) {
                lGradBkg = new LinearGradient(rect.left, 0, rect.right, 0,
                        DayStyle.iColorBkgSelectedDark,
                        DayStyle.iColorBkgSelectedLight, Shader.TileMode.CLAMP);
            }

            if (lGradBkg != null) {
                pt.setShader(lGradBkg);
                canvas.drawRect(rect, pt);
            }

            pt.setShader(null);

        } else {

            pt.setColor(DayStyle.getColorBkg(bHoliday, bToday));
            if (!bIsActiveMonth)
                pt.setAlpha(iAlphaInactiveMonth);
            canvas.drawRect(rect, pt);
        }
    }

    public void drawDayNumber(Canvas canvas, boolean bFocused) {
        // draw day number
        pt.setTypeface(null);
        pt.setAntiAlias(true);
        pt.setShader(null);
        pt.setFakeBoldText(true);
        pt.setTextSize(fTextSize);

        pt.setUnderlineText(false);
        if (bToday)
            pt.setUnderlineText(true);

        int iTextPosX = (int) rect.right - (int) pt.measureText(sDate);
        int iTextPosY = (int) rect.bottom + (int) (-pt.ascent())
                - getTextHeight();

        iTextPosX -= ((int) rect.width() >> 1)
                - ((int) pt.measureText(sDate) >> 1);
        iTextPosY -= ((int) rect.height() >> 1) - (getTextHeight() >> 1);

        // draw text
        if (bSelected || bFocused) {
            if (bSelected)
                pt.setColor(DayStyle.iColorTextSelected);
            if (bFocused)
                pt.setColor(DayStyle.iColorTextFocused);
        } else {
            pt.setColor(DayStyle.getColorText(bHoliday, bToday));
        }

        if (!bIsActiveMonth)
            pt.setAlpha(iAlphaInactiveMonth);

        canvas.drawText(sDate, iTextPosX, iTextPosY + iMargin, pt);

        pt.setUnderlineText(false);
    }

    public boolean IsViewFocused() {
        return (this.isFocused() || bTouchedDown);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean bHandled = false;
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            bHandled = true;
            bTouchedDown = true;
            invalidate();
            startAlphaAnimIn(DateWidgetDayCell.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;
    }

    public static void startAlphaAnimIn(View view) {
        AlphaAnimation anim = new AlphaAnimation(0.5F, 1);
        anim.setDuration(ANIM_ALPHA_DURATION);
        anim.startNow();
        view.startAnimation(anim);
    }

}

 

 

-------------------------------------------

package com.test.calendar;

import java.util.*;
import android.content.*;
import android.view.*;
import android.widget.LinearLayout.LayoutParams;
import android.graphics.*;

public class DateWidgetDayHeader extends View {
    // fields
    private final static int iDayHeaderFontSize = 12;

    // fields
    private Paint pt = new Paint();
    private RectF rect = new RectF();
    private int iWeekDay = -1;
    private boolean bHoliday = false;

    // methods
    public DateWidgetDayHeader(Context context, int iWidth, int iHeight) {
        super(context);
        setLayoutParams(new LayoutParams(iWidth, iHeight));
    }

    public void setData(int iWeekDay) {
        this.iWeekDay = iWeekDay;
        this.bHoliday = false;
        if ((iWeekDay == Calendar.SATURDAY) || (iWeekDay == Calendar.SUNDAY))
            this.bHoliday = true;
    }

    private void drawDayHeader(Canvas canvas) {
        if (iWeekDay != -1) {
            // background
            pt.setColor(DayStyle.getColorFrameHeader(bHoliday));
            canvas.drawRect(rect, pt);

            // text
            pt.setTypeface(null);
            pt.setTextSize(iDayHeaderFontSize);
            pt.setAntiAlias(true);
            pt.setFakeBoldText(true);
            pt.setColor(DayStyle.getColorTextHeader(bHoliday));

            final int iTextPosY = getTextHeight();
            final String sDayName = DayStyle.getWeekDayName(iWeekDay);

            // draw day name
            final int iDayNamePosX = (int) rect.left
                    + ((int) rect.width() >> 1)
                    - ((int) pt.measureText(sDayName) >> 1);
            canvas.drawText(sDayName, iDayNamePosX, rect.top + iTextPosY + 2,
                    pt);
        }
    }

    private int getTextHeight() {
        return (int) (-pt.ascent() + pt.descent());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // init rectangles
        rect.set(0, 0, this.getWidth(), this.getHeight());
        rect.inset(1, 1);

        // drawing
        drawDayHeader(canvas);
    }

}
--------------------------------------------------------

package com.test.calendar;

import java.util.*;

public class DayStyle {
    // methods

    private static String[] getWeekDayNames() {
        String[] vec = new String[10];
//        vec[Calendar.SUNDAY] = "Sun";
//        vec[Calendar.MONDAY] = "Mon";
//        vec[Calendar.TUESDAY] = "Tue";
//        vec[Calendar.WEDNESDAY] = "Wed";
//        vec[Calendar.THURSDAY] = "Thu";
//        vec[Calendar.FRIDAY] = "Fri";
//        vec[Calendar.SATURDAY] = "Sat";

        vec[Calendar.SUNDAY] = "周日";
        vec[Calendar.MONDAY] = "周一";
        vec[Calendar.TUESDAY] = "周二";
        vec[Calendar.WEDNESDAY] = "周三";
        vec[Calendar.THURSDAY] = "周四";
        vec[Calendar.FRIDAY] = "周五";
        vec[Calendar.SATURDAY] ="周六";
        return vec;
    }

    public static String getWeekDayName(int iDay) {
        return vecStrWeekDayNames[iDay];
    }

    // fields
    private final static String[] vecStrWeekDayNames = getWeekDayNames();

    // fields
    public final static int iColorFrameHeader = 0xff666666;
    public final static int iColorFrameHeaderHoliday = 0xff707070;
    public final static int iColorTextHeader = 0xffcccccc;
    public final static int iColorTextHeaderHoliday = 0xffd0d0d0;

    public final static int iColorText = 0xffdddddd;
    public final static int iColorBkg = 0xff888888;
    public final static int iColorTextHoliday = 0xfff0f0f0;
    public final static int iColorBkgHoliday = 0xffaaaaaa;

    public final static int iColorTextToday = 0xff002200;
    public final static int iColorBkgToday = 0xff88bb88;

    public final static int iColorTextSelected = 0xff001122;
    public final static int iColorBkgSelectedLight = 0xffbbddff;
    public final static int iColorBkgSelectedDark = 0xff225599;

    public final static int iColorTextFocused = 0xff221100;
    public final static int iColorBkgFocusLight = 0xffffddbb;
    public final static int iColorBkgFocusDark = 0xffaa5500;

    // methods
    public static int getColorFrameHeader(boolean bHoliday) {
        if (bHoliday)
            return iColorFrameHeaderHoliday;
        return iColorFrameHeader;
    }

    public static int getColorTextHeader(boolean bHoliday) {
        if (bHoliday)
            return iColorTextHeaderHoliday;
        return iColorTextHeader;
    }

    public static int getColorText(boolean bHoliday, boolean bToday) {
        if (bToday)
            return iColorTextToday;
        if (bHoliday)
            return iColorTextHoliday;
        return iColorText;
    }

    public static int getColorBkg(boolean bHoliday, boolean bToday) {
        if (bToday)
            return iColorBkgToday;
        if (bHoliday)
            return iColorBkgHoliday;
        return iColorBkg;
    }

    public static int getWeekDay(int index, int iFirstDayOfWeek) {
        int iWeekDay = -1;

        if (iFirstDayOfWeek == Calendar.MONDAY) {
            iWeekDay = index + Calendar.MONDAY;
            if (iWeekDay > Calendar.SATURDAY)
                iWeekDay = Calendar.SUNDAY;
        }

        if (iFirstDayOfWeek == Calendar.SUNDAY) {
            iWeekDay = index + Calendar.SUNDAY;
        }

        return iWeekDay;
    }

}
----------------------------------------------------

package com.test.calendar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.widget.Button;

public class SymbolButton extends Button {
    // fields
    private static final int iColor = 0xffaaaaaa;
    private static final int iColorActive = 0xff442200;

    // fields
    public enum symbol {
        none, arrowLeft, arrowRight
    };

    // fields
    private Paint pt = new Paint();
    private RectF rect = new RectF();
    private RectF rectDraw = new RectF();
    private symbol symbolType = symbol.none;

    // methods
    public SymbolButton(Context context, symbol symbolType) {
        super(context);
        this.symbolType = symbolType;
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        pt.setAntiAlias(true);
        pt.setStrokeCap(Paint.Cap.ROUND);

        rectDraw.set(0, 0, getWidth(), getHeight());
        rectDraw.left += 6;
        rectDraw.right -= 6;
        rectDraw.top += 4;
        rectDraw.bottom -= 8;

        if (symbolType != symbol.none) {
            pt.setStrokeWidth(5);

            pt.setColor(iColor);
            if (this.isPressed() || this.isFocused())
                pt.setColor(iColorActive);

            drawArrow(canvas);
        }
    }

    private void drawArrow(Canvas canvas) {
        rect.set(rectDraw);
        rect.inset(15, 5);
        canvas.drawLine(rect.left, rect.centerY(), rect.right, rect.centerY(),
                pt);
        if (symbolType == symbol.arrowRight) {
            canvas.drawLine(rect.right, rect.centerY(), rect.right - 6,
                    rect.top, pt);
            canvas.drawLine(rect.right, rect.centerY(), rect.right - 6,
                    rect.bottom, pt);
        }
        if (symbolType == symbol.arrowLeft) {
            canvas.drawLine(rect.left, rect.centerY(), rect.left + 6, rect.top,
                    pt);
            canvas.drawLine(rect.left, rect.centerY(), rect.left + 6,
                    rect.bottom, pt);
        }
    }

}

分享到:
评论

相关推荐

    android日历实现__GO桌面版&&开源收藏版

    "android日历实现__GO桌面版&&开源收藏版"这个标题暗示我们将讨论一个与GO桌面版Android日历相关的项目,同时该项目可能是开源的,意味着我们可以深入研究其代码并学习实现细节。 **1. Android日历API介绍** ...

    Android 日历实现(包含农历)

    总之,实现一个包含农历功能的Android日历应用,需要熟练掌握Android的ContentProvider、自定义视图绘制、手势识别以及第三方库的集成。通过不断优化和迭代,可以打造出一款功能丰富、用户体验良好的日历应用。

    Android日历的实现

    以下是关于这个Android日历实现的相关知识点: 1. **Android SDK和API**: Android系统提供了Calendar类,它是java.util.Calendar的子类,用于处理日期和时间。此外,Android API还提供了CalendarView控件,但它的...

    Android日历可左右活动上下定位收缩

    "Android日历可左右活动上下定位收缩"这个项目的核心在于实现一个高度自定义的日历控件,以下将详细解释实现这一功能的关键知识点。 首先,我们需要了解Android的View体系结构。在Android中,自定义视图通常继承自...

    Android日历签到

    综上所述,实现“Android日历签到”功能,需要掌握Android日历API的使用,包括获取日历权限、查询日历源、创建和管理日历事件,以及在UI上展示签到信息。通过这一系列步骤,用户就能在自己的Android设备上方便地进行...

    android 日历

    这个项目为开发者提供了一个完整的Android日历实现,旨在帮助学习和理解日历功能的实现细节。 首先,Android日历API提供了与系统日历集成的能力,包括创建、更新和删除日历事件。在"LetvCalendar"源码中,我们可以...

    android日历demo

    在Android平台上,开发一款日历应用可以帮助用户管理他们的日常计划和事件。本示例"android日历DemoCalendar"提供了一个...通过研究这个Demo,开发者可以深入理解Android日历应用的实现原理,并为自己的项目提供参考。

    android实现日历控件

    本压缩包提供的是关于如何在Android中实现自定义日历控件的源代码示例。下面将详细介绍如何在Android项目中创建并使用自定义的日历视图。 首先,我们需要理解Android中的日期和时间处理。Android系统提供了`java....

    Android日历行程.rar

    这个"Android日历行程"的压缩包文件可能包含了关于如何在Android平台上操作和开发日历应用的相关资料,比如`Calendar_01`可能是一个文档或者源代码,详细讲解了Android日历API的使用。 Android的日历API提供了丰富...

    Android 使用RecycleView打造自定义日历

    在这个项目中,我们将会探讨如何使用RecycleView来打造一个自定义的日历界面,这将涉及到Android UI设计、自定义适配器以及事件处理等多个方面的知识。 首先,RecycleView的核心在于其强大的复用机制,通过视图池...

    自定义实现Android日历

    在Android平台上,自定义日历视图是一种常见的需求,它能够提供更为个性化和功能丰富的用户体验。Caldroid是一个流行的开源库,专为Android设计,用于创建美观且易用的日历组件。通过Caldroid,开发者可以轻松地在...

    android日历改进版,可直接项目中使用。

    本项目提供了一个经过改进的Android日历实现,可以直接应用于实际项目中。改进的主要内容包括对行数判断的优化,以便更好地展示日期并实现选中状态的标注。 1. **日历视图定制** - Android原生的日历组件可能无法...

    基于Android日历及记事功能完整实现.pdf

    根据提供的文件内容,可以提炼出以下与Android日历及记事功能完整实现相关的知识点: 1. Android开发基础 在讨论Android日历和记事功能的实现之前,需要对Android开发有一个基础的理解。Android开发通常使用Java或...

    android 日历、日程记录

    下面我们将深入探讨Android日历和日程记录的相关知识点。 一、Android系统日历API Android提供了Calendar Provider API,允许应用访问和修改用户的日历数据。开发者可以通过ContentResolver和Uri对象来操作日历和...

    Android 日历、Android 农历日历 Android 日历节假日

    以下是对标题“Android日历、Android农历日历、Android日历节假日”以及描述中所述知识点的详细阐述。 1. **Android日历API**: Android提供了`android.provider.CalendarContract`类,它是Android系统日历服务的...

    Android日历

    本文将深入探讨Android日历相关的知识点,基于提供的"Android日历"源代码,我们将涵盖以下主题:日历API的使用、日历应用的架构、权限管理、UI设计、事件操作以及如何实现自定义日历视图。 1. **Android日历API**:...

    Android日历+记事本源码

    这个“Android日历+记事本源码”项目,正是实现了这样的功能,它将日历组件与记事本功能巧妙地结合在一起,为用户提供了一个实用的工具。 首先,我们来详细了解一下日历功能的实现。在Android中,日历功能主要通过`...

Global site tag (gtag.js) - Google Analytics