我怎么能用9补丁绘制一个文本

时间:2013-07-03 13:03:13

标签: java android drawable nine-patch

我已经知道如何在普通的drawable上绘制文字:

 public BitmapDrawable writeOnDrawable(int drawableId, String text){

    Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);

    Paint paint = new Paint(); 
    paint.setStyle(Style.FILL);  
    paint.setColor(Color.BLACK); 
    paint.setTextSize(20); 

    Canvas canvas = new Canvas(bm);
    canvas.drawText(text, 0, bm.getHeight()/2, paint);

    return new BitmapDrawable(bm);
}

甚至是这样:

Drawable image = getResources().getDrawable(tile_types[tileType]);
// Store our image size as a constant
final int IMAGE_WIDTH = image.getIntrinsicWidth();
final int IMAGE_HEIGHT = image.getIntrinsicHeight();

// You can also use Config.ARGB_4444 to conserve memory or ARGB_565 if 
// you don't have any transparency.
Bitmap canvasBitmap = Bitmap.createBitmap(IMAGE_WIDTH, 
                                      IMAGE_HEIGHT, 
                                      Bitmap.Config.ARGB_8888);
// Create a canvas, that will draw on to canvasBitmap. canvasBitmap is
// currently blank.
Canvas imageCanvas = new Canvas(canvasBitmap);
// Set up the paint for use with our Canvas
Paint imagePaint = new Paint();
imagePaint.setTextAlign(Align.CENTER);
imagePaint.setTextSize(16f);

// Draw the image to our canvas
image.draw(imageCanvas);
// Draw the text on top of our image
imageCanvas.drawText("Sample Text", 
                     IMAGE_WIDTH / 2, 
                     IMAGE_HEIGHT / 2, 
                     imagePaint);
// This is the final image that you can use 
BitmapDrawable finalImage = new BitmapDrawable(canvasBitmap);

,但我如何使用能够占据我输入的文字大小的9-patch drawable。

0 个答案:

没有答案