产品设计遇到了文本渐变和背景渐变,效果图
遇到问题:
- 渐变失效
- 渐变色太浅
解决:
- 没有默认颜色,渐变色就会很淡
- textview最好wrapcontent或者准确的dp值,如果设置成match_parent渐变会失效但是文本内容宽度只有一小部分那么渐变会失效
(因为是根据文本长度进行渐变的,如果宽度与实际不一致就会出现失效的问题) - 因为2所以外层一般需要包裹一层
工具类
public class TextViewUtils {
public static void setGradientForTextView(TextView textView) {
float endX = textView.getPaint().getTextSize() * textView.getText().length();
LinearGradient linearGradient = new LinearGradient(
0f, 0f, endX, 0f,
Color.parseColor("#FF63CEFF"),
Color.parseColor("#FF5E63FC"),
Shader.TileMode.CLAMP
);
textView.getPaint().setShader(linearGradient);
textView.invalidate();
}
}
部分布局代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="取消"
android:textColor="@color/C_999999"
android:textSize="18sp" />
<View
android:id="@+id/view_line"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/C_DDDDDD" />
<LinearLayout
android:id="@+id/ll_confirm"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<TextView
android:id="@+id/tv_confirm"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="center"
android:text="确定"
android:textColor="@color/C_5E6AFC"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
使用
TextViewUtils.setGradientForTextView(tvConfirm);
仅此记录,如果有更好的实现方法,还请留言指教