Android Canvas drawText不起作用 [英] Android Canvas drawText not working

查看:598
本文介绍了Android Canvas drawText不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用画布的drawText方法创建了一个自定义视图.不知何故,这些文本都没有显示在任何Jelly Bean设备上.它适用于ICS及以下版本.

I made a custom view using canvas's drawText method. Somehow none of the text is showing on any of the Jelly Bean devices. It works fine for ICS and below.

有人知道该方法或任何相关方法的内容是否从API 15更改为16吗?

Does anyone know if anything has changed from API 15 to 16 for this method or any related methods?

编辑代码:(来自提供画布作为参数的draw方法)

Edit Code: (from the draw method where canvas is supplied as a parameter)

    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStrokeWidth(3);
    paint.setColor(context.getResources().getColor(R.color.plot_background));
    canvas.drawRect(new Rect(0,0,getWidth(),getHeight()), paint);
    paint.setColor(color_text);
    paint.setTextSize(getScaled(18.5f));
    paint.setTextAlign(Align.CENTER);
    canvas.drawText(title, (graphwidth / 2) + horstart, border/2+15, paint);

我知道该行已执行并且坐标正确,因为相同的代码可在较旧的平台上工作.

I know the line is been executed and the coordinates are correct because the same code works on the older platforms.

推荐答案

感谢Eric.找出错误.我基于canvas.getDensity()缩放应用程序中的所有内容.由于某种原因,对于软心豆粒糖设备,此时draw函数始终为0的getDensity().但它确实会为1.6-> 4.0.3

Thanks Eric. Figured out the error. I scale everything in the app base on canvas.getDensity(). getDensity() at the moment the draw function is ALWAYS 0 for jelly bean devices for some reason. But it does return the correct value for anything between 1.6 -> 4.0.3

我之所以没有发布代码(这是我的错)是因为我不怀疑getDensity()是问题所在,因为在过去两年中该应用程序投放市场以来从未如此.

I didn't post the code for that (which is my fault) is because I didn't suspect getDensity() to be the problem since it never did in the last two years while the app is in the market.

解决方法是修改getScaled函数.

The workaround was to modify the getScaled function.

public float getScaled(Canvas canvas,float in){
    return in * ( canvas.getDensity()==0 ? 1 : canvas.getDensity()/ 160.0f);
} 

文档确实说过可以返回DENSITY_NONE,但我认为可能发生的情况是在Jelly Bean中为您进行缩放,因为如果我将其乘以1,它就可以在两个不同密度的设备上发挥作用我刚刚测试.

The documentation does say that DENSITY_NONE could be returned but I think what might have happened is that in Jelly Bean does the scaling for you since if I just multiply it by 1, it works as a charm on the two different density device that I just tested on.

(附言:如果我输入错误或确认,熟悉Android操作系统内部的任何人都可以纠正我吗?)

(P.S. Can anyone familiar the internals of Android OS correct me if I am wrong or confirm it? )

这篇关于Android Canvas drawText不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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