lkf181 2022-12-24 02:40 采纳率: 86.5%
浏览 25
已结题

求自动调整文字大小填满Canvas 屏幕区域的算法

想写一个“手持字幕屏幕”,即可以打字(尽量的大文字)给人看,我用的是:StaticLayout类在while (true){}中不断调用
现在想在输入时自动调整文字大小 我是if大 就减小if小就增大,这样会产生反复调整大小,没完没了的抖动,我现在暂时通过限制调整时间在9秒之内,勉强能用。
但肯定不是好办法

以下是主要实现代码:

    private  float g_resetTextSize;
    private TextPaint g_textPaint;
    private void drawString(Canvas p_canvasLocked){
        if (p_canvasLocked != null) {
            p_canvasLocked.rotate(90);// 字体旋转9度
            float l_left =  0;// l_centerX;
            float l_top = - Config.deviceHeight/2;
            float l_marging = 20;
            g_textPaint.setColor(Color.parseColor("#000000"));
            g_textPaint.setTextSize(g_resetTextSize);
            g_textPaint.setAntiAlias(true);

            StaticLayout l_layoutopen =new StaticLayout(Config.ShowTextMsg, g_textPaint, p_canvasLocked.getHeight(),
                    Layout.Alignment.ALIGN_CENTER,1.0f,0.0f , true);
//                    //超过Config.deviceHeight屏幕时,就会换行,也可以使用“\r\n”来实现换行
            //Log.i(Config.myTag, "p_canvasLocked.getHeight(): "+p_canvasLocked.getHeight());//3040
            int l_height = l_layoutopen.getHeight();

            if (Config.isReadjust){
                if((l_height - Config.deviceWidth) > l_marging ){//总行高度 大于屏幕宽度
                    g_resetTextSize  -= 5; //字体太大了需要减小
                }
                else if ((l_height - Config.deviceWidth) < l_marging ){
                    g_resetTextSize += 5;
                }
            }

            p_canvasLocked.save();
            p_canvasLocked.translate(l_left+l_marging,l_top+l_marging);
            l_layoutopen.draw(p_canvasLocked);
            p_canvasLocked.restore();

        }
        //return g_resetTextSize;
    }

  • 写回答

1条回答 默认 最新

  • 全栈CV工程师 2022-12-28 15:30
    关注

    单纯显示的话,用原生的TextView就可以做到自动调整字体大小了。autosize了解一下。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月28日
  • 已采纳回答 1月20日
  • 创建了问题 12月24日

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了