旋转渐变而不是椭圆形 [英] Rotating the gradient not the oval

查看:102
本文介绍了旋转渐变而不是椭圆形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有扫描渐变的椭圆形,我想旋转渐变而不是椭圆本身(因为当我旋转椭圆时,它不再处于正确的位置).我对此一无所获.有任何想法吗?这就是我想要做的

I have an oval shape with a sweep gradient, I want to rotate the gradient not the oval itself (since when I rotate oval it is not in the right position anymore). I couldn't find anything about this. Any ideas? This is what I'm trying to do

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>

        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">
            <stroke
                android:width="1dp"
                android:color="#585858" />
            <rotate
                android:fromDegrees="0"
                android:pivotX="50%"
                android:pivotY="50%"
                android:toDegrees="40">
                <gradient
                    android:endColor="#FF7DD6"
                    android:startColor="#FFFFFF"
                    android:type="sweep"
                    android:useLevel="false" />
            </rotate>

        </shape>

    </item>

</layer-list>

推荐答案

如果使用的是SweepGradient,则可以使用setLocalMatrix方法:

If you are using SweepGradient there is setLocalMatrix method that could be used:

 float initial_rotation_angle_degrees = 30; 
 SweepGradient gradient = new SweepGradient(0,0
                        ,new int[]{
                        ,0xff0078be
                        ,0xfff4535c
                        }
                        ,null);
        Matrix matrix = new Matrix();
// Rotating the gradient 
        matrix.postRotate(initial_rotation_angle_degrees);
// Translating the gradient (The initial cx, cy values must be 0)
        matrix.postTranslate(cx,cy);
        gradient.setLocalMatrix(matrix);

这篇关于旋转渐变而不是椭圆形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆