2016-01-07 40 views
1

我想在ImageView中顯示Android中的LaTex項目。我正在使用jlatexmath庫。到目前爲止,我從公式中得到了TexIcon對象。現在我想在ImageView中顯示該內容(或者任何地方,只需要在活動中顯示它們)。一種方法是將其轉換爲位圖或PNG,然後在ImageView顯示,但我不能TexIcon轉換爲其他格式,這是我的java代碼至今:在Android中顯示LaTex

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    String math = "\\frac {V_m} {K_M+S}"; 
    TeXFormula fomule = new TeXFormula(math); 
    TeXIcon ti = fomule.createTeXIcon(TeXConstants.STYLE_DISPLAY, 40); 
} 

我是比較新的LaTex,感謝先進!

回答

2

編輯:我試圖在安卓但依賴它需要的金額(如從Java AWT和Swing包)移植到Android的使用jlatexMath,我不得不離開它半途而廢。

這是從JLatex回購但BufferedImage的和插圖中未提供的Android中可用的樣本代碼。

TeXFormula formula = new TeXFormula(latex); 
    // Note: Old interface for creating icons: 
    //TeXIcon icon = formula.createTeXIcon(TeXConstants.STYLE_DISPLAY, 20); 
    // Note: New interface using builder pattern (inner class): 
    TeXIcon icon = formula.new TeXIconBuilder().setStyle(TeXConstants.STYLE_DISPLAY).setSize(20).build(); 
    icon.setInsets(new Insets(5, 5, 5, 5)); 

    BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB); 
    Graphics2D g2 = image.createGraphics(); 
    g2.setColor(Color.white); 
    g2.fillRect(0,0,icon.getIconWidth(),icon.getIconHeight()); 
    JLabel jl = new JLabel(); 
    jl.setForeground(new Color(0, 0, 0)); 
    icon.paintIcon(jl, g2, 0, 0); 
    File file = new File("Example2.png"); 
    try { 
     ImageIO.write(image, "png", file.getAbsoluteFile()); 
    } catch (IOException ex) {} 

我會建議其他圖書館 - https://github.com/kexanie/MathView這是周圍的WebView的包裝,你可以使用MathJax或Katex公司庫。