Java 将图像添加为textview';文本背景
最近,我想对我的新项目做一些艺术化的事情,如标题所示,添加一个背景图像作为文本的背景,而不是周围典型的恒定颜色。具体来说,我有一个梯度图像的文本 像这样的 通常,我希望有一个xml可绘制的资源文件类型的解决方案,但任何简单的解决方案都非常受欢迎 编辑:Java 将图像添加为textview';文本背景,java,android,xml,Java,Android,Xml,最近,我想对我的新项目做一些艺术化的事情,如标题所示,添加一个背景图像作为文本的背景,而不是周围典型的恒定颜色。具体来说,我有一个梯度图像的文本 像这样的 通常,我希望有一个xml可绘制的资源文件类型的解决方案,但任何简单的解决方案都非常受欢迎 编辑: 感谢所有着色器解决方案,但为了回答问题,只有提供有关图像背景解决方案的答案才会被接受/给予奖励您可以将位图着色器应用于文本视图绘制 myTextView.getPaint().setShader(new BitmapShader(myBitma
感谢所有着色器解决方案,但为了回答问题,只有提供有关图像背景解决方案的答案才会被接受/给予奖励您可以将
位图着色器
应用于文本视图
绘制
myTextView.getPaint().setShader(new BitmapShader(myBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
如果不想使用图像,还有其他类型的着色器。文档是。是用于在绘制期间返回水平颜色跨度的对象的基类。着色器的子类安装在名为Paint.setShader(着色器)的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");
你应该打电话给适当的
你也可以跟随
您可以使用
yourTextView.getPaint().setShader(新的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)
及
着色器的文档:
Shaders.TileMode的文档:
另一种方式(与想法相同,未经测试):
将文本转换为位图和可绘制,然后在可绘制文件夹中使用创建gradient.xml。可以设置角度以及开始和结束颜色。将此可绘制设置为背景
<?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>
建议您使用相对布局,而不是使用着色器和上述答案建议的内容来加载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>
我没有试过,但是。对于线性渐变效果很好,谢谢。一定会考虑使用这个。你能指定你所说的“图像背景”吗?这与位图有何不同?File.jpeg>>图像背景
<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>