2.文字退出的动画...">
美文网首页Android-杂章
公告由下到上循环滚动的自定义view

公告由下到上循环滚动的自定义view

作者: yu_yue | 来源:发表于2018-07-17 17:47 被阅读11次

1.文字进入的动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="100%"
android:toYDelta="0" />
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>

2.文字退出的动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="-100%" />
<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>

3.自定义view

public class NoticeView extends ViewFlipper {

private Context mContext;
private int fontSize;
private int textColor;

public NoticeView(Context context) {
    super(context);
    mContext = context;
    initView();
}

public NoticeView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.NoticeView);
    //获取字体大小,默认大小是12sp
    fontSize = (int) ta.getDimension(R.styleable.NoticeView_text_font, 12);
    Log.i("fontSize","fontSize:"+fontSize);
    //获取文字颜色,默认颜色是BLUE
    textColor = ta.getColor(R.styleable.NoticeView_text_color, Color.GRAY);
    ta.recycle();
    initView();
}

private void initView() {
    setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.notice_slide_in_bottom));
    setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.notice_slide_out_top));
}
public void showNoticeMessages(List<String> messages, NoticeClickListener listener) {
    if (messages == null || messages.size() == 0) {
        return;
    }

    removeAllViews();
    startFlipping();
    setAutoStart(true);
    for (int i = 0;i <messages.size();i++) {
        TextView textView = new TextView(mContext);
        textView.setTextSize(fontSize);
        textView.setGravity(Gravity.CENTER_VERTICAL);
        textView.setText(messages.get(i));
        textView.setTag(i);
        textView.setTextColor(textColor);
        textView.setOnClickListener(listener);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        lp.gravity = Gravity.CENTER_VERTICAL;
        addView(textView,lp);
    }
}
public void showNoNotice(String message){
    if (message == null){
        return;
    }
    removeAllViews();
    stopFlipping();
    setAutoStart(false);
    TextView textView = new TextView(mContext);
    textView.setTextSize(fontSize);
    textView.setText(message);
    textView.setTextColor(textColor);
    textView.setGravity(Gravity.CENTER_VERTICAL);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    lp.gravity = Gravity.CENTER_VERTICAL;
    addView(textView,lp);
}
public interface NoticeClickListener extends View.OnClickListener {
    void onClick(View view);
}

public void release(){
    removeAllViews();
    stopFlipping();
}

}

相关文章

网友评论

    本文标题:公告由下到上循环滚动的自定义view

    本文链接:https://www.haomeiwen.com/subject/zeygpftx.html