Android基于AdapterViewFlipper實現(xiàn)的圖片/文字輪播動畫控件
需要注意的是,AdapterViewFlipper 在布局時,寬高一定要用 match_parent 或者 具體dp值。
如果寬、高中使用了 wrap_content 時,會導致 AdapterViewFlipper 容器的寬高,最終變成第一個item的寬高。即使后續(xù)item的寬高超過第一個item,也不會生效,內(nèi)容顯示只會被限定在第一個的寬高范圍內(nèi)。
原理也很好理解,后續(xù)item沒有繪制出來時, wrap_content 計算出來的結(jié)果,就是第一個item的寬高。當后續(xù) item 顯示的時候,沒有地方去重新更新父容器 AdapterViewFlipper 的寬高。
2. 常用方法1.AdapterViewAnimator支持的XML屬性如下:
android:animateFirstView:設置顯示組件的第一個View時是否使用動畫。 android:inAnimation:設置組件顯示時使用的動畫。 android:loopViews:設置循環(huán)到最后一個組件時是否自動跳轉(zhuǎn)到第一個組件。 android:outAnimation:設置組件隱藏時使用的動畫。2.輪播控制:
startFlipping、stopFlipping : 開始、停止播放 showPrevious、showNext:上一個、下一個3.輪播狀態(tài)與參數(shù)
isFlipping:是否輪播中 flipInterval: 動畫間隔4.設置入場、出場動畫:setInAnimation、setOutAnimation
3. 文字/圖片 輪播 Demo/** * 圖片/文字輪播 * 坑點:text_flipper height 如果設置wrap_content 導致item寬度只會以第一個item的為準 */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 ButtonautoBtn = findViewById(R.id.auto_btn) as ButtonpreBtn?.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_XYview.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'> <RelativeLayoutandroid:layout_width='match_parent'android:layout_height='100dp'android:background='@android:color/holo_red_light'android:orientation='vertical'><!--寬高要必須設置填充滿,否則wrap_content時,大小變成第一個item的大小--><AdapterViewFlipper android: android:layout_width='match_parent' android:layout_height='match_parent' android:autoStart='true' android:flipInterval='2000' /> </RelativeLayout> <RelativeLayoutandroid:layout_width='match_parent'android:layout_height='0dp'android:layout_weight='1'><AdapterViewFlipper android: 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: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_alignParentLeft='true' android:layout_alignParentBottom='true' android:text='上一個' /><Button android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_alignParentBottom='true' android:layout_centerHorizontal='true' android:text='下一個' /><Button android: 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'> <TextViewandroid: 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' />參考
https://www.jb51.net/article/210687.htm
以上就是Android基于AdapterViewFlipper實現(xiàn)的圖片/文字輪播動畫控件的詳細內(nèi)容,更多關于Android 實現(xiàn)圖片/文字輪播動畫控件的資料請關注好吧啦網(wǎng)其它相關文章!
相關文章:
1. js select支持手動輸入功能實現(xiàn)代碼2. asp.net core項目授權(quán)流程詳解3. CSS3中Transition屬性詳解以及示例分享4. jsp文件下載功能實現(xiàn)代碼5. 開發(fā)效率翻倍的Web API使用技巧6. bootstrap select2 動態(tài)從后臺Ajax動態(tài)獲取數(shù)據(jù)的代碼7. vue使用moment如何將時間戳轉(zhuǎn)為標準日期時間格式8. html中的form不提交(排除)某些input 原創(chuàng)9. PHP橋接模式Bridge Pattern的優(yōu)點與實現(xiàn)過程10. ASP常用日期格式化函數(shù) FormatDate()
