Android - 文字阴影?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2486936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:03:16  来源:igfitidea点击:

Android - shadow on text?

android

提问by Grendizer

I am wondering how to add shadow on text in android?

我想知道如何在android中的文本上添加阴影?

I have the following code which is applied on a bitmap and I wanted to be shadowed...

我有以下代码应用于位图,我想被阴影......

paint.setColor(Color.BLACK);
paint.setTextSize(55);
paint.setFakeBoldText(false);
paint.setShadowLayer(1, 0, 0, Color.BLACK); //This only shadows my whole view...

回答by Jim Schubert

You should be able to add the style, like this (taken from source code for Ringdroid):

您应该能够添加样式,如下所示(取自 Ringdroid 的源代码):

  <style name="AudioFileInfoOverlayText">
    <item name="android:paddingLeft">4px</item>
    <item name="android:paddingBottom">4px</item>
    <item name="android:textColor">#ffffffff</item>
    <item name="android:textSize">12sp</item>
    <item name="android:shadowColor">#000000</item>
    <item name="android:shadowDx">1</item>
    <item name="android:shadowDy">1</item>
    <item name="android:shadowRadius">1</item>
  </style>

And in your layout, use the style like this:

在你的布局中,使用这样的样式:

 <TextView android:id="@+id/info"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       style="@style/AudioFileInfoOverlayText"
       android:gravity="center" />

Edit: the source code can be viewed here: https://github.com/google/ringdroid

编辑:源代码可以在这里查看:https: //github.com/google/ringdroid

Edit2: To set this style programmatically, you'd do something like this (modified from this exampleto match ringdroid's resources from above)

Edit2:要以编程方式设置此样式,您可以执行以下操作(从本示例修改以匹配上面的 ringdroid 资源)

TextView infoTextView = (TextView) findViewById(R.id.info);
infoTextView.setTextAppearance(getApplicationContext(),  
       R.style.AudioFileInfoOverlayText);

The signature for setTextAppearanceis

的签名setTextAppearance

public void setTextAppearance (Context context, int resid)

Since: API Level 1
Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource.

public void setTextAppearance(上下文上下文,int resid)

从:API 级别 1
设置来自指定 TextAppearance 资源的文本颜色、大小、样式、提示颜色和突出显示颜色。

回答by codeFood

You can do both in code and XML. Only 4 basic things to be set.

您可以在代码和 XML 中进行。只需要设置 4 个基本的东西。

  1. shadow color
  2. Shadow Dx - it specifies the X-axis offset of shadow. You can give -/+ values, where -Dx draws a shadow on the left of text and +Dx on the right
  3. shadow Dy - it specifies the Y-axis offset of shadow. -Dy specifies a shadow above the text and +Dy specifies below the text.
  4. shadow radius - specifies how much the shadow should be blurred at the edges. Provide a small value if shadow needs to be prominent. Else otherwise.
  1. 阴影颜色
  2. Shadow Dx - 它指定阴影的 X 轴偏移。您可以给出 -/+ 值,其中 -Dx 在文本左侧绘制阴影,在右侧绘制 +Dx
  3. shadow Dy - 它指定阴影的 Y 轴偏移。-Dy 指定文本上方的阴影,+Dy 指定文本下方的阴影。
  4. 阴影半径 - 指定阴影应在边缘模糊的程度。如果阴影需要突出,请提供一个小的值。否则否则。

e.g.

例如

    android:shadowColor="@color/text_shadow_color"
    android:shadowDx="-2"
    android:shadowDy="2"
    android:shadowRadius="0.01"

This draws a prominent shadow on left-lower side of text. In code, you can add something like this;

这会在文本的左下侧绘制一个突出的阴影。在代码中,你可以添加这样的东西;

    TextView item = new TextView(getApplicationContext());
    item.setText(R.string.text);
    item.setTextColor(getResources().getColor(R.color.general_text_color));
    item.setShadowLayer(0.01f, -2, 2,   getResources().getColor(R.color.text_shadow_color));

回答by Hoque MD Zahidul

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="20dp" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:shadowColor="#000"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="50"
        android:text="Text Shadow Example1"
        android:textColor="#FBFBFB"
        android:textSize="28dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Text Shadow Example2"
        android:textColor="#FBFBFB"
        android:textSize="28dp"
        android:textStyle="bold" />

</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="20dp" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:shadowColor="#000"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="50"
        android:text="Text Shadow Example1"
        android:textColor="#FBFBFB"
        android:textSize="28dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Text Shadow Example2"
        android:textColor="#FBFBFB"
        android:textSize="28dp"
        android:textStyle="bold" />

</LinearLayout>

In the above XML layout code, the textview1 is given with Shadow effect in the layout. below are the configuration items are

在上面的XML布局代码中,textview1在布局中被赋予了阴影效果。下面是配置项是

android:shadowDx– specifies the X-axis offset of shadow. You can give -/+ values, where -Dx draws a shadow on the left of text and +Dx on the right

android:shadowDx– 指定阴影的 X 轴偏移量。您可以给出 -/+ 值,其中 -Dx 在文本左侧绘制阴影,在右侧绘制 +Dx

android:shadowDy– it specifies the Y-axis offset of shadow. -Dy specifies a shadow above the text and +Dy specifies below the text.

android:shadowDy- 它指定阴影的 Y 轴偏移。-Dy 指定文本上方的阴影,+Dy 指定文本下方的阴影。

android:shadowRadius– specifies how much the shadow should be blurred at the edges. Provide a small value if shadow needs to be prominent. android:shadowColor – specifies the shadow color

android:shadowRadius– 指定阴影边缘的模糊程度。如果阴影需要突出,请提供一个小的值。android:shadowColor – 指定阴影颜色



Shadow Effect on Android TextView pragmatically

实用的 Android TextView 阴影效果

Use below code snippet to get the shadow effect on the second TextView pragmatically.

使用下面的代码片段以务实的方式在第二个 TextView 上获得阴影效果。

TextView textv = (TextView) findViewById(R.id.textview2);
textv.setShadowLayer(30, 0, 0, Color.RED);        
TextView textv = (TextView) findViewById(R.id.textview2);
textv.setShadowLayer(30, 0, 0, Color.RED);        

Output :

输出 :

enter image description here

在此处输入图片说明

回答by Roc Boronat

If you want to achieve a shadow like the one that Android does in the Launcher, we're managing these values. They're useful if you want to create TextViews that will appear as a Widget, without a background.

如果您想实现像 Android 在 Launcher 中所做的那样的阴影,我们正在管理这些值。如果您想创建将显示为 Widget 且没有背景的 TextViews,它们会很有用。

android:shadowColor="#94000000"
android:shadowDy="2"
android:shadowRadius="4"

回答by fhucho

Draw 2 texts: one gray (it will be the shadow) and on top of it draw the second text (y coordinate 1px more then shadow text).

绘制 2 个文本:一个灰色(它将是阴影)并在其顶部绘制第二个文本(y 坐标比阴影文本多 1px)。

回答by Dan Alboteanu

 <style name="WhiteTextWithShadow" parent="@android:style/TextAppearance">
    <item name="android:shadowDx">1</item>
    <item name="android:shadowDy">1</item>
    <item name="android:shadowRadius">1</item>
    <item name="android:shadowColor">@android:color/black</item>
    <item name="android:textColor">@android:color/white</item>
</style>

then use as

然后用作

 <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            tools:text="Today, May 21"
            style="@style/WhiteTextWithShadow"/>