android 달력 아이콘 표시 요일

3590 단어 이동개발하다
이전 문서에서는 달력 아이콘에 현재 날짜를 표시하는 것에 대해 설명했습니다.
현재 나는 달력 아이콘에 대응하는 주를 표시할 수 있는 몇 줄의 코드를 추가했다.
소스 수정은 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;
        }
    }

좋은 웹페이지 즐겨찾기