功能:图文混排,可自动缩放字体,如图:
单点触控使用的代码来自:http://blog.csdn.net/xiaanming/article/details/42833893 谢谢博主!
在该demo中只是将bitmap改为了显示图文混排的效果,不足之处,请大家指正,共同进步!
贴上2个重要的方法:
private void MeasureBitmapTextSize(HSpannableString htr) //计算图文内容需要的bitmap高宽以及对应的字体大小
{
//根据字体大小重新计算字体大小
int columnSpace = 5; //列间距
int rowSpace = 5; //行间距
float charWidth = 0; //每个字符占的宽度,以一个英文字符为单位
float charHeight = 0; //每个字符的高度 Paint tryPaint = new Paint();
tryPaint.setTextAlign(Paint.Align.CENTER);
tryPaint.setAntiAlias(true); //设置画笔为无锯齿
// tryPaint.setTextSize(mTextSize);
tryPaint.setColor(mTextColor); float curRowLen = 0;
mBmpWidth = 0;
mBmpHeight = 0;
int size = htr.stringList.size(); int curTextSize = (int) mTextSize;
if(mTextSize==min_textsize)
{
curTextSize = max_textsize;
}
for(;curTextSize > min_textsize;curTextSize--) //从当前字体开始循环缩小
{
mBmpHeight = 0;
tryPaint.setTextSize(curTextSize);
FontMetrics fontMetrics = tryPaint.getFontMetrics();
charHeight = fontMetrics.bottom - fontMetrics.top;
curRowLen = columnSpace;
for(int i=0;i<size;i++)
{
Hstring hs = htr.stringList.get(i);
if(hs.type==Hstring.TYPE_TEXT)
{
charWidth = tryPaint.measureText(hs.ch+"",0,1);
}
else //图片元素,,一个图片占一个中文方块字的高宽
{
charWidth = tryPaint.measureText("哈"+"",0,1);
}
curRowLen += (columnSpace + charWidth);
Log.e("resetBitmap", "curRowLen="+curRowLen+";charWidth="+charWidth+";mTextSize="+mTextSize+";mBmpHeight="+mBmpHeight);
if(curRowLen > (mParentWidth-TEXT_PADDING*2)) //换行
{
Log.e("resetBitmap", "TAG1:curRowLen="+curRowLen+";mParentWidth="+mParentWidth+";TEXT_PADDING="+TEXT_PADDING);
//需要换行
curRowLen = charWidth+ columnSpace;
mBmpWidth = (mParentWidth - TEXT_PADDING*2);
mBmpHeight += (charHeight+rowSpace);
Log.e("resetBitmap", "TAG1:mBmpWidth="+mBmpWidth+";i="+i+";size="+size);
if(i>=(size-1)) //说明是最后一行
{
mBmpHeight += (charHeight+rowSpace);
}
}
else if(i>=(size-1) && curRowLen > minBmpWidth && mBmpWidth!=(mParentWidth- TEXT_PADDING*2))//第一行时
{
mBmpWidth = (int) curRowLen;
mBmpHeight = (int)(charHeight+rowSpace);
Log.e("resetBitmap", "TAG2:mBmpWidth="+mBmpWidth);
break;
}
else if(i>=(size-1) && mBmpWidth!=(mParentWidth- TEXT_PADDING*2))//第一行时
{
mBmpWidth = minBmpWidth;
mBmpHeight = (int) (charHeight+rowSpace);
Log.e("resetBitmap", "TAG3:mBmpWidth="+mBmpWidth);
break;
}
else if(i>=(size-1) && mBmpWidth == (mParentWidth- TEXT_PADDING*2)) //当前有换行
{
mBmpWidth = (mParentWidth- TEXT_PADDING*2);
mBmpHeight += (charHeight+rowSpace);
Log.e("resetBitmap", "TAG4:mBmpWidth="+mBmpWidth);
break;
}
}
if(mBmpHeight<=(mParentHeight - TEXT_PADDING*2) || curTextSize <=min_textsize)
{
mTextSize = curTextSize;
break;
}
}
// Log.v("resetBitmap", "mBmpWidth="+mBmpWidth+";mBmpHeight="+mBmpHeight);
}
再次,将内容画到bitmap上去(mBmpWidth和 mBmpHeight)
/**
* 将图文内容生成图片
* @param htr
* @param textSize
* @param textColor
* @return
*/
public Bitmap createTextBitmap(HSpannableString htr,float textSize,int textColor)
{
//设置初始属性
int columnSpace = 5; //列间距
int rowSpace = 5; //行间距
float curX =0,curY=0;
// float curRowWidth = 0; //当前所在行的实时宽度
MeasureBitmapTextSize(htr); //重新设置改bitmap的高宽
if(mBmpWidth <=0 || mBmpHeight <=0)
{
mBmpWidth = minBmpWidth;
mBmpHeight = minBmpHeight;
}
Bitmap bitmap = Bitmap.createBitmap(mBmpWidth, mBmpHeight, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
//画背景
Paint bgpaint = new Paint();
bgpaint.setColor(Color.parseColor("#CEFFCE"));
Rect bgrect = new Rect(0, 0, mBmpWidth, mBmpHeight);
canvas.drawRect(bgrect, bgpaint);
//画内容
Paint fontPaint = new Paint();
fontPaint.setTextAlign(Paint.Align.CENTER);
fontPaint.setAntiAlias(true); //设置画笔为无锯齿
fontPaint.setTextSize(textSize);
fontPaint.setColor(textColor); // 计算每一个坐标
// float baseX = 0;
// float baseY = rowSpace;
FontMetrics fontMetrics = fontPaint.getFontMetrics();
float fontTotalHeight = fontMetrics.bottom - fontMetrics.top;
// float offY = fontTotalHeight / 2 - fontMetrics.bottom;
// float newY = baseY + offY;
curX = columnSpace;
curY = fontTotalHeight - fontMetrics.descent;
float charWidth= 0;
float tryCurWidth=0; //用于判断换行的
for(int i=0;i<htr.stringList.size();i++)
{
Hstring hs = htr.stringList.get(i);
if(hs.type==Hstring.TYPE_TEXT) //文本
{
charWidth = fontPaint.measureText(hs.ch+"",0,1);
//计算会不会超出边界
tryCurWidth = curX + (columnSpace + charWidth); if(tryCurWidth > (mParentWidth-TEXT_PADDING*2))
{
curY += rowSpace+fontTotalHeight;
curX = columnSpace;
}
else
{
curX = tryCurWidth-(columnSpace + charWidth);
}
Log.e("createTextBitmap3", "curX="+curX+";hs.ch="+hs.ch+";charWidth="+charWidth);
canvas.drawText(hs.ch+"", curX+charWidth/2, curY, fontPaint);
curX += (columnSpace + charWidth);
tryCurWidth = 0;
}
else //图片
{
charWidth = fontPaint.measureText("哈"+"",0,1);
//预算会不会超出边界
tryCurWidth = curX + (columnSpace + charWidth);
if(tryCurWidth > (mParentWidth-TEXT_PADDING*2))
{
curY += rowSpace+fontTotalHeight;
curX = columnSpace;
}
else
{
curX = tryCurWidth-(columnSpace + charWidth);
}
Rect srcRect = new Rect();
srcRect.left = 0;
srcRect.top = 0;
srcRect.right = htr.stringList.get(i).bmp.getWidth();
srcRect.bottom = htr.stringList.get(i).bmp.getWidth() + htr.stringList.get(i).bmp.getHeight();
Rect bmpRect = new Rect((int)curX,(int)(curY + fontMetrics.ascent+fontMetrics.leading),(int)(curX+columnSpace + charWidth),(int)(curY -fontMetrics.ascent));
Log.v("图片", "curY="+curY+";ascent="+fontMetrics.ascent+";leading="+fontMetrics.leading);
Paint bmpPaint = new Paint();
Log.e("createTextBitmap3", "curX="+curX+";hs.ch="+hs.ch+";charWidth="+charWidth);
canvas.drawBitmap(htr.stringList.get(i).bmp, srcRect,bmpRect, bmpPaint);
curX += (columnSpace + charWidth);
tryCurWidth = 0;
}
}
return bitmap;
}