在大佬MarqueeViewLibrary框架的基础上实现控件的各项参数动态配置;
原文地址:
https://gitcode.net/mirrors/gongwen/MarqueeViewLibrary
实现后效果图:
配置参数 动态替换下面的值即可
Boolean isPlayAnim = true;//是否滚动
int width = 600;//控件宽度dp
int heigh = 80;//控件高度dp
int margin_left = 5;//左边距 dp
int margin_right = 0;//右边距 dp
int margin_top = 5;//上边距 dp
int margin_bottom = 0;//下边距 dp
String backgroundColor = "#cccccc";//控件背景色
int fontSize = 14;//字体大小dp
String textColor = "#000000";//字体颜色
int scrollSpeed = 4000;//翻页速度 毫秒
int scrollDirection = 4;//滚动方向:1上2下3左4右 其他默认左;
int animSpeed = 2000;//动画持续时间 毫秒
boolean isSingleLine = true;//是否单行
int textAlineType = 1;//文字居中方式:1居中,2横向居中3纵向居中 默认1
//设置数据源
final List<String> datas = Arrays.asList("《赋得古原草送别》", "离离原上草,一岁一枯荣。", "野火烧不尽,春风吹又生。", "远芳侵古道,晴翠接荒城。",
"又送王孙去,萋萋满别情。", "文字显示不下时,系统的处理方式(可选:none,start,middle,end)");
配置控制
//本地父布局
RelativeLayout rl_main_layout = findViewById(R.id.rl_main_layout);
int widthPx = MyUtils.px2dip(this, width);
int heighPx = MyUtils.px2dip(this, heigh);
SimpleMarqueeView marqueeView = new SimpleMarqueeView(this);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) rl_main_layout.getLayoutParams();
layoutParams.width = widthPx;
layoutParams.height = heighPx;
layoutParams.setMargins(MyUtils.dip2px(this, margin_left), MyUtils.dip2px(this, margin_top),
MyUtils.dip2px(this, margin_right), MyUtils.dip2px(this, margin_bottom));
marqueeView.setLayoutParams(layoutParams);//设置宽高及margin
rl_main_layout.setBackgroundColor(Color.parseColor(backgroundColor));//设置背景色
rl_main_layout.addView(marqueeView);
/**
* 设置动画
* SimpleMarqueeView marqueeView SimpleMarqueeView控件
* int scrollDirection 滚动方向
* int animSpeed 动画持续时间
* int width控件宽度
* */
setAnimation(marqueeView, scrollDirection, animSpeed, widthPx, heighPx);
marqueeView.setFlipInterval(scrollSpeed);//翻页时间间隔
marqueeView.setTextColor(Color.parseColor(textColor));//字体颜色
marqueeView.setTextSize(MyUtils.dip2px(this, fontSize));
marqueeView.setTextEllipsize(TextUtils.TruncateAt.END);//文字显示不下时,系统的处理方式
switch (textAlineType) {//1居中,2横向居中3纵向居中 默认1
case 1:
marqueeView.setTextGravity(Gravity.CENTER);
break;
case 2:
marqueeView.setTextGravity(Gravity.CENTER_HORIZONTAL);
break;
case 3:
marqueeView.setTextGravity(Gravity.CENTER_VERTICAL);
break;
default:
marqueeView.setTextGravity(Gravity.CENTER);
break;
}
marqueeView.setTextSingleLine(isSingleLine);//是否单行
SimpleMF<String> marqueeFactory = new SimpleMF(this);
marqueeFactory.setData(datas);
marqueeView.setMarqueeFactory(marqueeFactory);
if (isPlayAnim && datas != null || datas.size() > 0) {
marqueeView.startFlipping();
} else {
marqueeView.stopFlipping();
}
动画配置
private void setAnimation(SimpleMarqueeView marqueeView, int scrollDirection, int animSpeed, int width, int heigh) {
switch (scrollDirection) {
case 1://上
setBottomToTop(marqueeView, animSpeed, heigh);
break;
case 2://下
setTopToBottom(marqueeView, animSpeed, heigh);
break;
case 3://左
setRightToLeftAnim(marqueeView, animSpeed, width);
break;
case 4://右
setLeftToRoght(marqueeView, animSpeed, width);
break;
default://左
setRightToLeftAnim(marqueeView, animSpeed, width);
break;
}
}
//从下到上
private void setBottomToTop(SimpleMarqueeView marqueeView, int animSpeed, int heigth) {
AlphaAnimation alphaAnim = new AlphaAnimation(0f, 1f);
TranslateAnimation translateAnim = new TranslateAnimation(0, 0f, heigth, 0);
AnimationSet in_bottom = new AnimationSet(true);
in_bottom.addAnimation(alphaAnim);
in_bottom.addAnimation(translateAnim);
in_bottom.setDuration(animSpeed);
AlphaAnimation alphaAnim1 = new AlphaAnimation(1, 0f);
TranslateAnimation translateAnim1 = new TranslateAnimation(0f, 0, 0, -heigth);
AnimationSet out_top = new AnimationSet(true);
out_top.addAnimation(alphaAnim1);
out_top.addAnimation(translateAnim1);
out_top.setDuration(animSpeed);
marqueeView.setInAndOutAnim(in_bottom, out_top);
}
//从上到下
private void setTopToBottom(SimpleMarqueeView marqueeView, int animSpeed, int heigth) {
AlphaAnimation alphaAnim = new AlphaAnimation(0f, 1f);
TranslateAnimation translateAnim = new TranslateAnimation(0, 0f, -heigth, 0);
AnimationSet in_bottom = new AnimationSet(true);
in_bottom.addAnimation(alphaAnim);
in_bottom.addAnimation(translateAnim);
in_bottom.setDuration(animSpeed);
AlphaAnimation alphaAnim1 = new AlphaAnimation(1, 0f);
TranslateAnimation translateAnim1 = new TranslateAnimation(0f, 0, 0,heigth);
AnimationSet out_top = new AnimationSet(true);
out_top.addAnimation(alphaAnim1);
out_top.addAnimation(translateAnim1);
out_top.setDuration(animSpeed);
marqueeView.setInAndOutAnim(in_bottom, out_top);
}
//从右到左
private void setRightToLeftAnim(SimpleMarqueeView marqueeView, int animSpeed, int width) {
TranslateAnimation translateAnim = new TranslateAnimation(width, 0f, 0, 0);
AnimationSet in_right = new AnimationSet(true);
in_right.addAnimation(translateAnim);
in_right.setDuration(animSpeed);
TranslateAnimation translateAnim1 = new TranslateAnimation(0f, -width, 0, 0);
AnimationSet out_left = new AnimationSet(true);
out_left.setDuration(animSpeed);
out_left.addAnimation(translateAnim1);
marqueeView.setInAndOutAnim(in_right, out_left);
}
//从左到右
private void setLeftToRoght(SimpleMarqueeView marqueeView, int animSpeed, int width) {
TranslateAnimation translateAnim = new TranslateAnimation(-width, 0, 0, 0);
AnimationSet in_right = new AnimationSet(true);
in_right.addAnimation(translateAnim);
in_right.setDuration(animSpeed);
TranslateAnimation translateAnim1 = new TranslateAnimation(0, width, 0, 0);
AnimationSet out_left = new AnimationSet(true);
out_left.setDuration(animSpeed);
out_left.addAnimation(translateAnim1);
marqueeView.setInAndOutAnim(in_right, out_left);
}
网友评论