本篇文章将共享两个VIEW组件,一个天气组件和一个日期组件,这两个组件本来是一个AppWidget后来,我看著冷笑话,将他们盖住一个VIEW的组件,可以像用于WindowsPhone7的用户控件一样扯放在你想的项目中。本篇将展示这两个组件的撰写过程,工程文件如下: 包名讲解: com.terry.weather 程序的入口包在 com.yaomei.adapter 天气预报组件用于到的数据源 com.yaomei.model 天气预报用于到的模型包在 com.yaomei.util 提供天气信息的工具包 com.yaomei.widget 天气预报组件、日期组件的存放在方位 从包名可以显现出,撰写一个天气预报所必须的代码量比撰写一个日期VIEW所必须的代码量要非常少,那么我们再行把天气预报的一些构建思路跟大家讲讲。 首先,本实例用于的天气预报是一个可以自己国际化的天气组件VIEW,可以看右图,将所必须的URL都放进ANDROID自己的国际化文件夹里面,比如中文的话就这样写出: 《stringname=googleWeatherApi》 《![CDATA[.yaomei.widget; importjava.io.ByteArrayInputStream; importjava.io.ByteArrayOutputStream; importjava.util.ArrayList; importjava.util.Calendar; importjava.util.List; importjava.util.Timer; importjava.util.TimerTask; importorg.apache.commons.codec.binary.Base64; importandroid.app.Activity; importandroid.content.Context; importandroid.content.SharedPreferences; importandroid.content.res.TypedArray; importandroid.graphics.Bitmap.CompressFormat; importandroid.graphics.drawable.BitmapDrawable; importandroid.graphics.drawable.Drawable; importandroid.os.Handler; importandroid.os.Message; importandroid.text.Html; importandroid.util.AttributeSet; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.widget.GridView; importandroid.widget.ImageView; importandroid.widget.LinearLayout; importandroid.widget.TextView; importandroid.widget.Toast; importcom.terry.weather.R; importcom.yaomei.adapter.weatherAdapter; importcom.yaomei.model.WeatherMdoel; importcom.yaomei.util.strHelpeUtil; publicclassWeatherViewextendsLinearLayout{ privatestaticfinalStringHour_COMPARE=hour_compare; privatestaticfinalStringDAY_OF_WEEK=day_of_week; privatestaticfinalStringLOW=low; privatestaticfinalStringHIGH=high; privatestaticfinalStringCONDITION=condition; privatestaticfinalStringIMAGE=image; privatestaticfinalStringDATE_COMPARE=date_compare; privatestaticfinalStringCITYNAE_SHARE=cityNameShare; privateImageViewiv_weather; privateTextViewtv_state,tv_position,tv; WeatherMdoelmodel; privateList《WeatherMdoel》weatherList=null; GridViewgv; Timertimer; Handlerhandler=newHandler(){ publicvoidhandleMessage(Messagemsg){ if(msg.arg1==1){ if(weatherList.size()》0){ gv .setAdapter(newweatherAdapter(getContext(), weatherList)); init(); }else{ Toast.makeText(getContext(),查找将近数据,1000).show(); } //msg.recycle(); } }; }; /** *自动读取天气 */ privatebooleanautoLoad=false; publicbooleangetAutoLoad(){ returnautoLoad; } publicvoidsetAutoLoad(booleanisLoad){ this.autoLoad=isLoad; } /** *城市名称 */ privateStringcityName=; publicStringgetCityName(){ returncityName; } publicvoidsetCityName(StringcityName){ this.cityName=cityName; } /** *设置每几小时改版一次 */ privateintupdateHour; publicintgetUpdateHour(){ returnupdateHour; } publicvoidsetUpdateHour(inthour){ this.updateHour=hour; } publicWeatherView(Contextcontext){ this(context,null); //TODOAuto-generatedconstructorstub } publicWeatherView(Contextcontext,AttributeSetattrs){ super(context,attrs); intresouceID=-1; TypedArraytyedArray=context.obtainStyledAttributes(attrs, R.styleable.WeatherView); intN=tyedArray.getIndexCount(); for(inti=0;i《N;i++){ intattr=tyedArray.getIndex(i); switch(attr){ caseR.styleable.WeatherView_AutoLoad: setAutoLoad(tyedArray.getBoolean( R.styleable.WeatherView_AutoLoad,false)); break; caseR.styleable.WeatherView_CityName: resouceID=tyedArray.getResourceId( R.styleable.WeatherView_CityName,0); setCityName(resouceID》0?tyedArray.getResources().getText( resouceID).toString():tyedArray .getString(R.styleable.WeatherView_CityName)); break; caseR.styleable.WeatherView_UpdateHour: setUpdateHour(tyedArray.getInteger( R.styleable.WeatherView_UpdateHour,3)); break; } } Viewview=LayoutInflater.from(getContext()).inflate( R.layout.weather_layout,this); tv=(TextView)view.findViewById(R.id.tv_temperature); gv=(GridView)view.findViewById(R.id.grid); iv_weather=(ImageView)view.findViewById(R.id.iv_weather); tv_state=(TextView)view.findViewById(R.id.tv_state); tv_position=(TextView)view.findViewById(R.id.tv_position); timer=newTimer(); if(getAutoLoad()){ startLoadWeather(); } tyedArray.recycle(); } /** *开始读取 */ publicvoidstartLoadWeather(){ timer.schedule(newTimerTask(){ @Override publicvoidrun(){ SharedPreferencesshare=getContext().getSharedPreferences( weather,Activity.MODE_PRIVATE); longtime=System.currentTimeMillis(); finalCalendarmCalendar=Calendar.getInstance(); mCalendar.setTimeInMillis(time); StringtempDate=mCalendar.get(Calendar.YEAR)+- +mCalendar.get(Calendar.MONTH)+- +mCalendar.get(Calendar.DAY_OF_MONTH); if(share.contains(DATE_COMPARE)){ if(share.getString(CITYNAE_SHARE,).equals(cityName)){ inttime_cop=mCalendar.get(Calendar.HOUR) -share.getInt(Hour_COMPARE,0); Stringdate=share.getString(DATE_COMPARE,); if(time_cop》=getUpdateHour() ||!date.equals(tempDate)){ saveWeatherList(mCalendar.get(Calendar.HOUR), tempDate); }elseif(time_cop《getUpdateHour()){ weatherList=newArrayList《WeatherMdoel》(); for(inti=0;i《4;i++){ WeatherMdoelmodel=newWeatherMdoel(); model.setWeek(share.getString(DAY_OF_WEEK+i, )); model.setLowTemp(share.getString(LOW+i,)); model .setHighTemp(share.getString(HIGH+i, )); model.setConditions(share.getString(CONDITION +i,)); Stringimage=share.getString(IMAGE+i,); byte[]base64Bytes=Base64.decodeBase64(image .getBytes()); ByteArrayInputStreambais=newByteArrayInputStream( base64Bytes); model.setImageUrl(); model .setImageDrawable(Drawable .createFromStream(bais, weather_image)); weatherList.add(model); } } }else{ saveWeatherList(mCalendar.get(Calendar.HOUR),tempDate); } }else{ saveWeatherList(mCalendar.get(Calendar.HOUR),tempDate); } //把适当的操作者放到于线程中继续执行,不堵塞UI if(handler.hasMessages(1)) handler.obtainMessage().recycle(); else{ Messagemsg=handler.obtainMessage(); msg.arg1=1; msg.sendToTarget(); } } },0,getUpdateHour()*3600*1000); } /** *第一次或者另外新的读取 */ voidsaveWeatherList(inthour,Stringday){ weatherList=newArrayList《WeatherMdoel》(); weatherList=strHelpeUtil.searchWeather(Html.fromHtml( getContext().getResources() .getString(R.string.googleWeatherApi)).toString(), getCityName()); SharedPreferences.EditorshareEditor=getContext() .getSharedPreferences(weather,Activity.MODE_PRIVATE).edit(); shareEditor.clear(); inti=0; for(WeatherMdoelmodel:weatherList){ shareEditor.putString(DAY_OF_WEEK+i,model.getWeek()); shareEditor.putString(LOW+i,model.getLowTemp()); shareEditor.putString(HIGH+i,model.getHighTemp()); shareEditor.putString(CONDITION+i,model.getConditions()); /** *将图片现金 */ ByteArrayOutputStreambaos=newByteArrayOutputStream(); ((BitmapDrawable)strHelpeUtil.loadImage(model.getImageUrl())) .getBitmap().compress(CompressFormat.JPEG,50,baos); StringImageBase64=newString(Base64.encodeBase64(baos .toByteArray())); shareEditor.putString(IMAGE+i,ImageBase64); i++; } shareEditor.putString(DATE_COMPARE,day); shareEditor.putInt(Hour_COMPARE,hour); shareEditor.putString(CITYNAE_SHARE,cityName); shareEditor.commit(); } /** *初始化组件信息 */ voidinit(){ model=weatherList.get(0); iv_weather.setImageDrawable(model.getImageUrl()==?model .getImageDrawable():strHelpeUtil.loadImage(model .getImageUrl())); tv_state.setText(model.getConditions()); tv_position.setText(getCityName()); tv.setText(getContext().getResources().getString(R.string.temp_format, model.getLowTemp(),model.getHighTemp())); } /** *获释对象 */ publicvoidreleaseTimer(){ timer.。
本文来源:tb天博体育-www.vakolah.com
Copyright © 2000-2024 www.vakolah.com. tb天博体育科技 版权所有备案号:ICP备48186170号-3网站地图