android 画图
2012-09-13 16:59
134 查看
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;
}
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;
}
相关文章推荐
- Android画图之Matrix(一)
- Android画图Path类的使用
- Android入门之画图详解
- Android画图drawText入门(二)-绘制文本
- Android画图Path的使用_2
- Android画图之抗锯齿
- (转)Android画图学习总结(三)——Drawable
- Android 画图方式总结
- 【转】Android vector Path Data画图详解
- 通过Dreamweaver画图把android复杂多点击的页面转换成html,再通过android和js的交互实现点击
- Android画图之Matrix(二)
- Android画图之抗锯齿paint和Canvas两种方式实例
- android画图-----shape的使用
- Android画图最基本的三个对象(Color,Paint,Canvas)
- Android画图并保存图片(转载)
- Android apk 里面的2D和3D画图 详解
- android游戏开发笔记(1)——画图及输入响应(一只被玩弄的老鼠)
- Android画图最基本的三个对象(Color,Paint,Canvas)
- android 画图之setXfermode
- 熟悉Android 画图