代号一-放大或缩小所有样式的所有字体的方法 [英] Codename One - Method to enlarge or reduce all the fonts in all the styles

查看:100
本文介绍了代号一-放大或缩小所有样式的所有字体的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个具有一组复杂主题(重叠)的代号应用程序,或者只有一个复杂主题的应用程序。

Suppose that I have a Codename One app with a complex set of themes (that are overlayed) or an app that has only one theme that is complex.

我的问题是我是否可以使用简单的Java代码按给定的百分比放大或缩小所有样式的所有字体大小(以mm为单位)。

My question is if I can have simple Java code to enlarge or reduce by a given percentage all the font sizes (that are in mm) of all styles.

我知道我可以覆盖更改所有字体大小的主题,但在某些情况下可能会很痛苦。

I know that I can overlay a theme that changes all the font sizes, but in some conditions can be painful.

推荐答案

谢谢Shai,谢谢tizbn,我对您的两个答案都进行了冥想。之后,我创建了以下方法来执行我所要求的操作,也许对其他人可能有用。

Thank you Shai and thank you tizbn, I meditated on both your answers. After that, I created the following method to do what I asked, maybe can be useful to other people.

/**
 * Increases or reduces of a given percentage all the font sizes of a given
 * theme, overlaying a new theme containing only the new font sizes. The
 * font sizes will be increased when the percentage is positive, they will
 * be reduced when the percentage is negative. Legal percentage values are
 * from -90 to 300.
 *
 * Example of usage: changeFontSizesOfTheme(theme, "Theme", 50);
 *
 * @param theme the given resource file
 * @param themeName the name of given theme
 * @param percentage the given percentage to increase or reduce the font
 * sizes
 */
public static void changeFontSizesOfTheme(Resources theme, String themeName, int percentage) {
    if (theme.isTheme(themeName) && percentage != 0 && percentage >= -90 && percentage <= 300) {
        Hashtable hashtable = theme.getTheme("Theme");
        Hashtable overlay = new Hashtable();
        Set<String> keys = hashtable.keySet();
        Iterator<String> itr = keys.iterator();
        String key;
        Object value;
        int count = 0;
        while (itr.hasNext()) {
            key = itr.next();
            value = hashtable.get(key);
            if (value instanceof Font) {
                Font originalFont = (Font) value;
                float originalFontSize = originalFont.getPixelSize();
                float newFontSize = originalFontSize * (100 + percentage) / 100;
                overlay.put(key, originalFont.derive(newFontSize, originalFont.getStyle()));
                count++;
            }
        }
        UIManager.getInstance().addThemeProps(overlay);
        Log.p(count + " font sizes of the theme \"" + themeName + "\" were overlayed");
    }
}

这篇关于代号一-放大或缩小所有样式的所有字体的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆