安卓数学公式 FlexibleRichTextView 的使用

最近项目中有用到数学键盘公式
例如:数学公式:这里写图片描述
用户输入键盘后产生的数据应该就是这样的
$$ \\[ \\sum_{k=1}^n k^2 = \\frac{1}{2} n (n+1).\\] $$
但是我们肯定不能显示出这样的一长串东西出来给用户。
基于目前现状,一直想着寻找替换方案,最近寻找了一下解决方案,惊奇的发现现在已经有支持Latex原生渲染的开源框架了。今天来学习使用一下。它就是:FlexibleRichTextView。

源码GitHub地址:
FlexibleRichTextView
基本标签介绍:
initLable 点这里
详细介绍:
API详细介绍

基本使用:
在你的项目build.gradle文件中add,注意不是app目录下的build.gradle文件

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

同时 在你app的build.gradle文件 add

compile 'com.github.daquexian:FlexibleRichTextView:0.8.2'

接下来就是尝试了:
现在你的Application或者活动中初始化

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        //init flexiblerichtextview 
        AjLatexMath.init(this);
        //code highlight
        CodeProcessor.init(this);
    }
}

在布局文件中添加一个:

 <com.daquexian.flexiblerichtextview.FlexibleRichTextView
        android:id="@+id/id_rich_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

在主活动中:

FlexibleRichTextView flexibleRichTextView = findViewById(R.id.id_rich_tv);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("$$\\sum_{i=1}^n a_i=0$$,");
        stringBuilder.append("\r\n");
        stringBuilder.append("$$f(x)=x^{x^x}$$");
        stringBuilder.append("\r\n");
        stringBuilder.append("$$f(x_1,x_x,\\ldots,x_n) = x_1^2 + x_2^2 + \\cdots + x_n^2 $$");
        stringBuilder.append("\r\n");
        stringBuilder.append("$$\\left. \\frac{du}{dx} \\right|_{x=0}.$$");
        stringBuilder.append("\r\n");
        stringBuilder.append("f(n) = \\begin{cases} \\frac{n}{2}, & \\text{if } n\\text{ is even} \\\\ 3n+1, & " +
                "\\text{if } n\\text{ is odd} \\end{cases}");
        stringBuilder.append("\r\n");
        stringBuilder.append("$$\\mbox{对任意的$x>0$}, \\mbox{}f(x)>0. $$");
        stringBuilder.append("\r\n");
        stringBuilder.append("$$\\sqrt[n]{x_r_r_r} $$");
        stringBuilder.append("\r\n");
        stringBuilder.append("$$ \\frac{x+2}{x} \\sqrt{x} $$");
        stringBuilder.append("\r\n");
        stringBuilder.append("$$ \\[f(x,y,z) = 3y^2 z \\left( 3 + \\frac{7x+5}{1 + y^2} \\right).\\] $$");
        stringBuilder.append("\r\n");
        stringBuilder.append("$$ P(x|c)=\\frac{P(c|x)\\cdot P(x)}{P(x)} $$");
        stringBuilder.append("\r\n");
        stringBuilder.append("$$ \\Large x=\\frac{-b\\pm\\sqrt{b^2-4ac}}{2a} $$");
        stringBuilder.append("\r\n");
        stringBuilder.append("$$ \\sum_{i=1}^n i = \\frac{n(n+1)}2 $$");
        stringBuilder.append("\r\n");
        stringBuilder.append("$$ f(x)=\\int_{-\\infty}^x e^{-t^2}dt $$ 这道公式我也不知道怎么做");
        stringBuilder.append("\r\n");
        stringBuilder.append("$$ \\cos 2\\theta  = \\cos^2 \\theta - \\sin^2 \\theta = 2 \\cos^2 \\theta - 1. $$");
        stringBuilder.append("\r\n");
        stringBuilder.append("$$ \\displaystyle= \\frac{k(k+1)}{2}+k+1 $$");
        stringBuilder.append("\r\n");
        stringBuilder.append("$$ \\frac{x}{2}-3=0 $$");
        stringBuilder.append("\r\n");
        stringBuilder.append("$$ x=\\frac{3}{2} $$");
        stringBuilder.append("\r\n");
        stringBuilder.append("$$ \\[ \\sum_{k=1}^n k^2 = \\frac{1}{2} n (n+1).\\] $$");
        flexibleRichTextView.setText(stringBuilder.toString());

运行一下看看效果:

好像没什么问题。
可以看到解析公式的时候都是以”美元符号“开始以“美元符号”结束。(CSDN把美元符号也作为标签了 不能输入 。。。。)
今天就是做一个简单的尝试。更详细介绍的可以参考上面GitHub的链接地址。

猜你喜欢

转载自blog.csdn.net/lyjSmile/article/details/82497778
今日推荐