版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Hu_wenpeng/article/details/78223572
这是一个可以滚动文字的 view 内部有一个TextView 控制文字可以滚动:
自定义属性:
<!--垂直跑马灯样式-->
<declare-styleable name="MarqueeViewStyle">
<attr name="mvInterval" format="integer|reference" />
<attr name="mvAnimDuration" format="integer|reference" />
<attr name="mvTextSize" format="dimension|reference" />
<attr name="mvTextColor" format="color|reference" />
</declare-styleable>
文件源代码:
package ry.com.hxf_core.commonUtils.marqueeview;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.ViewFlipper;
import java.util.ArrayList;
import java.util.List;
import ry.com.hxf_core.R;
import ry.com.hxf_core.commonUtils.utils.DensityUtil;
/**
* Created by sunfusheng on 16/5/31.
*/
public class MarqueeView extends ViewFlipper {
private Context mContext;
private List<String> notices;
private boolean isSetAnimDuration = false;
private int interval = 2000;
private int animDuration = 500;
private int textSize = 14;
private int textColor = 0xffffffff;
public MarqueeView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
this.mContext = context;
if (notices == null) {
notices = new ArrayList<>();
}
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MarqueeViewStyle, defStyleAttr, 0);
interval = typedArray.getInteger(R.styleable.MarqueeViewStyle_mvInterval, interval);
isSetAnimDuration = typedArray.hasValue(R.styleable.MarqueeViewStyle_mvAnimDuration);
animDuration = typedArray.getInteger(R.styleable.MarqueeViewStyle_mvAnimDuration, animDuration);
if (typedArray.hasValue(R.styleable.MarqueeViewStyle_mvTextSize)) {
textSize = (int) typedArray.getDimension(R.styleable.MarqueeViewStyle_mvTextSize, textSize);
textSize = DensityUtil.px2sp(mContext, textSize);
}
textColor = typedArray.getColor(R.styleable.MarqueeViewStyle_mvTextColor, textColor);
typedArray.recycle();
setFlipInterval(interval);
Animation animIn = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_in);
if (isSetAnimDuration) animIn.setDuration(animDuration);
setInAnimation(animIn);
Animation animOut = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_out);
if (isSetAnimDuration) animOut.setDuration(animDuration);
setOutAnimation(animOut);
}
public void setTextColor( int color) {
this.textColor = color;
}
// 根据公告字符串启动轮播
public void startWithText(final String notice) {
if (TextUtils.isEmpty(notice)) return;
getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
getViewTreeObserver().removeGlobalOnLayoutListener(this);
startWithFixedWidth(notice, getWidth());
}
});
}
// 根据公告字符串列表启动轮播
public void startWithList(List<String> notices) {
setNotices(notices);
start();
}
// 根据宽度和公告字符串启动轮播
private void startWithFixedWidth(String notice, int width) {
int noticeLength = notice.length();
int dpW = DensityUtil.px2dip(mContext, width);
int limit = dpW/textSize;
if (dpW == 0) {
throw new RuntimeException("Please set MarqueeView width !");
}
if (noticeLength <= limit) {
notices.add(notice);
} else {
int size = noticeLength/limit + (noticeLength%limit != 0? 1:0);
for (int i=0; i<size; i++) {
int startIndex = i*limit;
int endIndex = ((i+1)*limit >= noticeLength? noticeLength:(i+1)*limit);
notices.add(notice.substring(startIndex, endIndex));
}
}
start();
}
// 启动轮播
public boolean start() {
if (notices == null || notices.size() == 0) return false;
removeAllViews();
for (String notice:notices) {
addView(createTextView(notice));
}
if (notices.size() > 1) {
startFlipping();
}
return true;
}
// 创建ViewFlipper下的TextView
private TextView createTextView(String text) {
TextView tv = new TextView(mContext);
tv.setSingleLine();
tv.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
tv.setText(text);
tv.setTextColor(textColor);
tv.setTextSize(textSize);
return tv;
}
public List<String> getNotices() {
return notices;
}
public void setNotices(List<String> notices) {
this.notices = notices;
}
}
在布局文件中使用的时候 已经引入了命名空间:
你发现在 mvTextColor 这个属性我们点不出来 遇见这种情况 只能手动 写出来了 ,不知道 朋友们有没有更好的办法 让自定义属性可以点出来 (可以留言告诉我一下啊),记着之前 自定义view引入命名空间后
<declare-styleable name="MarqueeViewStyle">
<attr name="mvInterval" format="integer|reference" />
<attr name="mvAnimDuration" format="integer|reference" />
<attr name="mvTextSize" format="dimension|reference" />
<attr name="mvTextColor" format="color|reference" />
</declare-styleable>
上面的所有属性都是可以点出来的。