Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用9补丁绘制文本_Java_Android_Drawable_Nine Patch - Fatal编程技术网

Java 如何使用9补丁绘制文本

Java 如何使用9补丁绘制文本,java,android,drawable,nine-patch,Java,Android,Drawable,Nine Patch,我已经知道如何在常规可绘制图形上绘制文本: public BitmapDrawable writeOnDrawable(int drawableId, String text){ Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true); Paint paint = new Paint(); paint.setSt

我已经知道如何在常规可绘制图形上绘制文本:

 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可绘制文件,它将能够占用我输入的文本大小。

您不能将9-patch作为TextView的背景吗?我正在尝试做的是将其用作mapview中的覆盖项,因此我需要传递一个可绘制文件。您找到了这样做的方法吗?我正在尝试做完全相同的事情。仍然没有什么,我必须使用正常的绘图。