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 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭