自定义动态跳动的三个加载小点

来源:互联网 发布:童装淘宝店简介怎么写 编辑:程序博客网 时间:2024/05/19 18:16

先说下背景把,最近sdk改版,中间有一个加载框,其实就是显示一串提示字,但是有时需要加载的时间比较长,用户又不能做其他操作,时间久了,感觉界面像卡死一样,不然,其实还在加载,体验非常不好,所以想在文字后边加三个加载的小点不停的跳动。

技术实现方式

  • 写三个字符串,把这段拼接到文字后边,开个handler 间隔一百毫秒换一个,也可以实现,但是有个问题,如果这样的每个页面都要写个handler 去设置它,并且在文字长度是自适应的情况下,会出现ui不停的跳动,因为他在不停的计算textview长度,显然体验不好。
  • 在TextView右边加个图片,需要三张,不停的设置,这样显然没必要啊,就为了这个加图片增加包的大小,不可取。
  • 在TextView后边再加一个textveiw,这个用来显示三个小点,这样也行,但是还是要在代码中去实现一些方法,太麻烦了,有没有更好的办法能,一劳永逸的,当然是有的。
    先说下思路,既然有多个地方调用,又怕麻烦,那就自己定义一个类,自定义一个类继承自TextView,这个自定义的textView专门用来显示三个动态的点,用的时候只要在xml中添加这个类就行了,是不是就比较方便了。废话不多说了,直接上代码。

1.先定义一个资源文件,用于显示字符串

<string name="text_number">%1$s</string>

2.自定义WaitingTextView 继承自TextView

public class WaitingTextView extends TextView {    int textNumber;    int number = 1;    String str;    public WaitingTextView(Context context) {        super(context);    }    public WaitingTextView(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);        textNumber = Utils.getResourceIdByName("R.string.text_number");        str=context.getString(textNumber);        new UpdateHandler().sendEmptyMessage(0);    }    class UpdateHandler extends Handler {        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            int tPosition = msg.what;            if (number % 3 == 1) {                setText(String.format(str, "."));                number++;            } else if (number % 3 == 2) {                setText(String.format(str, ".."));                number++;            } else {                setText(String.format(str, "..."));                number = 1;            }            this.sendEmptyMessageDelayed(tPosition, 400);        }    }}

提醒下Utils.getResourceIdByName(“R.string.text_number”);这句是通过包名获取资源id,主要跨平台用,具体方法就不贴出来了,如果不想用这个可以直接改成:

 context.getResources().getString(R.string.text_number);

到此,控件就定义完了,但是咋用呢,还是贴出来吧。只要把他放在需要显示的控件后边就行了,代码中完全不用写东东。

 < <你的包名>.WaitingTextView                android:id="@+id/toast_waiting"                android:layout_width="@dimen/dp_15"                android:layout_height="wrap_content"                android:maxLines="1"                android:textSize="@dimen/sp_12" />

当然,还有人说,这样也有点麻烦啊,我要写两个textview,第一个用来显示提示信息的textView 第二个才是显示动态点的,不是也很麻烦么。其实可以再简单一点的,根据自己业务来定,这种情况我们可以自定属性了也可以这样写,看代码:

 class UpdateHandler extends Handler {        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            int tPosition = msg.what;            if (number % 3 == 1) {                setText( getText() + String.format(str, "."));                number++;            } else if (number % 3 == 2) {                setText( getText() +String.format(str, ".."));                number++;            } else {                setText( getText() +String.format(str, "..."));                number = 1;            }            this.sendEmptyMessageDelayed(tPosition, 400);        }    }

这样你只要为自定义WaitingTextView 添加在xml中添加text属性就可以了,一个自定义view就搞定,但是如果你设置了宽度自适应又居中显示的化,注意了,ui会跳,因为长度在变化。

原创粉丝点击