Java Android背景颜色从左到右的过渡

Java Android背景颜色从左到右的过渡,java,android,xml,Java,Android,Xml,我想知道是否有一种方法可以使背景从一个相对位置动画化,使颜色在大约1秒内从蓝色变为黄色,从左到右。(此动画应在单击时触发)。 谢谢你的帮助, ty我认为你可以使用,最基本的方法是有三种颜色:蓝色、渐变蓝黄色和黄色: 在res/drawable中创建gradient.xml文件(从左到右): 我不确定这段代码是否给了你你所期望的,但是如果你有一些想象力,你应该得到你想要的 希望这有帮助。可绘制的gradient\u 2 <?xml version="1.0" encoding="utf-8"

我想知道是否有一种方法可以使背景从一个相对位置动画化,使颜色在大约1秒内从蓝色变为黄色,从左到右。(此动画应在单击时触发)。 谢谢你的帮助, ty

我认为你可以使用,最基本的方法是有三种颜色:蓝色、渐变蓝黄色和黄色:

在res/drawable中创建gradient.xml文件(从左到右):

我不确定这段代码是否给了你你所期望的,但是如果你有一些想象力,你应该得到你想要的


希望这有帮助。

可绘制的
gradient\u 2

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="@color/deep_purple_500"
        android:endColor="@color/blue_700"
        android:angle="0"/>
</shape>
您可以更改颜色满0、45、90、180的agle

android:angle="0"
您还可以添加
centerColor

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

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout_id);
AnimationDrawable animationDrawable = (AnimationDrawable) relativeLayout.getBackground();

mybutton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
            animationDrawable.start();
    }
});
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="@color/deep_purple_500"
        android:endColor="@color/blue_700"
        android:angle="0"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient_2"
    android:orientation="vertical">
</LinearLayout>
android:angle="0"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="0"
        android:centerColor="#D8EDFD"
        android:endColor="#D8EDFD "
        android:startColor="#F6F9FF " />
</shape>