android 画图
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;
}
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer