android - adding a String over a Drawable image?

前端 未结 3 1576
猫巷女王i
猫巷女王i 2020-12-29 16:08

I\'m trying to add a String to a Drawable image. I\'m currently not using a Panel to draw and I\'d like to keep it that way. Any idea

3条回答
  •  悲哀的现实
    2020-12-29 16:39

    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);
    

提交回复
热议问题