仿小米天气预报未来几天趋势预报折线图

废话不多说,直接贴实现效果图和代码。。。。。。

效果图如下:
这里写图片描述
这里写图片描述

首先说一下我的实现思路,外部一个HorizontalScrollView,为scrollView添加一个LinearLayout子控件容器,遍历循环往LinearLayout中添加每天天气的layout,并为view设置每一天的数据。数据来源于https://www.nowapi.com/api/weather.future

下边贴代码:
首先是每天天气的layout布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">
    <TextView
        android:id="@+id/tv_horizontal_item_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="星期一\n2016-01-11"
        android:gravity="center_horizontal"/>
    <ImageView
        android:id="@+id/iv_horizontal_item_day_pic"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@mipmap/ic_launcher"
        />
    <TextView
        android:id="@+id/tv_horizontal_item_day_weather"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="晴"
        android:layout_marginBottom="6dp"/>
    <com.example.cyy.weather.widget.FutureWeatherTrendView
        android:id="@+id/view_horizontal_item_trend"
        android:layout_width="120dp"
        android:layout_height="120dp" />
    <ImageView
        android:id="@+id/iv_horizontal_item_night_pic"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@mipmap/ic_launcher"
        android:layout_marginTop="6dp"/>
    <TextView
        android:id="@+id/tv_horizontal_item_night_weather"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="晴"/>
    <TextView
        android:id="@+id/tv_horizontal_item_wind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="西南风\n3级"
        android:layout_marginTop="9dp"
        android:gravity="center_horizontal"/>

</LinearLayout>

预览图是这样的
这里写图片描述
中间那块儿是一个自定义的view,下边看看这个自定义view是如何实现的,为了让这个自定义view适合任何数据。大致思路是:
1.算出每一度所占的高度,就是这几天中有一个最高的温度和一个最低的温度,让这两个温度相减得到最大温度差,然后用这个view的高度(除去上下文本的高度)除以这个温度差即得到这个值;
2.为了与前一天后一天的温度相连起来,有必要知道与前一天温度的中间值和与后一天温度的中间值,这个值根据相似三角形原理算出分别是取当天与前一天温度中间值以及当天与后一天温度中间值
如图:
这里写图片描述
如果是第一天如下图(不必知道与上一天的中间值)
这里写图片描述

如果是最后一天如下图(不必知

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值