我有一个RelativeLayout
,我已经从drawable
中设置了它的背景。当选中RadioButton
时,我能够将RelativeLayout
的背景更改为另一个。但是,当它发生变化时,如何给它添加动画效果?
代码: activity_main.xml:
<RelativeLayout
android:layout_width="80dp"
android:layout_height="50dp"
android:gravity="center"
android:background="@drawable/original"
android:id="@+id/rel1">
<RadioButton
android:id="@+id/radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
original.xml(drawable):
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#ffffff">
</solid>
<corners
android:radius="50dp">
</corners>
</shape>
pressed.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#2196F3">
</solid>
<corners
android:radius="50dp">
</corners>
</shape>
Java类:
radio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b){
relativeLayout.setBackgroundResource(R.drawable.pressed);
}
}
});
ObjectAnimator
来完成它。 - Mohamed Fadel Buffon