2011.09.13(3)——— android 添加快捷方式并且图标上添加数字

前两个日志的合并

我们可以给桌面添加快捷方式的时候 制定一个带数字的图片




1、添加一个方法

private Bitmap generatorContactCountIcon(Bitmap icon){    //初始化画布    int iconSize=(int)getResources().getDimension(android.R.dimen.app_icon_size);    Bitmap contactIcon=Bitmap.createBitmap(iconSize, iconSize, Config.ARGB_8888);    Canvas canvas=new Canvas(contactIcon);        //拷贝图片    Paint iconPaint=new Paint();    iconPaint.setDither(true);//防抖动    iconPaint.setFilterBitmap(true);//用来对Bitmap进行滤波处理,这样,当你选择Drawable时,会有抗锯齿的效果    Rect src=new Rect(0, 0, icon.getWidth(), icon.getHeight());    Rect dst=new Rect(0, 0, iconSize, iconSize);    canvas.drawBitmap(icon, src, dst, iconPaint);        //在图片上创建一个覆盖的联系人个数    int contacyCount=11;    //启用抗锯齿和使用设备的文本字距    Paint countPaint=new Paint(Paint.ANTI_ALIAS_FLAG|Paint.DEV_KERN_TEXT_FLAG);    countPaint.setColor(Color.RED);    countPaint.setTextSize(20f);    countPaint.setTypeface(Typeface.DEFAULT_BOLD);    canvas.drawText(String.valueOf(contacyCount), iconSize-18, 25, countPaint);    return contactIcon;    }


上面
int contacyCount=11;

这个是写死的 你可以在程序里面更具业务来控制

2、修改方法:

private void installShortCut(){Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));// 是否可以有多个快捷方式的副本,参数如果是true就可以生成多个快捷方式,如果是false就不会重复添加shortcutIntent.putExtra("duplicate", false);Intent mainIntent = new Intent(Intent.ACTION_MAIN);mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);// 要删除的应用程序的ComponentName,即应用程序包名+activity的名字//intent2.setComponent(new ComponentName(this.getPackageName(), this.getPackageName() + ".MainActivity"));mainIntent.setClass(this, this.getClass());shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mainIntent);//shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, generatorContactCountIcon(((BitmapDrawable)(getResources().getDrawable(R.drawable.icon))).getBitmap()));sendBroadcast(shortcutIntent);}



以前是根据id来找到图片
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));


现在改为根据图片:
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, generatorContactCountIcon(((BitmapDrawable)(getResources().getDrawable(R.drawable.icon))).getBitmap()));


这样既可

但是 这样会不停的删除 创建快捷方式 更新太多
网上貌似大家都是用widget来做的 这个有空可以看一下


更多相关文章

  1. android常用开源库分享
  2. android 中的二维码生成与去除白边
  3. 由Eclipse导入Android(安卓)Studio的.9图片报错的处理
  4. Android(安卓)利用url获取Bitmap图片
  5. android背景选择器selector用法汇总
  6. android studio 中添加 Volley module
  7. Android(安卓)Log日志保存到本地并读取
  8. android背景选择器selector用法汇总
  9. android实现图片按任意路径截取

随机推荐

  1. 自定义view-制作一个加载中的圆形
  2. android 对话框详解
  3. Android(安卓)签名信息读取
  4. android之RecycleView之ItemTouchHelper
  5. Android WiFi 架构总览(模块及接口)
  6. ionic build android log
  7. 播放音乐时的状态条使用
  8. Android AD Manifest
  9. Android(安卓)中的 values XML
  10. 分享:Android程序员,必备精品网站大汇总