0

我正在画布上的背景图像上绘制文本。我以交互方式移动图像(如占卜板指针)。我已将画布设置为黑色,指针为红色,我想在上面写白色文本,以便指针上有玩家的名字。

在 Android 2.3.4 中,它在红色指针顶部显示为纯白色文本,非常清晰,但我想使用任何颜色。在 Android 4.1.2 中,我几乎看不到白色文字。这是我的代码:

    public Pointer(Context context) {
    super(context);

    paintBg = new Paint();
    paintBg.setColor(Color.BLACK);
    paintName = new Paint();
    paintName.setColor(Color.WHITE);
    paintName.setTextSize(50); // set text size
    paintName.setStrokeWidth(5);
    paintName.setTextAlign(Paint.Align.CENTER);

    this.setImageResource(res); // pointer.png in res/drawable folder
    Drawable d = getResources().getDrawable(res);
    h = d.getIntrinsicHeight();
    w = d.getIntrinsicWidth();

    canvas = new Canvas();
    canvas.drawPaint(paintBg);//make background black

    // float imageScale = width / w; // how image size scales with screen
}


@Override
public void onDraw(Canvas canvas) {

    y = this.getHeight() / 2; // center of screen
    x = this.getWidth() / 2;
    int left = Math.round(x - 0.8f * w);
    int right = Math.round(x + 0.8f * w);

    canvas.save();
    canvas.rotate((direction + 180) % 360, x, y); // rotate to normal
    canvas.drawText(s, x, y + 20, paintName); // draw name

    canvas.restore();
    canvas.rotate(direction, x, y); // rotate back
    super.onDraw(canvas);
}

4.1.2 中的哪些变化会影响这一点,或者我做错了什么?感谢您对此的帮助,因为它让我发疯。

编辑以包括屏幕截图:

安卓 2.3.4
2.3.4 图像

安卓 4.1.2 4.1.2 图像

请注意白色文本在 2.3.4 中是如何出现在顶部的,而在 4.1.2 中它是如何出现在下方或浑浊的。

正如free3dom 指出的那样,它与alpha 有关。我确实更改了 alpha,因为如果我不更改,文本不会出现在箭头顶部。看起来具有指针图像的 ImageView 总是在顶部 - 这可能是怎么回事?以下是我处理设置 alpha 的方式:

    public static void setAlpha(View view, float alpha, int duration) {
if (Build.VERSION.SDK_INT < 11) {
    final AlphaAnimation animation = new AlphaAnimation(alpha, alpha);
    animation.setDuration(duration);
    animation.setFillAfter(true);
    view.startAnimation(animation);
 } else                          //for 11 and above
    view.setAlpha(alpha);
}

也许它与使用 this.setImageResource(res) 设置图像资源有关?根据 android developer guide,我只能将 alpha 设置为单个视图,并且视图中的所有内容都会更改。然而,如果我降低 alpha,箭头图像似乎变得足够透明,可以让我看到文本。

4

1 回答 1

0

您设置了笔画宽度,但从不指出应该将笔画用于Paint.

尝试添加

paintName.setStyle( FILL_AND_STROKE );

于 2013-09-08T20:55:07.663 回答