设置动态壁纸字体类型导致屏幕变黑
我有这段代码,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试仅初始化字体一次(不要在每次调用
drawText2()
时加载它)也不需要以下行:
Try to initialize the font once only(do not load it everytime
drawText2()
called)Also the following line is not needed:
这是在黑暗中进行的疯狂尝试:)
您应该检查用于设置字体的代码是否在
与字体初始化代码相同的线程。
该领域存在一些已知问题,尤其是在您进行设置时
在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