简体   繁体   English

Android Canvas drawText不起作用

[英]Android Canvas drawText not working

I made a custom view using canvas's drawText method. 我使用画布的drawText方法创建了一个自定义视图。 Somehow none of the text is showing on any of the Jelly Bean devices. 不知何故,这些文本都没有显示在任何Jelly Bean设备上。 It works fine for ICS and below. 它适用于ICS及以下版本。

Does anyone know if anything has changed from API 15 to 16 for this method or any related methods? 有人知道该方法或任何相关方法的内容是否已从API 15更改为16吗?

Edit Code: (from the draw method where canvas is supplied as a parameter) 编辑代码:(从提供画布作为参数的draw方法中进行)

    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. 我知道该行已执行并且坐标正确,因为相同的代码可在较旧的平台上工作。

Thanks Eric. 谢谢埃里克。 Figured out the error. 找出错误。 I scale everything in the app base on canvas.getDensity(). 我基于canvas.getDensity()缩放应用程序中的所有内容。 getDensity() at the moment the draw function is ALWAYS 0 for jelly bean devices for some reason. 由于某种原因,对于软心豆粒糖设备,此时draw函数始终为0的getDensity()。 But it does return the correct value for anything between 1.6 -> 4.0.3 但是它确实为1.6-> 4.0.3之间的任何值返回正确的值

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

The workaround was to modify the getScaled function. 解决方法是修改getScaled函数。

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

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

(PS Can anyone familiar the internals of Android OS correct me if I am wrong or confirm it? ) (PS:如果我输入错误或确认,熟悉Android OS内部结构的人可以纠正我吗?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM