private Bitmap generatePhoneNumberIcon(Uri lookupUri, int type, int actionResId) {
final Resources r = getResources();
boolean drawPhoneOverlay = true;
final float scaleDensity = getResources().getDisplayMetrics().scaledDensity;


Bitmap photo = loadContactPhoto(lookupUri, null);
if (photo == null) {
// If there isn't a photo use the generic phone action icon instead
Bitmap phoneIcon = getPhoneActionIcon(r, actionResId);
if (phoneIcon != null) {
photo = phoneIcon;
drawPhoneOverlay = false;
} else {
return null;
}
}


// Setup the drawing classes
Bitmap icon = createShortcutBitmap();
Canvas canvas = new Canvas(icon);


// Copy in the photo
Paint photoPaint = new Paint();
photoPaint.setDither(true);
photoPaint.setFilterBitmap(true);
Rect src = new Rect(0,0, photo.getWidth(),photo.getHeight());
Rect dst = new Rect(0,0, mIconSize, mIconSize);
canvas.drawBitmap(photo, src, dst, photoPaint);


// Create an overlay for the phone number type
String overlay = null;
switch (type) {
case Phone.TYPE_HOME:
overlay = getString(R.string.type_short_home);
break;


case Phone.TYPE_MOBILE:
overlay = getString(R.string.type_short_mobile);
break;


case Phone.TYPE_WORK:
overlay = getString(R.string.type_short_work);
break;


case Phone.TYPE_PAGER:
overlay = getString(R.string.type_short_pager);
break;


case Phone.TYPE_OTHER:
overlay = getString(R.string.type_short_other);
break;
}
if (overlay != null) {
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
textPaint.setTextSize(20.0f * scaleDensity);
textPaint.setTypeface(Typeface.DEFAULT_BOLD);
textPaint.setColor(r.getColor(R.color.textColorIconOverlay));
textPaint.setShadowLayer(3f, 1, 1, r.getColor(R.color.textColorIconOverlayShadow));
canvas.drawText(overlay, 2 * scaleDensity, 16 * scaleDensity, textPaint);
}


// Draw the phone action icon as an overlay
if (ENABLE_ACTION_ICON_OVERLAYS && drawPhoneOverlay) {
Bitmap phoneIcon = getPhoneActionIcon(r, actionResId);
if (phoneIcon != null) {
src.set(0, 0, phoneIcon.getWidth(), phoneIcon.getHeight());
int iconWidth = icon.getWidth();
dst.set(iconWidth - ((int) (20 * scaleDensity)), -1,
iconWidth, ((int) (19 * scaleDensity)));
canvas.drawBitmap(phoneIcon, src, dst, photoPaint);
}
}


return icon;
}


private Bitmap framePhoto(Bitmap photo) {

final Resources r = getResources();
final Drawable frame = r.getDrawable(17302105);


final int width = r.getDimensionPixelSize(R.dimen.contact_shortcut_frame_width);
final int height = r.getDimensionPixelSize(R.dimen.contact_shortcut_frame_height);


frame.setBounds(0, 0, width, height);


final Rect padding = new Rect();
frame.getPadding(padding);


final Rect source = new Rect(0, 0, photo.getWidth(), photo.getHeight());
final Rect destination = new Rect(padding.left, padding.top,
width - padding.right, height - padding.bottom);


final int d = Math.max(width, height);
final Bitmap b = Bitmap.createBitmap(d, d, Bitmap.Config.ARGB_8888);
final Canvas c = new Canvas(b);


c.translate((d - width) / 2.0f, (d - height) / 2.0f);
frame.draw(c);
c.drawBitmap(photo, source, destination, new Paint(Paint.FILTER_BITMAP_FLAG));


return b;
}

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android Stdio实现点击按钮出现图片
  2. Android 通过Http访问Web端的Servlet
  3. 【Android】创建、读取XML文件
  4. Android如何设置圆角按钮
  5. 设置Android输入法的回车键
  6. android SAX
  7. Android Gradle版本和Android plugin版本
  8. Android中如何判断是否联网
  9. 通过Intent在Activity|Service之间传递数
  10. android sqlite 中文乱码。。麻烦详细点