Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android文本视图阴影不';半径设置为0时不工作_Android_Textview_Shadow - Fatal编程技术网

Android文本视图阴影不';半径设置为0时不工作

Android文本视图阴影不';半径设置为0时不工作,android,textview,shadow,Android,Textview,Shadow,我需要在标题上画一个阴影,没有任何模糊,如下图所示: 我的问题是,只要我将阴影半径设置为0,阴影就不再出现 下面是我正在使用的代码,在我的style.xml中 <style name="navigationTitleWithShadow"> <item name="android:textAllCaps">true</item> <item name="android:textColor">@color/navigationTit

我需要在标题上画一个阴影,没有任何模糊,如下图所示:

我的问题是,只要我将阴影半径设置为0,阴影就不再出现

下面是我正在使用的代码,在我的style.xml中

<style name="navigationTitleWithShadow">
    <item name="android:textAllCaps">true</item>
    <item name="android:textColor">@color/navigationTitleForegroundColor</item>
    <item name="android:shadowColor">@color/navigationTitleShadowColor</item>
    <item name="android:shadowDx">1</item>
    <item name="android:shadowDy">1</item>
    <item name="android:shadowRadius">0</item>
</style>

真的
@颜色/导航标题背景颜色
@颜色/导航标题ShadowColor
1.
1.
0
在布局中:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"

    android:text="@string/menuTableViewController_navigationTitle"
    android:textAppearance="@style/navigationTitleWithShadow" />


知道如何使用这个0阴影半径吗?

我找到了一个解决方案,将TextView子类化,覆盖方法onDraw()。这是我的密码:

@Override
protected void onDraw(Canvas canvas) {
    //super.onDraw(canvas);

    if ( getShadowColor() != 0 && getShadowRadius() == 0 ) {

        Paint paint = this.getPaint();
        paint.setColor(getShadowColor());

        paint.setTextAlign(Paint.Align.CENTER);

        int xPos = (canvas.getWidth() / 2);
        int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() +
                paint.ascent()) / 2));

        // Draw the shadow text first, so it appears below
        canvas.drawText(getText().toString().toUpperCase(), xPos + getShadowDx(), yPos + getShadowDy(), paint);

        paint.setColor(getCurrentTextColor());

        // Draw the main text on top of it
        canvas.drawText(getText().toString().toUpperCase(), xPos, yPos, paint);

    } else {
        super.onDraw(canvas);
    }

}

它工作得很好,虽然我不知道为什么我需要setTextAlign,但没有它,我的文本就错放在右边了。

我找到了一个解决方案,将TextView子类化,覆盖方法onDraw()。这是我的密码:

@Override
protected void onDraw(Canvas canvas) {
    //super.onDraw(canvas);

    if ( getShadowColor() != 0 && getShadowRadius() == 0 ) {

        Paint paint = this.getPaint();
        paint.setColor(getShadowColor());

        paint.setTextAlign(Paint.Align.CENTER);

        int xPos = (canvas.getWidth() / 2);
        int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() +
                paint.ascent()) / 2));

        // Draw the shadow text first, so it appears below
        canvas.drawText(getText().toString().toUpperCase(), xPos + getShadowDx(), yPos + getShadowDy(), paint);

        paint.setColor(getCurrentTextColor());

        // Draw the main text on top of it
        canvas.drawText(getText().toString().toUpperCase(), xPos, yPos, paint);

    } else {
        super.onDraw(canvas);
    }

}

它工作得很好,虽然我不知道为什么我需要setTextAlign,但没有它,我的文本就错放在右边了。

我不确定,但通过阅读文档,我看到android:shadowRadius必须是一个浮点值。我不知道是否可以将值设为0。也许试试0.0汉克斯!我尝试使用0.0、0.1、0.5,只要我的值小于1,就没有阴影。在我的情况下,
0.1f
对我有效(我正在将阴影应用于绘制对象)。我不确定,但通过阅读文档,我看到android:shadowRadius必须是浮点值。我不知道是否可以将值设为0。也许试试0.0汉克斯!我尝试使用0.0、0.1、0.5,只要我的值小于1,就没有阴影。在我的情况下,
0.1f
对我有效(我正在将阴影应用于绘制对象)。