android launcher 日历icon显示日期

转自:http://blog.csdn.net/tfslovexizi/article/details/28424143?utm_source=tuicool&utm_medium=referral

看到iphone上的日历图标上的数字会随着日期的变化而变化,最近在android平台上也研究了 一下,实现方法如下:

直接上源码

在launcher里改的

首先,在IconCache.java文件中,找到方法private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info,
            HashMap<Object, CharSequence> labelCache) 
在entry.icon = Utilities.createIconBitmap(icon, mContext); 这个位置修改:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">if(info.activityInfo.packageName.equals("com.android.calendar")){  
  2.                 entry.icon = Utilities.createCalendarIconBitmap(icon, mContext);  
  3.             }else{  
  4.                 if (index >= 0) {  
  5.                     entry.icon = Utilities.createIconBitmap(icon, mContext);  
  6.                 } else {  
  7.                     entry.icon = Utilities.createIconBitmap(  
  8.                             /* SPRD: Feature 253522, Remove the application drawer view @{ */  
  9.                             // getFullResIcon(info), mContext);  
  10.                         icon, mContext, true);  
  11.                 }  
  12.             }</span>  


改后源码如下:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info,  
  2.             HashMap<Object, CharSequence> labelCache) {  
  3.         CacheEntry entry = mCache.get(componentName);  
  4.         if (entry == null) {  
  5.             entry = new CacheEntry();  
  6.   
  7.             mCache.put(componentName, entry);  
  8.   
  9.             ComponentName key = LauncherModel.getComponentNameFromResolveInfo(info);  
  10.             if (labelCache != null && labelCache.containsKey(key)) {  
  11.                 entry.title = labelCache.get(key).toString();  
  12.             } else {  
  13.                 entry.title = info.loadLabel(mPackageManager).toString();  
  14.                 if (labelCache != null) {  
  15.                     labelCache.put(key, entry.title);  
  16.                 }  
  17.             }  
  18.             if (entry.title == null) {  
  19.                 entry.title = info.activityInfo.name;  
  20.             }  
  21.   
  22.              /* SPRD: Fix bug 281291, remove icon_top for theme defaut icon @{ */  
  23.             /* SPRD: UUI theme : system icons @{ */  
  24.             Drawable icon;  
  25.             /* SPRD: Fix bug294355, add just to ThemeManager. @{ */  
  26.             int index = sysIndexOf(componentName.getClassName());  
  27.             Log.i("jxt""index:"+index+",Name:"+componentName.getClassName());  
  28.             icon = getFullResIcon(info);  
  29.   
  30. //changed by  leo   
  31.             if(info.activityInfo.packageName.equals("com.android.calendar")){  
  32.                 entry.icon = Utilities.createCalendarIconBitmap(icon, mContext);  
  33.             }else{  
  34.                 if (index >= 0) {  
  35.                     entry.icon = Utilities.createIconBitmap(icon, mContext);  
  36.                 } else {  
  37.                     entry.icon = Utilities.createIconBitmap(  
  38.                             /* SPRD: Feature 253522, Remove the application drawer view @{ */  
  39.                             // getFullResIcon(info), mContext);  
  40.                         icon, mContext, true);  
  41.                 }  
  42.             }  
  43.             /* @} */  
  44.             /* @} */  
  45.   
  46.             /* @} */  
  47.         }  
  48.         return entry;  
  49.     }</span>  

接下来修改Utilities.java

添加一个函数:


[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">static Bitmap createCalendarIconBitmap(Drawable icon, Context context){  
  2.         Bitmap calendarIcon = createIconBitmap(icon,context);  
  3.         String dayString  = String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));  
  4.           
  5.         synchronized (sCanvas) {  
  6.             final Canvas canvas = sCanvas;  
  7.             canvas.setBitmap(calendarIcon);  
  8.               
  9.             final float mDensity = context.getResources().getDisplayMetrics().density;  
  10.               
  11.             Paint mDatePaint = new Paint();  
  12.             mDatePaint.setTypeface(Typeface.DEFAULT_BOLD);  
  13.             mDatePaint.setTextSize((int)30F * mDensity);  
  14.             mDatePaint.setColor(0xff000000);  
  15.             mDatePaint.setAntiAlias(true);  
  16.   
  17.             Rect rect = new Rect();  
  18.             mDatePaint.getTextBounds(dayString,0,dayString.length(),rect);  
  19.             int hoffset = 20;  
  20.             int width1 = rect.right - rect.left;  
  21.             int height1 = rect.bottom - rect.top;  
  22.             int width2 = calendarIcon.getWidth();  
  23.             int height2 = calendarIcon.getHeight() + hoffset;  
  24.               
  25.             canvas.drawText(dayString,(width2 - width1)/2 - rect.left,(height2 - height1)/2 - rect.top,mDatePaint);  
  26.               
  27.             canvas.setBitmap(null);  
  28.             return calendarIcon;  
  29.         }  
  30.     }</span>  
再修改LauncherModel.java文件:

在onReceive()方法中添加如下代码:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">//changed by leo  
  2.         }else if(Intent.ACTION_DATE_CHANGED.equals(action) ||  
  3.                 Intent.ACTION_TIME_CHANGED.equals(action) ||  
  4.                 Intent.ACTION_TIMEZONE_CHANGED.equals(action)){  
  5.             final String packageName = "com.android.calendar";  
  6.             enqueuePackageUpdated(new PackageUpdatedTask(  
  7.                     PackageUpdatedTask.OP_UPDATE, new String[]{packageName}));  
  8.         }</span>  
最后修改LauncherApplication.java文件,如果是launcher3的源码,则修改LauncherAppState.java文件

我这里修改的是

LauncherAppState.java

在构造函数 private LauncherAppState()中添加:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;"//changed by leo  
  2.         filter.addAction(Intent.ACTION_TIME_CHANGED);  
  3.         filter.addAction(Intent.ACTION_DATE_CHANGED);  
  4.         filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);  
  5.   
  6. </span>  

修改后的样式为:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;"// Register intent receivers  
  2.         IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);  
  3.         filter.addAction(Intent.ACTION_PACKAGE_REMOVED);  
  4.         filter.addAction(Intent.ACTION_PACKAGE_CHANGED);  
  5.         filter.addDataScheme("package");  
  6.         sContext.registerReceiver(mModel, filter);  
  7.         filter = new IntentFilter();  
  8.         filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);  
  9.         filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);  
  10.         filter.addAction(Intent.ACTION_LOCALE_CHANGED);  
  11.         filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);  
  12.  //changed by leo  
  13.         filter.addAction(Intent.ACTION_TIME_CHANGED);  
  14.         filter.addAction(Intent.ACTION_DATE_CHANGED);  
  15.         filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);  
  16.           
  17.         sContext.registerReceiver(mModel, filter);  
  18. </span>  
当然, 这样是不能运行看到效果的,要将该导入的包都导入 ,然后再单编译launcher模块,push到手机里,就能看到日历图标上显示当前日期了。

      今天看到好多人修改后引起了问题,我把源码提取出来了,放在资源里,需要的可以去下载查看,请点击链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值