运行结果: 
 

此Widget组件只包含五个类文件: 

  • DateWidget
  • DateWidgetDayCell
  • DateWidgetDayHeader
  • DayStyle
  • SymbolButton


*红色为启动的activity 

DateWidget.java:

package com.iaiai;

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.LinearLayout.LayoutParams;
import android.widget.TextView;
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;
}

}


DateWidgetDayCell.java: 

Java代码
package com.iaiai;

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

}

DateWidgetDayHeader.java: 

Java代码
package com.iaiai;

import java.util.Calendar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.View;
import android.widget.LinearLayout.LayoutParams;

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

}
DayStyle.java: 
Java代码
package com.iaiai;

import java.util.Calendar;

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

}

SymbolButton.java: 
Java代码
package com.iaiai;

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

/**
*
* <p>
* Title: SymbolButton.java
* </p>
* <p>
* E-Mail: 176291935@qq.com
* </p>
* <p>
* QQ: 176291935
* </p>
* <p>
* Http: iaiai.iteye.com
* </p>
* <p>
* Create time: 2011-8-19
* </p>
*
*
@author 丸子
*
@version 0.0.1
*/
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);
}
}

}


转自:http://www.iteye.com/topic/1113688