Code example for Paint

Methods: getFontMetricsInt, getTextBounds, getTypeface, measureText, setAntiAlias, setColor, setShadowLayer, setTextSize, setTypeface

0
 
        Canvas canvas = new Canvas(mWorkingBitmap);
        Log.i(TAG, "- Canvas: " + canvas
              + "  dimensions: " + canvas.getWidth() + " x " + canvas.getHeight());
 
        Paint textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(FONT_SIZE);
        textPaint.setColor(0xFFFFFFFF);
        Log.i(TAG, "- Paint: " + textPaint);
 
        Typeface face = textPaint.getTypeface();
        Log.i(TAG, "- default typeface: " + face);
 
        // The most standard font for lolcat captions is Impact.  (Arial 
        // Black is also common.)  Unfortunately we don't have either of 
        // these on the device by default; the closest we can do is 
        // DroidSans-Bold: 
        face = Typeface.DEFAULT_BOLD;
        Log.i(TAG, "- new face: " + face);
        textPaint.setTypeface(face);
 
        // Look up the positions of the captions, or if this is our very 
        // first time rendering them, initialize the positions to default 
        // values. 
 
        final int edgeBorder = 20;
        final int fontHeight = textPaint.getFontMetricsInt(null);
        Log.i(TAG, "- fontHeight: " + fontHeight);
 
        Log.i(TAG, "- Caption positioning:");
        int topX = 0;
        int topY = 0;
        if (topStringValid) {
            if (mCaptions[0].positionValid) {
                topX = mCaptions[0].xpos;
                topY = mCaptions[0].ypos;
                Log.i(TAG, "  - TOP: already had a valid position: " + topX + ", " + topY);
            } else { 
                // Start off with the "top" caption at the upper-left: 
                topX = edgeBorder;
                topY = edgeBorder + (fontHeight * 3 / 4);
                mCaptions[0].setPosition(topX, topY);
                Log.i(TAG, "  - TOP: initializing to default position: " + topX + ", " + topY);
            } 
        } 
 
        int bottomX = 0;
        int bottomY = 0;
        if (bottomStringValid) {
            if (mCaptions[1].positionValid) {
                bottomX = mCaptions[1].xpos;
                bottomY = mCaptions[1].ypos;
                Log.i(TAG, "  - Bottom: already had a valid position: "
                      + bottomX + ", " + bottomY);
            } else { 
                // Start off with the "bottom" caption at the lower-right: 
                final int bottomTextWidth = (int) textPaint.measureText(bottomString);
                Log.i(TAG, "- bottomTextWidth (" + bottomString + "): " + bottomTextWidth);
                bottomX = canvas.getWidth() - edgeBorder - bottomTextWidth;
                bottomY = canvas.getHeight() - edgeBorder;
                mCaptions[1].setPosition(bottomX, bottomY);
                Log.i(TAG, "  - BOTTOM: initializing to default position: "
                      + bottomX + ", " + bottomY);
            } 
        } 
 
        // Finally, render the text. 
 
        // Standard lolcat captions are drawn in white with a heavy black 
        // outline (i.e. white fill, black stroke).  Our Canvas APIs can't 
        // do this exactly, though. 
        // We *could* get something decent-looking using a regular 
        // drop-shadow, like this: 
        //   textPaint.setShadowLayer(3.0f, 3, 3, 0xff000000); 
        // but instead let's simulate the "outline" style by drawing the 
        // text 4 separate times, with the shadow in a different direction 
        // each time. 
        // (TODO: This is a hack, and still doesn't look as good 
        // as a real "white fill, black stroke" style.) 
 
        final float shadowRadius = 2.0f;
        final int shadowOffset = 2;
        final int shadowColor = 0xff000000;
 
        // TODO: Right now we use offsets of 2,2 / -2,2 / 2,-2 / -2,-2 . 
        // But 2,0 / 0,2 / -2,0 / 0,-2 might look better. 
 
        textPaint.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, shadowColor);
        if (topStringValid) canvas.drawText(topString, topX, topY, textPaint);
        if (bottomStringValid) canvas.drawText(bottomString, bottomX, bottomY, textPaint);
        // 
        textPaint.setShadowLayer(shadowRadius, -shadowOffset, shadowOffset, shadowColor);
        if (topStringValid) canvas.drawText(topString, topX, topY, textPaint);
        if (bottomStringValid) canvas.drawText(bottomString, bottomX, bottomY, textPaint);
        // 
        textPaint.setShadowLayer(shadowRadius, shadowOffset, -shadowOffset, shadowColor);
        if (topStringValid) canvas.drawText(topString, topX, topY, textPaint);
        if (bottomStringValid) canvas.drawText(bottomString, bottomX, bottomY, textPaint);
        // 
        textPaint.setShadowLayer(shadowRadius, -shadowOffset, -shadowOffset, shadowColor);
        if (topStringValid) canvas.drawText(topString, topX, topY, textPaint);
        if (bottomStringValid) canvas.drawText(bottomString, bottomX, bottomY, textPaint);
 
        // Stash away bounding boxes for the captions if this 
        // is our first time rendering them. 
        // Watch out: the x/y position we use for drawing the text is 
        // actually the *lower* left corner of the bounding box... 
 
        int textWidth, textHeight;
 
        if (topStringValid && mCaptions[0].captionBoundingBox == null) {
            Log.i(TAG, "- Computing initial bounding box for top caption...");
            textPaint.getTextBounds(topString, 0, topString.length(), mTmpRect);
            textWidth = mTmpRect.width();
            textHeight = mTmpRect.height();
            Log.i(TAG, "-  text dimensions: " + textWidth + " x " + textHeight);
            mCaptions[0].captionBoundingBox = new Rect(topX, topY - textHeight,
                                                       topX + textWidth, topY);
            Log.i(TAG, "-   RESULTING RECT: " + mCaptions[0].captionBoundingBox);
        } 
        if (bottomStringValid && mCaptions[1].captionBoundingBox == null) {
            Log.i(TAG, "- Computing initial bounding box for bottom caption...");
            textPaint.getTextBounds(bottomString, 0, bottomString.length(), mTmpRect);
            textWidth = mTmpRect.width();
            textHeight = mTmpRect.height();
            Log.i(TAG, "-  text dimensions: " + textWidth + " x " + textHeight);
            mCaptions[1].captionBoundingBox = new Rect(bottomX, bottomY - textHeight,
                                                       bottomX + textWidth, bottomY);