1 前言
由于项目需求,需要显示如下的效果 查了下资料,可以使用TextSwitcher实现,废话不多说。直接上干货
2 定义attrs属性
对于这个滚动TextSwitchView,我们一般用得比较多的属性就是字体大小与颜色了。我们将它定义在attrs.xml文件中
<declare-styleable name="TextSwitchView">
<attr name="textSize" format="dimension"/>
<attr name="textColor" format="color"/>
</declare-styleable>
3 定义Translate Animation
我们想要达到的效果是上下滚动,因此,定义在anim下的动画如下:
in_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="2000"
android:fromYDelta="100%p"
android:toYDelta="0%p">
</translate>
</set>
可以看到,进入动画为:Y方向上的100%p到0%p(表示完全显示出来),那么相应的出去的动画为: out_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="2000"
android:fromYDelta="0%p"
android:toYDelta="-100%p">
</translate>
</set>
4 java 代码
/**
* Created by qiyei2015 on 2016/11/27.
* 1273482124@qq.com
*/
public class TextSwitchView extends TextSwitcher implements ViewSwitcher.ViewFactory {
private final static String TAG =
"TextSwitchView";
private Context mContext;
private Timer mTimer;
private int mIndex = -
1;
private List<String> mResource;
private float mTextSize;
private int mTextColor;
private final static int SCROLL =
1;
private Handler mHandler =
new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case SCROLL:
updateText();
break;
default:
break;
}
}
};
public TextSwitchView(Context context){
this(context,
null);
}
public TextSwitchView(Context context, AttributeSet attrs){
super(context,attrs);
init(context,attrs);
}
/**
* 初始化
* @param context
* @param attrs
*/
private void init(Context context,AttributeSet attrs){
mContext = context;
if (mTimer ==
null){
mTimer =
new Timer();
}
TypedArray array = mContext.obtainStyledAttributes(attrs,R.styleable.TextSwitchView);
mTextSize = array.getDimensionPixelSize(R.styleable.TextSwitchView_textSize,
16);
mTextColor = array.getColor(R.styleable.TextSwitchView_textColor, Color.BLACK);
array.recycle();
setFactory(
this);
setInAnimation(AnimationUtils.loadAnimation(context, R.anim.in_animation));
setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.out_animation));
}
@Override
public View
makeView() {
TextView textView =
new TextView(mContext);
final float fontScale = getResources().getDisplayMetrics().scaledDensity;
Log.d(TAG,
"mTextSize:" + (
int)(mTextSize / fontScale +
0.5f));
textView.setTextSize((
int)(mTextSize / fontScale +
0.5f));
textView.setTextColor(mTextColor);
return textView;
}
/**
* 设置资源
* @param res
*/
public void setResource(List<String> res){
mResource = res;
}
/**
* 设置文字停留时间
* @param time
*/
public void setTextStayTime(
long time){
if (mTimer ==
null){
mTimer =
new Timer();
}
else {
mTimer.scheduleAtFixedRate(
new TimerTask() {
@Override
public void run() {
mHandler.sendEmptyMessage(SCROLL);
}
},
0,time);
}
}
/**
* 更新Text
* @return
*/
private void updateText(){
mIndex++;
if (mIndex > mResource.size() -
1){
mIndex =
0;
}
setText(mResource.get(mIndex));
}
/**
* 这里防止内存泄漏
*/
@Override
protected void onDetachedFromWindow() {
mTimer.cancel();
mHandler.removeMessages(SCROLL);
super.onDetachedFromWindow();
}
}
比较简单,就不再分析了
转载请注明原文地址: https://ju.6miu.com/read-969446.html