示例#1
0
  /** 初始化,查找ID,获取天气配置文件,设置容器属性 */
  private void init() {
    mWeatherContainer = new LinearLayout(context);
    // 利用工厂模式,获取天气业务对象,得到天气配置信息
    mWeatherEngine = BeanFactory.getImpl(WeatherEngine.class);
    config = mWeatherEngine.getWeatherConfig();

    String mAndroidIdStr = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
    mAndroidIdStr = mAndroidIdStr.replace("-", "").replace(" ", "").toUpperCase();
    mAndroidId = getLongAndroidId(mAndroidIdStr);
    Long num = Long.parseLong(mAndroidId);
    // 周列表,获取对应在数字,好直接从res/arrays获取不同国家的语言
    mWeekList = new ArrayList<String>();
    mWeekList.add("Sun");
    mWeekList.add("Mon");
    mWeekList.add("Tue");
    mWeekList.add("Wed");
    mWeekList.add("Thu");
    mWeekList.add("Fri");
    mWeekList.add("Sat");

    // 容器的大小及位置由用户指定
    RelativeLayout.LayoutParams params =
        new RelativeLayout.LayoutParams(
            DensityUtil.dip2px(context, config.getWidth()),
            DensityUtil.dip2px(context, config.getHeight()));
    mWeatherContainer.setLayoutParams(params);
    // mWeatherContainer.layout(config.getStartX(), config.getStartY(),
    // DensityUtil.px2dip(MainActivity.this, config.getWidth()),
    // DensityUtil.px2dip(MainActivity.this, config.getHeight()));
    mWeatherContainer.setBackgroundColor(Color.YELLOW);
  }
示例#2
0
        @Override
        public void handleMessage(Message msg) {
          if (msg.what == WEATHER_IS_READY) { // 利用handler发送延时消息,根据用户指定更新时间,更新天气
            if (mWeatherContainer.getChildCount() != 0) {
              mWeatherContainer.removeAllViews();
            }
            // String cityCode = config.getCityCode();
            String temperature = config.getShowType() > 0 ? "f" : "c";
            // showWeatherInfos("2161853", temperature);
            showWeatherInfos(temperature);
            handler.sendEmptyMessageDelayed(
                WEATHER_IS_READY, config.getUpdateFrequency() * 60 * 60);
          } else if (msg.what == GET_WEATHER_SUCCESS) {
            List<WeatherInfo> infos;
            SAXParserFactory factory = null;
            SAXParser saxParser = null;
            XMLReader xmlReader = null;
            GetYahooWeatherSaxTools tools = null;
            try {
              factory = SAXParserFactory.newInstance();
              saxParser = factory.newSAXParser();
              xmlReader = saxParser.getXMLReader();
              infos = new ArrayList<WeatherInfo>();
              tools = new GetYahooWeatherSaxTools(infos);

              xmlReader.setContentHandler(tools);
              xmlReader.parse(new InputSource(new StringReader((String) msg.obj)));
              // 获取当前是周几,用数字表示
              int currentDay = mWeekList.indexOf(infos.get(0).getDay());
              int forecastDay = 0;
              // 将得到的list天气数据,展示到屏幕上
              for (int i = 0; i < config.getForecastDays(); i++) {
                view = View.inflate(context, R.layout.weather_item, null);
                TextView tvDay = (TextView) view.findViewById(R.id.tv_day);
                TextView tvLow = (TextView) view.findViewById(R.id.tv_temperature_low);
                TextView tvHigh = (TextView) view.findViewById(R.id.tv_temperature_high);
                TextView tvWeather = (TextView) view.findViewById(R.id.tv_weather);
                ImageView ivWeatherIcon = (ImageView) view.findViewById(R.id.iv_weather_icon);
                forecastDay = currentDay + i;
                if (forecastDay >= 7) {
                  forecastDay = forecastDay % 7;
                }
                tvDay.setText(
                    context.getResources()
                        .getStringArray(R.array.weather_day_normalform)[forecastDay]);
                tvLow.setText(infos.get(i).getLow() + mShowTypeOfTemperature[config.getShowType()]);
                tvHigh.setText(
                    infos.get(i).getHigh() + mShowTypeOfTemperature[config.getShowType()]);
                tvWeather.setText(
                    context.getResources()
                        .getStringArray(R.array.weather_condition)[infos.get(i).getCode()]);
                ivWeatherIcon.setImageResource(R.drawable.w0);

                int width = config.getWidth() / config.getForecastDays();
                int height = config.getHeight();
                float wScale = width / 100f;
                float hScale = height / 100f;
                float squarScale = width * height / (100 * 100);

                LayoutParams viewChildParams = (LayoutParams) tvDay.getLayoutParams();
                viewChildParams.height *= hScale;
                viewChildParams.width *= wScale;
                tvDay.setLayoutParams(viewChildParams);

                viewChildParams = (LayoutParams) tvLow.getLayoutParams();
                viewChildParams.height *= hScale;
                viewChildParams.width *= wScale;
                tvLow.setLayoutParams(viewChildParams);
                tvHigh.setLayoutParams(viewChildParams);

                viewChildParams = (LayoutParams) tvWeather.getLayoutParams();
                viewChildParams.height *= hScale;
                viewChildParams.width *= wScale;
                tvWeather.setLayoutParams(viewChildParams);

                viewChildParams = (LayoutParams) ivWeatherIcon.getLayoutParams();
                viewChildParams.height *= hScale;
                viewChildParams.width *= wScale;
                ivWeatherIcon.setLayoutParams(viewChildParams);

                tvDay.setTextSize(DensityUtil.dip2px(context, 14 * squarScale));
                tvLow.setTextSize(DensityUtil.dip2px(context, 14 * squarScale));
                tvHigh.setTextSize(DensityUtil.dip2px(context, 14 * squarScale));
                tvWeather.setTextSize(DensityUtil.dip2px(context, 14 * squarScale));

                LinearLayout.LayoutParams viewParams =
                    new LinearLayout.LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                viewParams.weight = 1;

                mWeatherContainer.addView(view, viewParams);
              }
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
          super.handleMessage(msg);
        }