Android 自定义图文混排 完全版

1.自定义HtmlTextView

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.orhanobut.logger.Logger;
import com.sito.library.utils.LocalImageGetter;
import com.sito.library.utils.UrlImageGetter;

import org.xml.sax.XMLReader;

/**
 * Created by zhangan on 2017-06-15.
 */

public class HtmlTextView extends android.support.v7.widget.AppCompatTextView {

    public static final String TAG = "HtmlTextView";
    public static final boolean DEBUG = false;

    public HtmlTextView(Context context) {
        super(context);
    }

    public HtmlTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public void setHtmlFromString(String html, boolean useLocalDrawables,String host,String cook) {
        Html.ImageGetter imgGetter;
        if (useLocalDrawables) {
            imgGetter = new LocalImageGetter(getContext());
        } else {
            imgGetter = new UrlImageGetter(getContext(), this, TextUtils.isEmpty(host)?"":host,TextUtils.isEmpty(cook)?"":cook);
        }
        try {
            setText(Html.fromHtml(html, imgGetter, new MTagHandler()));
            setMovementMethod(LinkMovementMethod.getInstance());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return false;
    }

    Html.TagHandler tagHandler = new Html.TagHandler() {
        boolean first = true;
        String parent = null;
        int index = 1;

        @Override
        public void handleTag(boolean opening, String tag, Editable output,
                              XMLReader xmlReader) {
            Logger.d(tag + " output " + output + "opening " + opening);
            if (tag.equals("ul")) parent = "ul";
            else if (tag.equals("ol")) parent = "ol";
            if (tag.equals("li")) {
                if (parent.equals("ul")) {
                    if (first) {
                        output.append("\n\t•");
                        first = false;
                    } else {
                        first = true;
                    }
                } else {
                    if (first) {
                        output.append("\n\t" + index + ". ", 0, output.length() + 1);
                        first = false;
                        index++;
                    } else {
                        first = true;
                    }
                }
            }
        }

    };


}
2.LocalImageGetter类
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html;

public class LocalImageGetter implements Html.ImageGetter {

    Context c;

    public LocalImageGetter(Context c) {
        this.c = c;
    }

    @Override
    public Drawable getDrawable(String source) {
        int id = c.getResources().getIdentifier(source, "drawable", c.getPackageName());
        if (id == 0) {
            id = c.getResources().getIdentifier(source, "drawable", "android");
        }
        if (id == 0) {
            return null;
        } else {
            Drawable d = c.getResources().getDrawable(id);
            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
            return d;
        }
    }
}

3.UrlImageGetter类

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.orhanobut.logger.Logger;
import com.sito.library.utils.LocalImageGetter;
import com.sito.library.utils.UrlImageGetter;

import org.xml.sax.XMLReader;

/**
 * Created by zhangan on 2017-06-15.
 */

public class HtmlTextView extends android.support.v7.widget.AppCompatTextView {

    public static final String TAG = "HtmlTextView";
    public static final boolean DEBUG = false;

    public HtmlTextView(Context context) {
        super(context);
    }

    public HtmlTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public void setHtmlFromString(String html, boolean useLocalDrawables,String host,String cook) {
        Html.ImageGetter imgGetter;
        if (useLocalDrawables) {
            imgGetter = new LocalImageGetter(getContext());
        } else {
            imgGetter = new UrlImageGetter(getContext(), this, TextUtils.isEmpty(host)?"":host,TextUtils.isEmpty(cook)?"":cook);
        }
        try {
            setText(Html.fromHtml(html, imgGetter, new MTagHandler()));
            setMovementMethod(LinkMovementMethod.getInstance());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return false;
    }

    Html.TagHandler tagHandler = new Html.TagHandler() {
        boolean first = true;
        String parent = null;
        int index = 1;

        @Override
        public void handleTag(boolean opening, String tag, Editable output,
                              XMLReader xmlReader) {
            Logger.d(tag + " output " + output + "opening " + opening);
            if (tag.equals("ul")) parent = "ul";
            else if (tag.equals("ol")) parent = "ol";
            if (tag.equals("li")) {
                if (parent.equals("ul")) {
                    if (first) {
                        output.append("\n\t•");
                        first = false;
                    } else {
                        first = true;
                    }
                } else {
                    if (first) {
                        output.append("\n\t" + index + ". ", 0, output.length() + 1);
                        first = false;
                        index++;
                    } else {
                        first = true;
                    }
                }
            }
        }

    };


}

4.用法

布局中

            <com.xxx.widget.HtmlTextView
                android:id="@+id/id_recase_content"
                android:layout_marginTop="@dimen/dp_10"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:text="内容"
                android:textColor="@color/list_content"
                android:textSize="14sp"/>

页面代码

找到自定义图文混排控件‘

mContent.setHtmlFromString(caseInfo.getCaseContent(),false, Api.HOST, Api.getCookieStr())

猜你喜欢

转载自blog.csdn.net/yangzongbin/article/details/81179865