Android Add image to text (in text View)?

2019-02-03 02:50发布

first post here=) I've been looking for this answer and haven't been able to find it. What I want to do is have some text, then add a image, then rest of text. For example:

                             ____
                            |   |
Hi there, this is the photo |___|, hope you like it..

I've been looking but all I can find is add text to image or add image to image View, and I don't think that's what I want because the app is mainly text but with images on it.

So my question is: How do I add an Image to text?

thanks


UPDATE: I used the advice R.daneel.olivaw gave me and it worked nice=)

But I have a problem. lets say i have: "a b c" where I set the position of b as spanable. But if I try to delete the text and I delete b, the next time I write something it will become the image I used in spanable. How do I correct this? Any1one got any advice? thanks=)

5条回答
ゆ 、 Hurt°
2楼-- · 2019-02-03 03:17

your best option is to create your own view, and to override the onDraw() method using canvas.drawText() for text, and canvas.drawBitmap() for images.

See the Canvas doc : http://developer.android.com/reference/android/graphics/Canvas.html

Here is an example which draw some text at the center of the screen :

public class OverlayView extends View {

public OverlayView(final Context context) {
    super(context);
}

/**
 * Draw camera target.
 */
@Override
protected void onDraw(final Canvas canvas) {

    // view size
    int width = getWidth();
    int height = getHeight();

    float square_side = height - width * 0.8f; // size of the target square

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL);

    // text size is 5% of the screen height
    paint.setTextSize(height * 0.05f);

    // draw message depending of its width
    String message = getResources().getString(R.string.photo_target_text);

    float message_width = paint.measureText(message);

    paint.setColor(getResources().getColor(R.color.color_foreground));
    canvas.drawText(message, (width - message_width) / 2,
            (height - square_side) / 4, paint);

    super.onDraw(canvas);
}

}
查看更多
ゆ 、 Hurt°
3楼-- · 2019-02-03 03:19

I think you are looking for the Spannable interface, By using this you can add images to a text view.

This link might help.

enter image description here

查看更多
再贱就再见
4楼-- · 2019-02-03 03:29

This can be done with image getter: Html.ImageGetter

You will have to use HTML tags for each image.

查看更多
冷血范
5楼-- · 2019-02-03 03:43

as I know, there's no way to do it.

but when you use webview and generated html you can do simillar one.

generate html from your contents, and load it to webview, set webview settings to no zoom control.

查看更多
家丑人穷心不美
6楼-- · 2019-02-03 03:44
< string name="text" > "Hi there, this is the photo [img src=img.jpge/] , hope you like it. < / string>

Add this in to your String and it will work

查看更多
登录 后发表回答