Android自定义View实现比赛时间闪动效果
本文实例为大家分享了Android实现比赛时间闪动效果的具体代码,供大家参考,具体内容如下
效果
代码
上代码
public class TwinkleTextView extends TextView implements Runnable { /** * 是否显示 */ private boolean flag = true; /** * 闪动内容 */ private String twinkleText = "'"; /** * 闪动时间 */ private int delayMillis = 500; private boolean run = false; public TwinkleTextView(Context context, AttributeSet attrs) { super(context, attrs); } public TwinkleTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public TwinkleTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } public void beginRun(){ run = true; run(); } @Override public void run() { if (flag){ setVisibility(VISIBLE); flag = false; }else{ setVisibility(INVISIBLE); flag = true; } postDelayed(this, delayMillis); } public String getTwinkleText() { return twinkleText; } public void setTwinkleText(String twinkleText) { this.twinkleText = twinkleText; setText(twinkleText); } public int getDelayMillis() { return delayMillis; } public void setDelayMillis(int delayMillis) { this.delayMillis = delayMillis; } public boolean isRun() { return run; } public void setRun(boolean run) { this.run = run; } }
使用注意事项
我的RecyclerView框架是BRVAH
public class MyAdapter extends BaseQuickAdapter<User, BaseViewHolder> { public MyAdapter(int layoutResId, @Nullable List<User> data) { super(layoutResId, data); } @Override protected void convert(BaseViewHolder helper, User item) { TextView textView = helper.getView(R.id.text); textView.setText(helper.getAdapterPosition() + ""); TwinkleTextView twinkleTextView = helper.getView(R.id.twinkle); twinkleTextView.setDelayMillis(500); if (!twinkleTextView.isRun()) { twinkleTextView.beginRun(); } } }
使用的时候一定要判断是否正在运行
if (!twinkleTextView.isRun()) { twinkleTextView.beginRun(); }
谢谢。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)