android 日历图标显示星期

上一篇文档中谈到了在日历图标上显示当前日期,

现在我添加了几行代码,可以在日历图标上显示对应的星期;

修改源码是在Utilities.java中

函数createCalendarIconBitmap

添加几行代码:

int dayOfWeek = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
         String weekStrings[] = {"周日","周一","周二","周三","周四","周五","周六"};
		 String weekString = weekStrings[dayOfWeek];


Paint mWeekPaint = new Paint();
            mWeekPaint.setTypeface(Typeface.DEFAULT_BOLD);
            mWeekPaint.setTextSize((int)10F * mDensity);
            mWeekPaint.setColor(0xff000000);
            mWeekPaint.setAntiAlias(true);
            
            Rect rect1 = new Rect();
            mWeekPaint.getTextBounds(weekString,0,weekString.length(),rect1);
           int hoffset0 = 2;
            int width10 = rect1.right - rect1.left;
            int height10 = rect1.bottom - rect1.top + hoffset0;
            int width20 = calendarIcon.getWidth();
            int height20 = calendarIcon.getHeight() ;//+ hoffset0;
            
            canvas.drawText(weekString,(width20 - width10)/2 - rect1.left,height10,mWeekPaint);

修改后的函数为:

static Bitmap createCalendarIconBitmap(Drawable icon, Context context){
        Bitmap calendarIcon = createIconBitmap(icon,context);
        String dayString  = String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
		int dayOfWeek = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
         String weekStrings[] = {"周日","周一","周二","周三","周四","周五","周六"};
		 String weekString = weekStrings[dayOfWeek];
        Log.d("tanfusheng","weekString"+weekString);
        synchronized (sCanvas) {
            final Canvas canvas = sCanvas;
            canvas.setBitmap(calendarIcon);
            
            final float mDensity = context.getResources().getDisplayMetrics().density;
			
	    Paint mWeekPaint = new Paint();
            mWeekPaint.setTypeface(Typeface.DEFAULT_BOLD);
            mWeekPaint.setTextSize((int)10F * mDensity);
            mWeekPaint.setColor(0xff000000);
            mWeekPaint.setAntiAlias(true);
            Rect rect1 = new Rect();
            mWeekPaint.getTextBounds(weekString,0,weekString.length(),rect1);
            int hoffset0 = 2;
            int width10 = rect1.right - rect1.left;
            int height10 = rect1.bottom - rect1.top + hoffset0;
            int width20 = calendarIcon.getWidth();
            int height20 = calendarIcon.getHeight() ;//+ hoffset0;
            
            canvas.drawText(weekString,(width20 - width10)/2 - rect1.left,height10,mWeekPaint);
            
            Paint mDatePaint = new Paint();
            mDatePaint.setTypeface(Typeface.DEFAULT_BOLD);
            mDatePaint.setTextSize((int)30F * mDensity);
            mDatePaint.setColor(0xff000000);
            mDatePaint.setAntiAlias(true);
            Rect rect = new Rect();
            mDatePaint.getTextBounds(dayString,0,dayString.length(),rect);
            int hoffset = 20;
            int width1 = rect.right - rect.left;
            int height1 = rect.bottom - rect.top;
            int width2 = calendarIcon.getWidth();
            int height2 = calendarIcon.getHeight() + hoffset;
            
            canvas.drawText(dayString,(width2 - width1)/2 - rect.left,(height2 - height1)/2 - rect.top,mDatePaint);
            
            canvas.setBitmap(null);
            return calendarIcon;
        }
    }


android 日历图标显示星期,布布扣,bubuko.com

android 日历图标显示星期

上一篇:IOS中NSNotification使用笔记


下一篇:android开发异常信息收集程序代码