2016-01-07 121 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公司库。