How to write text on an image

前端 未结 4 1050
鱼传尺愫
鱼传尺愫 2020-12-14 04:56

I can do this in Objective-C on the iPhone, but now I\'m looking for the equivalent Android Java code. I can also do it in plain Java, but I don\'t know what the Android spe

相关标签:
4条回答
  • 2020-12-14 05:25

    What about something like:

    Bitmap bitmap = ... // Load your bitmap here
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(); 
    paint.setColor(Color.BLACK); 
    paint.setTextSize(10); 
    canvas.drawText("Some Text here", x, y, paint);
    
    0 讨论(0)
  • 2020-12-14 05:30

    I think this is also a good solution that can solve your problem. Or download the source code demo

    public Bitmap drawText(String text, int textWidth, int color) {
    
            // Get text dimensions
            TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
            textPaint.setStyle(Paint.Style.FILL);
            textPaint.setColor(Color.parseColor("#ff00ff"));
            textPaint.setTextSize(30);
    
            StaticLayout mTextLayout = new StaticLayout(text, textPaint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
    
            // Create bitmap and canvas to draw to
            Bitmap b = Bitmap.createBitmap(textWidth, mTextLayout.getHeight(), Bitmap.Config.ARGB_4444);
            Canvas c = new Canvas(b);
    
            // Draw background
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(color);
            c.drawPaint(paint);
    
            // Draw text
            c.save();
            c.translate(0, 0);
            mTextLayout.draw(c);
            c.restore();
    
            return b;
        }
    

    The above function draws a bitmap image from a text Or download the source code demo and see how it works

    0 讨论(0)
  • 2020-12-14 05:38

    GETah's answer wasn't working for me so I tweeked it a bit, here's a Kotlin version, which works, using TextPaint:

    val canvas = Canvas(bm)
    val textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG or Paint.LINEAR_TEXT_FLAG)
    textPaint.style = Paint.Style.FILL
    textPaint.color = Color.BLACK
    textPaint.textSize = 30f
    canvas.drawText("Hello", 50f, 50f, textPaint)
    
    0 讨论(0)
  • 2020-12-14 05:43

    You don't need to use graphics.

    A simpler approach would be to create a FrameLayout with two elements- the ImageView for the image, and another view for whatever you want drawn on top.

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    
    </FrameLayout>
    

    Of course, the thing on top of the image doesn't need to be a simple TextView, it can be another image, or another layout containing whatever arbitrary elements you like.

    0 讨论(0)
提交回复
热议问题