设置动态壁纸字体类型导致屏幕变黑

发布于 2024-12-06 06:58:01 字数 1174 浏览 0 评论 0原文

我有这段代码,

void drawText2(Canvas c)
    {       

        DisplayMetrics metrics = new DisplayMetrics();
        Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
        display.getMetrics(metrics);

        int screenwidth = metrics.widthPixels;
        int screenheight = metrics.heightPixels;



            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.BLACK);
            paint.setTextSize(150);
            paint.setAntiAlias(true);
            paint.setTypeface(Typeface.DEFAULT_BOLD);

            Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);
            float x = screenwidth/2;
            float y = screenheight/2;
            c.drawText("32", x, y, paint);

    }

效果很好,但如果我添加以下行

Typeface GC=Typeface.createFromAsset(getAssets(),"fonts/ADarling.ttf");

并将该行更改

paint.setTypeface(Typeface.DEFAULT_BOLD);

paint.setTypeface(Typeface.create(GC, 0));

它将使用字体,一切似乎都工作正常,但壁纸会随机变黑,并保持这种状态一段时间几分钟后它会再次出现,并且会继续这样做。我使用的代码错误吗?

I have this code

void drawText2(Canvas c)
    {       

        DisplayMetrics metrics = new DisplayMetrics();
        Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
        display.getMetrics(metrics);

        int screenwidth = metrics.widthPixels;
        int screenheight = metrics.heightPixels;



            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.BLACK);
            paint.setTextSize(150);
            paint.setAntiAlias(true);
            paint.setTypeface(Typeface.DEFAULT_BOLD);

            Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);
            float x = screenwidth/2;
            float y = screenheight/2;
            c.drawText("32", x, y, paint);

    }

Which works fine, but if I add in the following line

Typeface GC=Typeface.createFromAsset(getAssets(),"fonts/ADarling.ttf");

as well as change the line

paint.setTypeface(Typeface.DEFAULT_BOLD);

to

paint.setTypeface(Typeface.create(GC, 0));

It will use the font and everything seems to be working fine, but randomly the wallpaper will go black, and stay that way for a few minutes then it will appear again, and will continue to do this. Am I using the code wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梅窗月明清似水 2024-12-13 06:58:02

尝试仅初始化字体一次(不要在每次调用drawText2()时加载它)

private Typeface myFont;


public myConstructor() {  
    /* ... */  
    Typeface GC = Typeface.createFromAsset(getAssets(),"fonts/ADarling.ttf");  
    myFont = Typeface.create(GC, Typeface.NORMAL);
    /* ... */  
}

void drawText2(Canvas c)
    /* ... */  
    paint.setTypeface(myFont);
    /* ... */
}

也不需要以下行:

Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);

Try to initialize the font once only(do not load it everytime drawText2() called)

private Typeface myFont;


public myConstructor() {  
    /* ... */  
    Typeface GC = Typeface.createFromAsset(getAssets(),"fonts/ADarling.ttf");  
    myFont = Typeface.create(GC, Typeface.NORMAL);
    /* ... */  
}

void drawText2(Canvas c)
    /* ... */  
    paint.setTypeface(myFont);
    /* ... */
}

Also the following line is not needed:

Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);
生生漫 2024-12-13 06:58:02

这是在黑暗中进行的疯狂尝试:)

您应该检查用于设置字体的代码是否在
与字体初始化代码相同的线程。

该领域存在一些已知问题,尤其是在您进行设置时
在java中然后使用NDK来调整字体。这是因为 Java 和 NDK 在稍微不同的线程中运行,并且使用线程安全的内存分配系统。

只是一个想法,可能不适用于您。

托尼

Heres a wild stab in the dark:)

You should check that your code for setting the font is in the
same thread as the initialization code for the font.

There are some known issues in this area, especially if you are setting up things
in java then usings the NDK to adjust the font. This is because Java and the NDK operate in slightly different threads, and a thread safe memory allocation system is used.

Just a thought, which may not apply to you.

Tony

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文