Android自定义View实现HTML图文环绕效果

来源:互联网 发布:用友网络好消息 编辑:程序博客网 时间:2024/04/28 19:15

Android中并没有提供HTML图文环绕效果的View,最接近的算是TextView中的ImageSpan了,但并未完全实现图文环绕(图文混排)的效果。

1、Android系统TextView的ImageSpan实现图文环绕

Android中TextView实现图文环绕

代码如下:

1
2
3
4
5
6
7
8
TextView tv = new TextView(this);
       
SpannableString spanStr = new SpannableString("掌声那历史的房间里是副经理撒旦法阿斯顿及福利费是到发顺丰");
ImageSpan imageSpan = new ImageSpan(this, R.drawable.a);
spanStr.setSpan(imageSpan, 3, 5, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
tv.setText(spanStr);
       
setContentView(tv);


2、Android中自定义View实现图文环绕

android自定义view实现图文环绕

代码如下:

1
2
3
4
FloatImageText view = new FloatImageText(this);
view.setText("电视里发生了房间里是积分拉萨积分拉萨积分拉萨减肥啦空间  撒旦法发大水发撒旦法看完了鸡肉味容积率为热键礼物i经二路文件容量为积分拉萨解放路口上飞机撒离开房间爱水立方法拉圣诞节福禄寿");
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.a);
view.setImageBitmap(bm, 30, 30);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.orgcent.view;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.FontMetrics;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
/**
 * 模拟CSS中的float浮动效果
 */

public class FloatImageText extends View {
    private Bitmap mBitmap;
    private final Rect bitmapFrame= new Rect();
    private final Rect tmp = new Rect();
    private int mTargetDentity = DisplayMetrics.DENSITY_DEFAULT;
   
    private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private String mText;
    private ArrayList<TextLine> mTextLines;
    private final int[] textSize = new int[2];

    public FloatImageText(Context context,AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
        init();
    }

    public FloatImageText(Context context,AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FloatImageText(Context context){
        super(context);
        init();
    }
   
    private void init(){
        mTargetDentity = getResources().getDisplayMetrics().densityDpi;
        mTextLines = new ArrayList<TextLine>();
       
        mPaint.setTextSize(14);
        mPaint.setColor(Color.RED);
       
    }
   
   

    @Override
    protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {
        int w = 0, h = 0;
        //图片大小
        w += bitmapFrame.width();
        h += bitmapFrame.height();
       
        //文本宽度
        if(null!= mText && mText.length()> 0) {
            mTextLines.clear();
            int size = resolveSize(Integer.MAX_VALUE, widthMeasureSpec);
            measureAndSplitText(mPaint, mText, size);
            final int textWidth= textSize[0], textHeight= textSize[1];
            w += textWidth; //内容宽度
            if(h < textHeight) { //内容高度
                h = (int) textHeight;
            }
        }
       
        w = Math.max(w, getSuggestedMinimumWidth());
        h = Math.max(h, getSuggestedMinimumHeight());
       
        setMeasuredDimension(
                resolveSize(w, widthMeasureSpec),
                resolveSize(h, heightMeasureSpec));
    }
   
    @Override
    protected void onDraw(Canvas canvas){
        //绘制图片
        if(null!= mBitmap) {
            canvas.drawBitmap(mBitmap,null, bitmapFrame, null);
        }
       
        //绘制文本
        TextLine line;
        final int size = mTextLines.size();
        for(int i= 0; i< size; i++){
            line = mTextLines.get(i);
            canvas.drawText(line.text, line.x, line.y, mPaint);
        }
        System.out.println(mTextLines);
    }
   
   
    public void setImageBitmap(Bitmap bm){
        setImageBitmap(bm, null);
    }
   
    public void setImageBitmap(Bitmap bm,int left, int top){
        setImageBitmap(bm, new Rect(left, top,0, 0));
    }
   
    public void setImageBitmap(Bitmap bm, Rect bitmapFrame){
        mBitmap = bm;
        computeBitmapSize(bitmapFrame);
        requestLayout();
        invalidate();
    }
   
    public void setText(String text){
        mText = text;
        requestLayout();
        invalidate();
    }
   
    private void computeBitmapSize(Rect rect){
        if(null!= rect) {
            bitmapFrame.set(rect);
        }
        if(null!= mBitmap) {
            if(rect.right== 0 && rect.bottom== 0) {
                final Rect r = bitmapFrame;
                r.set(r.left, r.top,
                        r.left + mBitmap.getScaledHeight(mTargetDentity),
                        r.top + mBitmap.getScaledHeight(mTargetDentity));
            }
        } else {
            bitmapFrame.setEmpty();
        }
    }
   
    private void measureAndSplitText(Paint p,String content, int maxWidth){
        FontMetrics fm = mPaint.getFontMetrics();
        final int lineHeight = (int)(fm.bottom - fm.top);
       
        final Rect r = new Rect(bitmapFrame);
//        r.inset(-5, -5);
       
        final int length = content.length();
        int start = 0, end = 0, offsetX = 0, offsetY = 0;
        int availWidth = maxWidth;
        TextLine line;
        boolean onFirst = true;
        boolean newLine = true;
        while(start < length) {
            end++;
            if(end == length) { //剩余的不足一行的文本
                if(start <= length - 1){
                    if(newLine) offsetY+= lineHeight;
                    line = new TextLine();
                    line.text = content.substring(start, end- 1);
                    line.x = offsetX;
                    line.y = offsetY;
                    mTextLines.add(line);
                }
                break;
            }
            p.getTextBounds(content, start, end, tmp);
            if(onFirst){ //确定每个字符串的坐标
                onFirst = false;
                final int height= lineHeight + offsetY;
                if(r.top>= height) {//顶部可以放下一行文字
                    offsetX = 0;
                    availWidth = maxWidth;
                    newLine = true;
                } else if(newLine && (r.bottom >= height && r.left >= tmp.width())){ //中部左边可以放文字
                    offsetX = 0;
                    availWidth = r.left;
                    newLine = false;
                } else if(r.bottom >= height && maxWidth - r.right>= tmp.width()){ //中部右边
                    offsetX = r.right;
                    availWidth = maxWidth - r.right;
                    newLine = true;
                } else { //底部
                    offsetX = 0;
                    availWidth = maxWidth;
                    if(offsetY < r.bottom) offsetY = r.bottom;
                    newLine = true;
                }
            }
           
            if(tmp.width()> availWidth) { //保存一行能放置的最大字符串
                onFirst = true;
                line = new TextLine();
                line.text = content.substring(start, end- 1);
                line.x = offsetX;
                mTextLines.add(line);
                if(newLine){
                    offsetY += lineHeight;
                    line.y = offsetY;
                } else {
                    line.y = offsetY+ lineHeight;
                }
               
                start = end - 1;
            }
        }
        textSize[1]= offsetY;
    }
   
    class TextLine {
        String text;
        int x;
        int y;
       
        @Override
        public String toString(){
            return "TextLine [text="+ text + ", x="+ x + ", y="+ y + "]";
        }
    }
}

转载注明本文地址: http://orgcent.com/android-imagespan-view-html/


原创粉丝点击