分享

Image+.XML的方式,实现动画效果

 _知识库 2020-03-15

步骤一

创立.XML文件(如下图1所示),并将添加图片

图 1

步骤二

将<selector修改成<animation-list

<animation-list xmlns:android="http://schemas./apk/res/android" android:oneshot="true"><item android:duration="500" android:drawable="@drawable/door4"/>

  <item android:duration="500" android:drawable="@drawable/door3"/>
<item android:duration="500" android:drawable="@drawable/door2"/>
<item android:duration="500" android:drawable="@drawable/door1"/>

</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();

}
}

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多