项目中使用到了轮播广告。
效果图如下:(现在是前端写死的数据,随机手机尾号,随机金额)
布局文件:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_below="@+id/bga_banner"
android:layout_centerInParent="true">
<View
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="center_vertical"
android:background="@mipmap/notice_icon" />
<com.ps.sevenbannermaster.widget.NoticeView
android:id="@+id/view_notice"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
app:nvInterval="6000"
app:nvTextColor="@color/color_484848"
app:nvTextGravity="center">
</com.ps.sevenbannermaster.widget.NoticeView>
</LinearLayout>
然后在页面启动的时候,调用如下方法即可:
/**
* 启动广告
*/
private void startNotice(NoticeView noticeView){
noticeView.start(Arrays.asList(notices));
}
自定义的NoticeView.java如下:
/**
* Created : 2018/7/30 16:16
* Description :自定义广告播报
* Author : gengbaolong
*/
public class NoticeView extends TextSwitcher {
private Animation mInUp = anim(1.5f, 0);
private Animation mOutUp = anim(0, -1.5f);
private List<String> mDataList = new ArrayList<>();
private int mIndex = 0;
private int mInterval = 4000;
private int mDuration = 900;
private Drawable mIcon;
private int mIconTint = 0xff999999;
private int mIconPadding = 0;
private int mPaddingLeft = 0;
private boolean mIsVisible = false;
private boolean mIsStarted = false;
private boolean mIsResumed = true;
private boolean mIsRunning = false;
private final TextFactory mDefaultFactory = new TextFactory();
private final Runnable mRunnable = new Runnable() {
@Override
public void run() {
if (mIsRunning) {
show(mIndex + 1);
postDelayed(mRunnable, mInterval);
}
}
};
public static String[] price = new String[]{
"1000",
"1200",
"1500",
"1800",
"2000",
};
public NoticeView(Context context) {
this(context, null);
}
public NoticeView(Context context, AttributeSet attrs) {
super(context, attrs);
initWithContext(context, attrs);
setInAnimation(mInUp);
setOutAnimation(mOutUp);
setFactory(mDefaultFactory);
mInUp.setDuration(mDuration);
mOutUp.setDuration(mDuration);
}
private void initWithContext(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NoticeView);
mIcon = a.getDrawable(R.styleable.NoticeView_nvIcon);
mIconPadding = (int)a.getDimension(R.styleable.NoticeView_nvIconPadding, 0);
boolean hasIconTint = a.hasValue(R.styleable.NoticeView_nvIconTint);
if (hasIconTint) {
mIconTint = a.getColor(R.styleable.NoticeView_nvIconTint, 0xff999999);
}
mInterval = a.getInteger(R.styleable.NoticeView_nvInterval, 4000);
mDuration = a.getInteger(R.styleable.NoticeView_nvDuration, 900);
mDefaultFactory.resolve(a);
a.recycle();
if (mIcon != null) {
mPaddingLeft = getPaddingLeft();
int realPaddingLeft = mPaddingLeft + mIconPadding + mIcon.getIntrinsicWidth();
setPadding(realPaddingLeft, getPaddingTop(), getPaddingRight(), getPaddingBottom());
if (hasIconTint) {
mIcon = mIcon.mutate();
DrawableCompat.setTint(mIcon, mIconTint);
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mIcon != null) {
int y = (getMeasuredHeight() - mIcon.getIntrinsicWidth()) / 2;
mIcon.setBounds(mPaddingLeft, y, mPaddingLeft + mIcon.getIntrinsicWidth(), y + mIcon.getIntrinsicHeight());
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mIcon != null) {
mIcon.draw(canvas);
}
}
public int getIndex() {
return mIndex;
}
public void start(List<String> list) {
mDataList = list;
mIsVisible = true;
if (mDataList == null || mDataList.size() < 1) {
mIsStarted = false;
update();
} else {
mIsStarted = true;
update();
show(0);
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
// mIsVisible = false;
// update();
}
@Override
protected void onWindowVisibilityChanged(int visibility) {
super.onWindowVisibilityChanged(visibility);
// mIsVisible = visibility == VISIBLE;
// update();
}
/**
* 是否可見
* @param onResume
*/
public void updateResume(boolean onResume){
mIsVisible = onResume;
update();
}
private void update() {
boolean running = mIsVisible && mIsResumed && mIsStarted;
if (running != mIsRunning) {
if (running) {
postDelayed(mRunnable, mInterval);
} else {
removeCallbacks(mRunnable);
Log.e("ezy", "update() ");
}
mIsRunning = running;
}
Log.e("ezy", "update() visible=" + mIsVisible + ", started=" + mIsStarted + ", running=" + mIsRunning);
}
private void show(int index) {
mIndex = index % mDataList.size();
Random random = new Random();
StringBuilder showMsg = new StringBuilder("恭喜 手机<font color=#FB9423>尾号");
showMsg.append(randomPhone());
showMsg.append("</font>用户成功放款<font color=#FB9423>");
showMsg.append(price[random.nextInt(5)]);
showMsg.append("</font>元!");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
setText(Html.fromHtml(showMsg.toString(),Html.FROM_HTML_MODE_LEGACY));
} else {
setText(Html.fromHtml(showMsg.toString()));
}
}
private Animation anim(float from, float to) {
final TranslateAnimation anim = new TranslateAnimation(0, 0f, 0, 0f, Animation.RELATIVE_TO_PARENT, from, Animation.RELATIVE_TO_PARENT, to);
anim.setDuration(mDuration);
anim.setFillAfter(false);
anim.setInterpolator(new LinearInterpolator());
return anim;
}
class TextFactory implements ViewFactory {
DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
float size = dp2px(14);
int color = 1;
int lines = 1;
int gravity = Gravity.LEFT;
void resolve(TypedArray ta) {
lines = ta.getInteger(R.styleable.NoticeView_nvTextMaxLines, lines);
size = ta.getDimension(R.styleable.NoticeView_nvTextSize, size);
color = ta.getColor(R.styleable.NoticeView_nvTextColor, color);
gravity = ta.getInteger(R.styleable.NoticeView_nvTextGravity, gravity);
}
private int dp2px(float dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, dm);
}
@Override
public View makeView() {
TextView tv = new TextView(getContext());
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
tv.setMaxLines(lines);
if (color != 1) {
tv.setTextColor(color);
}
tv.setEllipsize(TextUtils.TruncateAt.END);
tv.setGravity(Gravity.CENTER_VERTICAL | gravity);
tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return tv;
}
}
/**
* 随机手机号码
* @return
*/
public String randomPhone(){
Random random = new Random();
Set<Integer> set = new HashSet<Integer>();
while (set.size() < 4)
{
int randomInt = random.nextInt(10);
set.add(randomInt);
}
StringBuffer phoneNum = new StringBuffer();
for (Integer i : set)
{
phoneNum.append("" + i);
}
return phoneNum.toString();
}
}