【问题标题】:Adding an image as textview's text background添加图像作为 textview 的文本背景
【发布时间】:2017-01-05 00:31:10
【问题描述】:

最近我想对我的新项目做一些艺术性的事情,就像标题所暗示的那样,添加背景图像作为文本的背景,而不是周围典型的恒定颜色。具体来说,我有这个文本的渐变图像。

类似的东西

通常我希望有一个 xml 可绘制资源文件类型的解决方案,但非常感谢任何简单的解决方案

编辑:

感谢所有着色器解决方案,但为了这个问题,只有提供有关图像背景的解决方案的答案才会被接受/给予赏金

【问题讨论】:

  • 非常适合线性渐变,谢谢。肯定会考虑使用这个。
  • 您能具体说明“图像背景”的含义吗?这和位图有什么不同?
  • File.jpeg >> 图片背景

标签: java android xml


【解决方案1】:

您可以将BitmapShader 应用于TextView 油漆。

myTextView.getPaint().setShader(new BitmapShader(myBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));

如果您不想使用图像,还有其他类型的着色器。文档是here

【讨论】:

    【解决方案2】:

    Shader 是在绘制期间返回颜色水平范围的对象的基类。 Shader 的子类安装在调用 paint.setShader(shader) 的 Paint 中。之后,使用该颜料绘制的任何对象(位图除外)都将从着色器中获取其颜色。

            Bitmap bitmapObj  = BitmapFactory.decodeResource(getResources(),R.drawable.your_image);
            Shader shaderObj = new BitmapShader(bitmapObj,Shader.TileMode.REPEAT,Shader.TileMode.REPEAT);
            textViewObj.getPaint().setShader(shaderObj);
            textViewObj.setText("hello");
    

    你应该打电话给正确的Shader.TileMode

    你也可以关注

    1. Gradient TextView
    2. Textview Gradient Text

    【讨论】:

      【解决方案3】:

      你可以使用yourTextView.getPaint().setShader(new LinearGradient(...));

      LinearGradient 有两个构造函数:

      /** Create a shader that draws a linear gradient along a line.
          @param x0           The x-coordinate for the start of the gradient line
          @param y0           The y-coordinate for the start of the gradient line
          @param x1           The x-coordinate for the end of the gradient line
          @param y1           The y-coordinate for the end of the gradient line
          @param  colors      The colors to be distributed along the gradient line
          @param  positions   May be null. The relative positions [0..1] of
                                      each corresponding color in the colors array. If this is null,
                                      the the colors are distributed evenly along the gradient line.
          @param  tile        The Shader tiling mode
      */
      
      public LinearGradient(float x0, float y0, float x1, float y1, int colors[], float positions[], TileMode tile)
      

      /** Create a shader that draws a linear gradient along a line.
          @param x0       The x-coordinate for the start of the gradient line
          @param y0       The y-coordinate for the start of the gradient line
          @param x1       The x-coordinate for the end of the gradient line
          @param y1       The y-coordinate for the end of the gradient line
          @param  color0  The color at the start of the gradient line.
          @param  color1  The color at the end of the gradient line.
          @param  tile    The Shader tiling mode
      */
      public LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1, TileMode tile)
      

      着色器文档:

      https://developer.android.com/reference/android/graphics/Shader.html

      Shaders.TileMode 文档:

      https://developer.android.com/reference/android/graphics/Shader.TileMode.html


      另一种方式(作为想法,未经测试):

      将文本转换为位图和可绘制,然后使用GradientDrawable

      【讨论】:

        【解决方案4】:

        在 drawable 文件夹中创建 gradient.xml。您可以设置角度以及开始和结束颜色。将此drawable设置为背景

           <?xml version="1.0" encoding="utf-8"?>
            <shape xmlns:android="http://schemas.android.com/apk/res/android"
             android:shape="rectangle">
             <gradient
            android:startColor="#FFFFFF"
            android:endColor="#00000000"
            android:angle="45"/>    
            </shape>
        

        【讨论】:

          【解决方案5】:

          建议您使用相对布局,而不是通过使用着色器和上述答案建议的东西来给 gpu / 系统带来负载。

          <RelativeLayout 
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >
          
              <ImageView
                  android:id="@+id/your_imageview_id"
                  android:layout_width="fill_parent"
                  android:layout_height="As_you_want_dp"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentRight="true"
                  android:scaleType="fitXY"
                  android:src="@drawable/ic_launcher" />
          
              <TextView
                  android:id="@+id/your_textview_id"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentTop="true"
                  android:layout_marginTop="20dp"
                  android:layout_centerHorizontal="true" />
          
          </RelativeLayout>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-07-29
            • 2015-07-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-06-21
            • 2012-01-25
            相关资源
            最近更新 更多