步骤一
创立.XML文件(如下图1所示),并将添加图片
图 1
步骤二
将<selector修改成<animation-list
步骤三
在activity_main.xml的Image属性注释
// app:srcCompat="@drawable/door1"
MainActivity文件编程
ImageView img;
AnimationDrawable open_door;//两个.XML文件
AnimationDrawable close_door;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intiview();
}
private void intiview() {
open_door = (AnimationDrawable) getResources().getDrawable(R.drawable.opendoor);
close_door=(AnimationDrawable) getResources().getDrawable(R.drawable.closedoor);
img = findViewById(R.id.imageView);
}
public void btn_close(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
img.setBackground(close_door);
}
close_door.start();
}
public void btn_Open(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
img.setBackground(open_door);
}
open_door.start();
}
}