您的位置:首页 > 移动开发 > Android开发

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;

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息