使用 TransitionDrawable 时应用崩溃
App crashes when using TransitionDrawable
我想在单击按钮时转换背景颜色,但按下按钮后我的应用程序似乎崩溃了。该按钮运行 TransitionDrawable 代码。我已将我的背景设置为 drawable 文件夹,其中包含 transition.Could 谁能帮助我?非常感谢!
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void colourChangeButton(View view) {
final TransitionDrawable transition = (TransitionDrawable) view.getBackground();
transition.startTransition(1000);
}
}
这是我定义颜色的可绘制文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#2575fc"></color>
</item>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#ff0844"></color>
</item>
</selector>
这是我的转换可绘制文件:
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/blue_background"></item>
<item android:drawable="@drawable/pink_background"></item>
</transition>
你快到了!
您需要将过渡可绘制对象设置为视图的背景。
假设您的 activity 的布局 xml 被称为 activity_main.xml
而您的
转换可绘制文件称为 transition.xml
.
在您的 activity_main.xml
的根布局中:
<LinearLayout
...
id="@+id/main_layout"
background="@drawable/transition">
<Button
id="@+id/start_transition_button"
... />
...
在MainActivity.java
中:
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.start_transition_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
transition.startTransition(500);
}
}
}
}
这是一个很好的post:
https://proandroiddev.com/transitiondrawable-small-gems-of-the-android-framework-4dcdd3c83319
我想在单击按钮时转换背景颜色,但按下按钮后我的应用程序似乎崩溃了。该按钮运行 TransitionDrawable 代码。我已将我的背景设置为 drawable 文件夹,其中包含 transition.Could 谁能帮助我?非常感谢!
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void colourChangeButton(View view) {
final TransitionDrawable transition = (TransitionDrawable) view.getBackground();
transition.startTransition(1000);
}
}
这是我定义颜色的可绘制文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#2575fc"></color>
</item>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#ff0844"></color>
</item>
</selector>
这是我的转换可绘制文件:
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/blue_background"></item>
<item android:drawable="@drawable/pink_background"></item>
</transition>
你快到了!
您需要将过渡可绘制对象设置为视图的背景。
假设您的 activity 的布局 xml 被称为 activity_main.xml
而您的
转换可绘制文件称为 transition.xml
.
在您的 activity_main.xml
的根布局中:
<LinearLayout
...
id="@+id/main_layout"
background="@drawable/transition">
<Button
id="@+id/start_transition_button"
... />
...
在MainActivity.java
中:
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.start_transition_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
transition.startTransition(500);
}
}
}
}
这是一个很好的post: https://proandroiddev.com/transitiondrawable-small-gems-of-the-android-framework-4dcdd3c83319