如何寫日曆中的一個小方塊
阿新 • • 發佈:2019-02-01
public class PaintView extends View { private static final String TAG = "CalendarView"; /** * 預設cell上下左右間距:4px,使繪製的View有網格效果 */ public static final float DEFAULT_SPACING_CELL = 4f; /** * 預設最右邊和最左邊間距:4px,使繪製的View有網格效果 */ public static final float DEFAULT_SPACING_CELL_LEFT_RIGHT = DEFAULT_SPACING_CELL / 2; /** * 預設一個cell減去這個數的4倍後三等分,用以繪製狀態圖示的座標位置(使圖示之間有空隙) */ private final float DEFAULT_AVG_CELL = 10f; /** * 一個cell三等分,用以繪製狀態圖示的座標位置(寬) */ private float defaultAvgCellWidth = 1f; /** * 一個cell三等分,用以繪製狀態圖示的座標位置(高) */ private float defaultAvgCellHeight = 1f; /** * 文字畫筆預設大小 */ private float defaultTextPatinSize = 1f; /** * 兩種模式 (月份和星期):暫時不需要 */ public static final int MONTH_STYLE = 0; public static final int WEEK_STYLE = 1; /** * 日曆模組上面的字型大小 */ private static final float CALENDAR_TEXT_SIZE = 17f; /** * 周首日兩種風格 (一週第一天是週日和一週第一天是週一) */ public static final int SUN_WEEK_STYLE = 0;//一週第一天是週日 public static final int MON_WEEK_STYLE = 1;//一週第一天是週一 public static final int TOTAL_COL = 7; public static final int TOTAL_ROW = 6; private Paint mRectPaint; private Paint mTextPaint; private Paint mBgPaint; private float mViewWidth; private float mViewHight; private float mCellSpaceWidth; private float mCellSpaceHeight; private Handler mHandler = new Handler(); public PaintView(Context context, AttributeSet attrs) { super(context, attrs); setBgPaint(R.color.lactation_tutorial_background_white_an_alp); // 預測經期 initCalendarItem(context); setTextPaint(mTextPaint.getColor(), defaultTextPatinSize); } private void fillIcon(Canvas canvas, int i, int j) { } private void showIconA(Canvas canvas, int i, int j, Bitmap bitmap) { float cellRight = (float) ((i + 1.0) * mCellSpaceWidth) + DEFAULT_SPACING_CELL * (i + 1.0f) + DEFAULT_SPACING_CELL_LEFT_RIGHT; float cellTop = (float) (j * mCellSpaceHeight) + DEFAULT_SPACING_CELL * (j + 1.0f); canvas.drawBitmap(bitmap, (float) (cellRight - bitmap.getWidth() - DEFAULT_AVG_CELL), (float) (cellTop + DEFAULT_AVG_CELL), mTextPaint); } private void initCalendarItem(Context context) { mViewWidth = DeviceUtils.getScreenWidth(context); mViewHight = DeviceUtils.getScreenHeight(context); mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mCellSpaceWidth = (mViewWidth - DEFAULT_SPACING_CELL * 7) / TOTAL_COL;// Math.min((int) (mViewHight - DEFAULT_SPACING_CELL * 7) / mCellSpaceHeight = mCellSpaceWidth * 0.85f;//cell長寬比4:3 LogUtils.i("initCalendarItem->>mViewWidth", mViewWidth + "::" + mCellSpaceWidth); defaultTextPatinSize = mCellSpaceHeight / 3f; defaultAvgCellWidth = (mCellSpaceWidth - 4 * DEFAULT_AVG_CELL) / 3f; defaultAvgCellHeight = (mCellSpaceHeight - 4 * DEFAULT_AVG_CELL) / 3f; defaultTextPatinSize = DeviceUtils.sp2px(context, CALENDAR_TEXT_SIZE); mTextPaint.setTextSize(defaultTextPatinSize); } private void setTextPaint(int color, float size) { if (mTextPaint == null) { mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); } mTextPaint.setTextSize(size); mTextPaint.setColor(color); } private void drawDateNo(Canvas canvas, String content, int i, int j) { canvas.drawText(content, (float) ((i + 0.3) * mCellSpaceWidth - mTextPaint.measureText(content) / 2) + DEFAULT_SPACING_CELL * (i) + DEFAULT_SPACING_CELL_LEFT_RIGHT, (float) ((j + 0.6) * mCellSpaceHeight - mTextPaint.measureText( content, 0, 1) / 2) + DEFAULT_SPACING_CELL * (j), mTextPaint); } private void setBgPaint(int color) { if (mBgPaint == null) { mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mBgPaint.setStyle(Paint.Style.FILL); } mBgPaint.setColor(color); } @Override public void draw(Canvas canvas) { super.draw(canvas); drawBg(canvas, 3, 3, mBgPaint); showIconA(canvas, 3, 3, CalendarItemBitmapHolder.getBitmapViaResId(getContext(), 13)); drawDateNo(canvas, "12", 3, 3); } private void drawBg(Canvas canvas, int i, int j, Paint mPaint) { Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setStyle(Paint.Style.FILL); paint.setColor(Color.RED); RectF rectF = new RectF(); rectF.left = (i * mCellSpaceWidth) + DEFAULT_SPACING_CELL * (i) + DEFAULT_SPACING_CELL_LEFT_RIGHT; //左邊 rectF.top = (j * mCellSpaceHeight) + DEFAULT_SPACING_CELL * (j + 1); rectF.right = (float) ((i + 1.0) * mCellSpaceWidth) + DEFAULT_SPACING_CELL * (i) + DEFAULT_SPACING_CELL_LEFT_RIGHT; rectF.bottom = (float) ((j + 1.0) * mCellSpaceHeight) + DEFAULT_SPACING_CELL * (j + 1); canvas.drawRoundRect(rectF, 10f, 10f, paint); } }
效果是如下圖: