自定义view实现图文环绕的效果

       为了实现类似于word里面的图文环绕的效果,参考了网络上的很多方法后选择了自定义view的方法。因为本人对webView的使用不是很擅长。

       参考了文章:http://mobile.51cto.com/abased-375949.htm

            后来发现,其实这个是已经封装好的方法。都不用我们去想算法。我真心感谢它的原始作者。为了以后引用和自我的巩固,所以决定写下来。

       首先,它的自定义view已经是成品了。我当一次搬运工了。

FloatImageText.java

package com.esri.arcgis.android.samples.helloworld;

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

	}

	@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 + "]";
		}
	}
}

接下来就是如何使用这个view了。我粗略滴了解了一下,使用只要在需要调用的地方添加text内容和图片内容并且设置一下图片显示的位置就可以了。我是显示在callout内。

这里是我callout的内部布局文件的设置:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/callouttile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textColor="#000000" />

        <ImageButton
            android:id="@+id/exitcallout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:background="@drawable/exit" />
    </LinearLayout>

    <cn.com.esrichina.imagetext.FloatImageText
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

</LinearLayout>

接下来是我的callout显示的时候,对自定义view的初始化和调用:

private void DisplayCallout(Point point) {
		LayoutInflater inflater = LayoutInflater.from(GoogleMapActivity.this);
		View view = inflater.inflate(R.layout.callout, null);
		calloutTile = (TextView) view.findViewById(R.id.callouttile);
		FloatImageText imageText;
		imgTxtView = (FloatImageText) view.findViewById(R.id.view);
		imgTxtView.setMinimumWidth(300);
		imgTxtView.setMinimumHeight(100);
		callout = mapView.getCallout();
		SetCalloutContent(checkGraphic);
		
		callout.setStyle(R.xml.calloutstyle);
		callout.setMaxWidth(400);
		callout.setMaxHeight(400);
		callout.refresh();
		callout.show(point, view);
	}


private void SetCalloutContent(int i) {
		
		int a, b;
		a = 2;
		b = 6;
		// callout内部控件设置
		if (isRelease.get(i) == 1) {
			calloutTile.getPaint().setFakeBoldText(true);// 字体加粗
			calloutTile.setText(stnm.get(i) + "(已解除)");
			if (lev.get(i) == 1) {
				Bitmap bm = BitmapFactory.decodeResource(getResources(),
						R.drawable.icon_blue);
				imgTxtView.setImageBitmap(bm, a, b);// 调节图片显示的方位。
			} else {
				Bitmap bm = BitmapFactory.decodeResource(getResources(),
						R.drawable.icon_yellow);
				imgTxtView.setImageBitmap(bm, a, b);
			}
		} else {
			calloutTile.getPaint().setFakeBoldText(true);// 字体加粗
			calloutTile.setText(stnm.get(i));
			if (lev.get(i) == 1) {
				Bitmap bm = BitmapFactory.decodeResource(getResources(),
						R.drawable.icon_blue);
				imgTxtView.setImageBitmap(bm, a, b);
				} else {
				Bitmap bm = BitmapFactory.decodeResource(getResources(),
						R.drawable.icon_yellow);
				imgTxtView.setImageBitmap(bm, a, b);
				}
		}
		imgTxtView.setText(content.get(i));// end callout config

	}


这样子就已经实现了调用。效果图再上一张吧。



最后在附上网上找到的利用webview的技术贴地址:http://www.easymorse.com/index.php/archives/1464

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值