过渡可绘制对象不显示所有颜色
Transition drawable not displaying all colors
我正在尝试通过单击按钮来切换背景颜色。它显示初始颜色(蓝色),当我单击它时它会变成下一种颜色(红色)。当我再次单击时,它会变回蓝色并迅速转变为红色,而不是我的第三种颜色(白色)。我已经在 colors.xml 文件中定义了颜色,并制作了一个名为 transition.xml 文件的可绘制资源进行过渡。
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
A TransitionDrawable is a drawable object that can cross-fade between the two drawable resources.
我正在尝试通过单击按钮来切换背景颜色。它显示初始颜色(蓝色),当我单击它时它会变成下一种颜色(红色)。当我再次单击时,它会变回蓝色并迅速转变为红色,而不是我的第三种颜色(白色)。我已经在 colors.xml 文件中定义了颜色,并制作了一个名为 transition.xml 文件的可绘制资源进行过渡。
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
A TransitionDrawable is a drawable object that can cross-fade between the two drawable resources.