一、效果图
二、实现方法
2.1 xml 实现方式
xml 文件 ``` <?xml version=”1.0” encoding=”utf-8”?>
<ImageView
android:id="@+id/iv_anim_fragment_two"
android:layout_width="230dp"
android:layout_height="270dp"
/>
- drawable 文件
<?xml version=”1.0” encoding=”utf-8”?>
<item android:drawable="@mipmap/p0" android:duration="50"/>
<item
android:drawable="@mipmap/p1"
android:duration="50" />
<item
android:drawable="@mipmap/p2"
android:duration="50" />
<item
android:drawable="@mipmap/p3"
android:duration="50" />
<item
android:drawable="@mipmap/p4"
android:duration="50" />
<item
android:drawable="@mipmap/p5"
android:duration="50" />
<item
android:drawable="@mipmap/p6"
android:duration="50" />
<item
android:drawable="@mipmap/p7"
android:duration="50" />
<item
android:drawable="@mipmap/p8"
android:duration="50" />
<item
android:drawable="@mipmap/p9"
android:duration="50" />
<item
android:drawable="@mipmap/p10"
android:duration="50" />
<item
android:drawable="@mipmap/p11"
android:duration="50" />
<item
android:drawable="@mipmap/p12"
android:duration="50" />
<item
android:drawable="@mipmap/p13"
android:duration="50" />
<item
android:drawable="@mipmap/p14"
android:duration="50" />
<item
android:drawable="@mipmap/p15"
android:duration="50" />
<item
android:drawable="@mipmap/p16"
android:duration="50" />
<item
android:drawable="@mipmap/p17"
android:duration="50" />
<item
android:drawable="@mipmap/p18"
android:duration="50" />
<item
android:drawable="@mipmap/p19"
android:duration="50" />
<item
android:drawable="@mipmap/p20"
android:duration="50" />
<item
android:drawable="@mipmap/p21"
android:duration="50" />
<item
android:drawable="@mipmap/p22"
android:duration="50" />
<item
android:drawable="@mipmap/p23"
android:duration="50" />
<item
android:drawable="@mipmap/p24"
android:duration="50" />
<item
android:drawable="@mipmap/p25"
android:duration="50" />
<item
android:drawable="@mipmap/p26"
android:duration="50" />
<item
android:drawable="@mipmap/p27"
android:duration="50" />
<item
android:drawable="@mipmap/p28"
android:duration="50" />
<item
android:drawable="@mipmap/p29"
android:duration="50" />
<item
android:drawable="@mipmap/p30"
android:duration="50" />
<item
android:drawable="@mipmap/p31"
android:duration="50" />
<item
android:drawable="@mipmap/p32"
android:duration="50" />
<item
android:drawable="@mipmap/p33"
android:duration="50" />
<item
android:drawable="@mipmap/p34"
android:duration="50" />
<item
android:drawable="@mipmap/p35"
android:duration="50" />
<item
android:drawable="@mipmap/p36"
android:duration="50" />
<item
android:drawable="@mipmap/p37"
android:duration="50" />
<item
android:drawable="@mipmap/p38"
android:duration="50" />
<item
android:drawable="@mipmap/p39"
android:duration="50" />
<item
android:drawable="@mipmap/p40"
android:duration="50" />
<item
android:drawable="@mipmap/p41"
android:duration="50" />
<item
android:drawable="@mipmap/p42"
android:duration="50" />
<item
android:drawable="@mipmap/p43"
android:duration="50" />
<item
android:drawable="@mipmap/p44"
android:duration="50" />
<item
android:drawable="@mipmap/p45"
android:duration="50" />
<item
android:drawable="@mipmap/p46"
android:duration="50" />
<item
android:drawable="@mipmap/p47"
android:duration="50" />
<item
android:drawable="@mipmap/p48"
android:duration="50" />
<item
android:drawable="@mipmap/p49"
android:duration="50" />
<item
android:drawable="@mipmap/p50"
android:duration="50" />
<item
android:drawable="@mipmap/p51"
android:duration="50" />
<item
android:drawable="@mipmap/p52"
android:duration="50" />
<item
android:drawable="@mipmap/p53"
android:duration="50" />
<item
android:drawable="@mipmap/p54"
android:duration="50" />
<item
android:drawable="@mipmap/p55"
android:duration="50" />
<item
android:drawable="@mipmap/p56"
android:duration="50" />
<item
android:drawable="@mipmap/p57"
android:duration="50" />
<item
android:drawable="@mipmap/p58"
android:duration="50" />
<item
android:drawable="@mipmap/p59"
android:duration="50" />
- activity 中调用
//xml设置实现方式 iv_anim_fragment_one.setImageResource(R.drawable.anim_list) val drawable: AnimationDrawable = iv_anim_fragment_one.drawable as AnimationDrawable btn_anim_fragment_xmlStart.setOnClickListener { drawable.start() }
btn_anim_fragment_xmlStop.setOnClickListener {
drawable.stop()
}
<a name="uGKXX"></a>
#### 2.2 java 代码实现
//代码实现方式 val animationDrawable = AnimationDrawable() for (i in 0..59){ val db = resources.getIdentifier(“p$i”,”mipmap”,packageName) val drawable2 = resources.getDrawable(db) animationDrawable.addFrame(drawable2,50) } animationDrawable.isOneShot = false iv_anim_fragment_two.setImageDrawable(animationDrawable)
btn_anim_fragment_codeStart.setOnClickListener {
animationDrawable.start()
}
btn_anim_fragment_codeStop.setOnClickListener {
animationDrawable.stop()
}
<a name="kojK7"></a>
#### 2.3 完整的 activity 代码
class AnimFragmentActivity: BaseActivity(R.layout.activity_anim_fragment) { override fun initData() {
}
override fun initEvent() {
//xml设置实现方式
iv_anim_fragment_one.setImageResource(R.drawable.anim_list)
val drawable: AnimationDrawable = iv_anim_fragment_one.drawable as AnimationDrawable
btn_anim_fragment_xmlStart.setOnClickListener {
drawable.start()
}
btn_anim_fragment_xmlStop.setOnClickListener {
drawable.stop()
}
//代码实现方式
val animationDrawable = AnimationDrawable()
for (i in 0..59){
val db = resources.getIdentifier("p$i","mipmap",packageName)
val drawable2 = resources.getDrawable(db)
animationDrawable.addFrame(drawable2,50)
}
animationDrawable.isOneShot = false
iv_anim_fragment_two.setImageDrawable(animationDrawable)
btn_anim_fragment_codeStart.setOnClickListener {
animationDrawable.start()
}
btn_anim_fragment_codeStop.setOnClickListener {
animationDrawable.stop()
}
}
override fun initInterface() {
}
override fun onReload() {
}