安卓數學公式 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的鏈接地址。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章