开门见代码
正如文章标题而言,只是简单的封装一下自带的ViewFlipper,这里简单介绍一下轮子的用法。
import android.content.Context
import android.os.Build
import android.text.TextUtils
import android.util.AttributeSet
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.animation.AnimationSet
import android.view.animation.TranslateAnimation
import android.widget.TextView
import android.widget.ViewFlipper
import base.ActivityHandler
import util.ThemeUtils
/**
* # ******************************************************************
* # ClassName: MyViewFlipper
* # Description: ViewFlipper自定义view,便于设置不同数量的item
* # Author: kxqin
* # Version: Ver 1.0
* # Create Date 2020/6/2 14:41
* # ******************************************************************
*/
class MyViewFlipper : ViewFlipper {
/**
* 字体颜色,需考虑黑白主题
*/
var mColor: Int = ThemeUtils.getColor(R.color.txt_color_black_1, R.color.txt_color_white_1)
/**
* 字体大小
*/
var mTextSize: Float = 12f
/**
* 是否单行
*/
var maxLines: Int = 1
/**
* 默认截断风格
*/
var ellipsizeStyle : TextUtils.TruncateAt ?= null
constructor(context: Context) : super(context)
constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)
init {
//设置间隔时间,此时间应大于进出动画时间的总和
this.setFlipInterval(5000)
this.setInAnimation(InAnimation())
this.setOutAnimation(OutAnimation())
//启动
this.startFlipping()
}
fun setData(dataList: ArrayList<String>) {
if (dataList.size <= 0) return
ActivityHandler.getInstance {
for (content in dataList) {
val textView = TextView(context)
textView.maxLines = if (maxLines <= 0) 1 else maxLines
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
textView.setTextColor(context.getColor(mColor))
} else {
textView.setTextColor(context.resources.getColor(mColor))
}
textView.setTextSize(mTextSize)
textView.ellipsize = ellipsizeStyle
textView.setText(content)
addView(textView)
}
//当数据源只有一条时,停止轮播
if (dataList.size == 1) {
this.stopFlipping()
}
}.sendEmptyMessage(0)
}
companion object {
/**
* 入场动画
*/
fun InAnimation() : Animation {
//淡入淡出动画
val alphaAnimation = AlphaAnimation(0f, 1f)
//移动动画
val translateAnimation = TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0f, TranslateAnimation.RELATIVE_TO_SELF, 0f,
TranslateAnimation.RELATIVE_TO_SELF, 1f, TranslateAnimation.RELATIVE_TO_SELF, 0f)
//创建动画集合
val set = AnimationSet(false)
set.addAnimation(alphaAnimation)
set.addAnimation(translateAnimation)
set.duration = 500
return set
}
/**
* 退出动画
*/
fun OutAnimation() : Animation {
//淡入淡出动画
val alphaAnimation = AlphaAnimation(1f, 0f)
//移动动画
val translateAnimation = TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0f, TranslateAnimation.RELATIVE_TO_SELF, 0f,
TranslateAnimation.RELATIVE_TO_SELF, 0f, TranslateAnimation.RELATIVE_TO_SELF, -1f)
//创建动画集合
val set = AnimationSet(false)
set.addAnimation(alphaAnimation)
set.addAnimation(translateAnimation)
set.duration = 500
return set
}
}
}
注意事项
这种简单封装可不能称得上为“自定义view”!
1、字体颜色,工具类里也是通过判断系统是否为暗黑模式进行选择两种字体颜色,不适配的话就使用一种颜色即可。
2、轮播已在init时启用,为了防止二哈程序员们忘记startFlipping,没错,说的就是我自己0_0.
3、外界使用时直接放入xml中,获取view后setData即可,也可以增加点击事件之类的,直接都用view的点击监听即可。
4、动画,一定要注意两个进出动画时间总和小于轮播间隔,这个你想象一下应该明白为啥。