简体   繁体   English

过渡绘图不显示所有 colors

[英]Transition drawable not displaying all colors

I am trying to switch the color of my background with the click of a button.我正在尝试通过单击按钮来切换背景颜色。 It shows the initial color(blue) and when I click it turns to the next color(red).它显示初始颜色(蓝色),当我单击它时,它会变成下一种颜色(红色)。 When I click again it goes back to blue and quickly transitions to red not my third color (white).当我再次单击时,它会变回蓝色并迅速转变为红色,而不是我的第三种颜色(白色)。 I have defined the colors in the colors.xml file and made a drawable resource called transition.xml file to transition. I have defined the colors in the colors.xml file and made a drawable resource called transition.xml file to transition.

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

    <item android:drawable="@color/blue_background"></item>
    <item android:drawable="@color/red_background"></item>
    <item android:drawable="@color/white"></item>
"

</transition>

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewGroup layout = (ViewGroup) findViewById(R.id.main_layout);
        TransitionDrawable transition = (TransitionDrawable) layout.getBackground();
        Button button = (Button) findViewById(R.id.StartTransitionButton);

        button.setOnClickListener(new View.OnClickListener() {
                                      @Override
                                      public void onClick(View v) {
                                          transition.startTransition(500);
                                          transition.startTransition(500);
                                          transition.startTransition(500);

                                      }
                                  }
        );
    }
}

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/transition"
    tools:context=".MainActivity">



    <LinearLayout
        android:id="@+id/main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/transition"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp"></LinearLayout>

TransitionDrawable supports only two layers TransitionDrawable 只支持两层

A TransitionDrawable is a drawable object that can cross-fade between the two drawable resources. TransitionDrawable 是可绘制的 object,可以在两个可绘制资源之间交叉淡入淡出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM