@SuppressLint(“SetTextI18n”)
@Override
public void onNext(WeatherBean weatherBean) {
//在主线程中处理得到的数据
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
###自定义View
布局中间展示未来15天天气,数据有日期、最高温度、最低温度、类型、类型图标,其中温度连成两条曲线,整体支持滑动。
我是这样设计的,温度曲线初始为两条直线,为这15天的平均值,然后开始变化,变到对应的值,从而形成曲线效果。
新建MyCurveView.java,继承自View。添加WeatherData内部类,添加对应的属性及get、set方法。
static class WeatherData {
private float lowTemp;
private float highTemp;
private int date;
private String type;
private Bitmap typeBitmap;
WeatherData(float lowTemp, float highTemp, int date, String type, Bitmap typeBitmap) {
this.lowTemp = lowTemp;
this.highTemp = highTemp;
this.date = date;
this.type = type;
this.typeBitmap = typeBitmap;
}
…
}
添加setProgress()方法,在网络请求完毕后,调用该方法更新数据和UI。首先调用arrayList保存网络数据,然后在动画中不断更新视图。
public void setProgress(int averageHigh, int averageLow, final int low, int top, ArrayList innerData) {
arrayList(innerData, top, low, averageHigh, averageLow);
ValueAnimator animatorHigh = ValueAnimator.ofInt(0, top);
animatorHigh.setDuration(1000);
animatorHigh.setInterpolator(new AccelerateInterpolator());
animatorHigh.addUpdateListener(valueAnimator -> {
mHighPercent = (int)valueAnimator.getAnimatedValue();
invalidate();
});
ValueAnimator animatorLow = ValueAnimator.ofInt(0, low);
animatorLow.setDuration(1000);
animatorLow.setInterpolator(new AccelerateInterpolator());
animatorLow.addUpdateListener(valueAnimator -> {
mLowPercent = (int)valueAnimator.getAnimatedValue();
});
AnimatorSet set = new AnimatorSet();
//两个动画同时进行
set.playTogether(animatorHigh, animatorLow);
//监听动画
set.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
//在做动画的时间内,通过该标志位禁止触摸动作
isAnimation = true;
}
@Override
public void onAnimationEnd(Animator animation) {
isAnimation = false;
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
set.start();
}
arrayList()方法,除了保存数据外,将温度做个转换,因为初始是从平均值开始变的