Android基于AdapterViewFlipper實(shí)現(xiàn)的圖片/文字輪播動畫控件

1. 問題/坑點(diǎn)
1.1 item寬高不生效問題
需要注意的是,AdapterViewFlipper 在布局時,寬高一定要用 match_parent 或者 具體dp值。
如果寬、高中使用了 wrap_content 時,會導(dǎo)致 AdapterViewFlipper 容器的寬高,最終變成第一個item的寬高。即使后續(xù)item的寬高超過第一個item,也不會生效,內(nèi)容顯示只會被限定在第一個的寬高范圍內(nèi)。
原理也很好理解,后續(xù)item沒有繪制出來時, wrap_content 計(jì)算出來的結(jié)果,就是第一個item的寬高。當(dāng)后續(xù) item 顯示的時候,沒有地方去重新更新父容器 AdapterViewFlipper 的寬高。
2. 常用方法
1.AdapterViewAnimator支持的XML屬性如下:
- android:animateFirstView:設(shè)置顯示組件的第一個View時是否使用動畫。
- android:inAnimation:設(shè)置組件顯示時使用的動畫。
- android:loopViews:設(shè)置循環(huán)到最后一個組件時是否自動跳轉(zhuǎn)到第一個組件。
- android:outAnimation:設(shè)置組件隱藏時使用的動畫。
2.輪播控制:
- startFlipping、stopFlipping : 開始、停止播放
- showPrevious、showNext:上一個、下一個
3.輪播狀態(tài)與參數(shù)
- isFlipping:是否輪播中
- flipInterval: 動畫間隔
4.設(shè)置入場、出場動畫:setInAnimation、setOutAnimation
3. 文字/圖片 輪播 Demo
/**
* 圖片/文字輪播
* 坑點(diǎn):text_flipper height 如果設(shè)置wrap_content 導(dǎo)致item寬度只會以第一個item的為準(zhǔn)
*/
class FlipperAnimActivity : AppCompatActivity(), View.OnClickListener {
private var textFlipper: AdapterViewFlipper? = null
private var imgFlipper: AdapterViewFlipper? = null
private var preBtn: Button? = null
private var nextBtn: Button? = null
private var autoBtn: Button? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_flipper_anim)
initTextFlipper()
initImgFlipper()
}
// 文字輪播
private fun initTextFlipper() {
textFlipper = findViewById(R.id.text_flipper)
val list = listOf("文字輪播測試0", "文字輪播測試02...")
textFlipper?.adapter = TextFlipperAdapter(this, list)
textFlipper?.setInAnimation(this, R.animator.text_flipper_in_from_bottom)
textFlipper?.setOutAnimation(this, R.animator.text_flipper_out_to_top)
// textFlipper?.flipInterval
// textFlipper?.startFlipping()
}
// 圖片輪播
private fun initImgFlipper() {
imgFlipper = findViewById(R.id.img_flipper)
val list = listOf("http://www.nicesoso.com/test/file/img/test.jpg", "http://www.nicesoso.com/test/file/img/test_h_1.jpg",
"http://www.nicesoso.com/test/file/img/test_h_2.jpg")
imgFlipper?.adapter = ImgFlipperAdapter(this, list)
imgFlipper?.setInAnimation(this, R.animator.img_flipper_in)
preBtn = findViewById(R.id.prev_btn)
nextBtn = findViewById(R.id.next_btn) as Button
autoBtn = findViewById(R.id.auto_btn) as Button
preBtn?.setOnClickListener(this)
nextBtn?.setOnClickListener(this)
autoBtn?.setOnClickListener(this)
}
override fun onClick(v: View?) {
when (v?.id) {
R.id.prev_btn -> {
imgFlipper?.showPrevious()
imgFlipper?.stopFlipping()
}
R.id.next_btn -> {
imgFlipper?.showNext()
imgFlipper?.stopFlipping()
}
R.id.auto_btn -> {
imgFlipper?.startFlipping()
}
}
}
override fun onDestroy() {
super.onDestroy()
textFlipper?.takeIf { it.isFlipping }?.stopFlipping()
imgFlipper?.takeIf { it.isFlipping }?.stopFlipping()
}
}
3.1 文字輪播:TextFlipperAdapter
class TextFlipperAdapter(private val context: Context, private val datas: List<String>) : BaseAdapter() {
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view = convertView ?: LayoutInflater.from(context).inflate(R.layout.item_flipper_text, parent, false)
val textView = view?.findViewById<TextView?>(R.id.text)
textView?.text = datas.get(position)
return view
}
override fun getItem(position: Int): Any {
return datas.get(position)
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getCount(): Int {
return datas.size
}
}
3.2 圖片輪播:ImgFlipperAdapter
class ImgFlipperAdapter(private val context: Context, private val datas: List<String>) : BaseAdapter() {
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view = convertView ?: ImageView(context)
(view as? ImageView)?.scaleType = ImageView.ScaleType.FIT_XY
view.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
(view as? ImageView)?.let { Glide.with(context).load(datas.get(position)).into(it) }
return view
}
override fun getItem(position: Int): Any {
return datas.get(position)
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getCount(): Int {
return datas.size
}
}
3.3 布局:activity_flipper_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@android:color/holo_red_light"
android:orientation="vertical">
<!--寬高要必須設(shè)置填充滿,否則wrap_content時,大小變成第一個item的大小-->
<AdapterViewFlipper
android:id="@+id/text_flipper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:autoStart="true"
android:flipInterval="2000" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<AdapterViewFlipper
android:id="@+id/img_flipper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:flipInterval="5000" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="圖片輪播測試(5s)"
android:textSize="24sp" />
<Button
android:id="@+id/prev_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="上一個" />
<Button
android:id="@+id/next_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="下一個" />
<Button
android:id="@+id/auto_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="自動播放" />
</RelativeLayout>
</LinearLayout>
文字item布局:item_flipper_text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@android:color/holo_blue_light">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:background="@android:color/holo_green_light"
android:gravity="center"
android:maxLines="1"
android:textSize="22sp" />
</LinearLayout>
3.4 動畫
文字輪播,入場動畫:res/animator/text_flipper_in_from_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:propertyName="y"
android:valueFrom="100"
android:valueTo="0"
android:valueType="floatType" />
文字輪播,出場動畫:res/animator/text_flipper_out_to_top.xml
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:propertyName="y"
android:valueFrom="0"
android:valueTo="-100"
android:valueType="floatType" />
圖片輪播,入場動畫:res/animator/img_flipper_in.xml
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:propertyName="x"
android:valueFrom="500"
android:valueTo="0"
android:valueType="floatType" />
參考
http://www.dbjr.com.cn/article/210687.htm
以上就是Android基于AdapterViewFlipper實(shí)現(xiàn)的圖片/文字輪播動畫控件的詳細(xì)內(nèi)容,更多關(guān)于Android 實(shí)現(xiàn)圖片/文字輪播動畫控件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android 應(yīng)用更換皮膚實(shí)現(xiàn)方法
本文主要介紹Android 應(yīng)用更換皮膚,Android應(yīng)用如果想更換皮膚這里幫大家整理了相關(guān)資料,有需要的小伙伴可以參考下2016-08-08
如何使用Mock修改Android設(shè)備上的features
這篇文章主要介紹了如何使用Mock修改Android設(shè)備上的features,想了解Mock的同學(xué)可以參考下2021-04-04
Android Webview上的ssl warning的處理方式詳解及實(shí)例
這篇文章主要介紹了Android Webview上的ssl warning的處理方式詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android RecyclerView的焦點(diǎn)記憶封裝
這篇文章主要介紹了Android RecyclerView的焦點(diǎn)記憶封裝,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
Android下通過httpClient發(fā)送GET和POST請求的實(shí)例代碼
這篇文章介紹了Android下通過httpClient發(fā)送GET和POST請求的實(shí)例代碼,有需要的朋友可以參考一下2013-08-08
Android開發(fā)中MJRefresh自定義刷新動畫效果
本文給大家介紹了MJRefresh自定義刷新動畫效果,包括常見用法等相關(guān)知識,非常不錯,具有參考借鑒價值,感興趣的朋友一起看看吧2016-11-11
微信小程序 實(shí)現(xiàn)列表刷新的實(shí)例詳解
這篇文章主要介紹了微信小程序 實(shí)現(xiàn)列表刷新的實(shí)例詳解的相關(guān)資料,這里提供了實(shí)現(xiàn)代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-11-11
Android關(guān)于Button背景或樣式失效問題解決方法
大家好,本篇文章主要講的是Android關(guān)于Button背景或樣式失效問題解決方法,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01

