Android 如何使用角度设置TextView颜色的渐变?

Android 如何使用角度设置TextView颜色的渐变?,android,textview,gradient,angle,textcolor,Android,Textview,Gradient,Angle,Textcolor,下面的代码将在textview上设置渐变(不是背景,而是文本本身)。但我需要改变这个梯度的角度,怎么做 Shader textShader = new LinearGradient(0, 0, 0, textView.getPaint().getTextSize(), new int[]{context.getResources().getColor(R.color.color1), context.getResources().getColor(R.color.color2)}

下面的代码将在textview上设置渐变(不是背景,而是文本本身)。但我需要改变这个梯度的角度,怎么做

Shader textShader = new LinearGradient(0, 0, 0, textView.getPaint().getTextSize(),
        new int[]{context.getResources().getColor(R.color.color1), context.getResources().getColor(R.color.color2)},
        new float[]{0, 1}, Shader.TileMode.CLAMP);
textView.getPaint().setShader(textShader);
提前谢谢。

根据这一点,我修改了您的代码。试试这个:

double angleInRadians=Math.torradians(45);
double length=textView.getPaint().getTextSize();
双端X=数学正弦(角半径)*长度;
双端Y=数学cos(角半径)*长度;
着色器textShader=新的LinearGradient(0,0,endX,endY,
新的int[]{context.getResources().getColor(R.color.color1),context.getResources().getColor(R.color.color2)},
新的float[]{0,1},Shader.TileMode.CLAMP);
textView.getPaint().setShader(textShader);

更新:
我惭愧地说,我的上述答案是不正确的。这些答案(和)是相同的,它们是好的,但它们几乎没有错误:
1。对于超过90度的agnles,它们不能正常工作。对于exmaple,如果以180度测试它们,则不会看到任何渐变,因为这些解决方案使用(0,0)点作为旋转中心。
2。如果文本视图的宽度或高度大于或小于其文本宽度或高度,则渐变无法正确渲染。例如,您可以通过一个文本视图对其进行测试,该文本视图具有
match_parent
width and height,居中
Hello
文本,或者启用了滚动小文本视图和长文本。
我开发了新的解决方案。它首先计算文本边界,然后围绕该边界的中心旋转渐变线

textView.post(新的Runnable(){
@凌驾
公开募捐{
final Rect textbund=新Rect(Integer.MAX\u值、Integer.MAX\u值、Integer.MIN\u值、Integer.MIN\u值);
最终布局=textView.getLayout();
对于(int i=0;itextbind.right)textbind.right=(int)right;
}
textbund.top=layout.getLineTop(0);
textbund.bottom=layout.getLineBottom(textView.getLineCount()-1);
if(textView.getIncludeFontPadding()){
Paint.FontMetrics FontMetrics=textView.getPaint().getFontMetrics();
textbund.top+=(fontMetrics.ascent-fontMetrics.top);
textbund.bottom-=(fontMetrics.bottom-fontMetrics.descent);
}
双角半径=数学半径(45);
double r=Math.sqrt(Math.pow(textbund.bottom-textbund.top,2)+
Math.pow(textbund.right-textbund.left,2))/2;
float centerX=textbund.left+(textbund.right-textbund.left)/2;
float centerY=textbund.top+(textbund.bottom-textbund.top)/2;
float startX=(float)Math.max(textbind.left,Math.min(textbind.right,centerX-r*Math.cos(angelinradians));
float startY=(float)Math.min(textbund.bottom,Math.max(textbund.top,centerY-r*Math.sin(angleInRadians));
float endX=(float)Math.max(textbind.left,Math.min(textbind.right,centerX+r*Math.cos(angelinradians));
float endY=(float)Math.min(textbund.bottom,Math.max(textbund.top,centerY+r*Math.sin(angleInRadians));
着色器textShader=新的LinearGradient(startX、startY、endX、endY、,
新建int[]{context.getResources().getColor(R.color.color1),
context.getResources().getColor(R.color.color2)},
新的float[]{0,1},Shader.TileMode.CLAMP);
textView.setTextColor(Color.WHITE);
textView.getPaint().setShader(textShader);
}
});
工作代码

final double angleInRadians = Math.toRadians(45);
final TextView textView = findViewById(R.id.app_name);
    textView.post(new Runnable() {
        @Override
        public void run() {
            double length = textView.getMeasuredWidth();

            double endX = Math.sin(angleInRadians) * length;
            double endY = Math.cos(angleInRadians) * length;

            Shader textShader = new LinearGradient(0, 0, (int) endX, (int) endY,
                    new int[]{Color.BLUE, Color.GREEN, Color.GREEN, Color.BLUE, Color.RED, Color.GREEN, Color.BLUE, Color.RED},
                    null, Shader.TileMode.CLAMP);
            textView.getPaint().setShader(textShader);
            textView.invalidate();
        }
    });
这将设置具有指定角度的渐变

final TextView myTextView = findViewById(R.id.my_text_view);

myTextView.post(new Runnable() 
    {

         @Override

            public void run() {

                int length = textView.getMeasuredWidth();

            float angle = 45; // specify angle in degrees here

            Shader textShader = new LinearGradient(0, 0, (int) (Math.sin(Math.PI * angle / 180) * length), 
                                    (int) (Math.cos(Math.PI * angle / 180) * length),

                                        new int[]{Color.BLUE, Color.GREEN, Color.GREEN, Color.BLUE, Color.RED, Color.GREEN, Color.BLUE, Color.RED},

    null, 
        Shader.TileMode.CLAMP);

        myTextView.getPaint().setShader(textShader);

            textView.invalidate();

            }

    });

渐变只出现在第一个字符上——其他字符是单色的。答案基于您的代码,我使用了
newfloat[]{0,1}
作为
位置
参数。可以使用null使其线性化。根据官方文档:“float:可能为空。颜色数组中每个对应颜色的相对位置[0..1]。如果为空,则颜色沿渐变线均匀分布。此值可能为空。”float[]不是问题。问题在于测量文本宽度,它给出的是文本高度而不是宽度——渐变只跨越第一个字符。试着提供一个关于你的解决方案如何工作的很好的描述。请参阅:。谢谢你为什么要使用
Runnable
并发布它?您可以在不启用Runnable的情况下进行更改。此外,您不需要使textview无效。如果不使用post,测量的宽度为零——在onCreateif use posr中使用它,则必须使textview无效,否则不会显示更改