仿网易新闻APP(四)——标题栏之本市天气(百度定位与车联网之天气查询)
废话不多说,上图:
下面用到的知识有,百度定位及车联网API的使用,当然车联网API看起来高大上,其实我们这里只用来获取车联网中的天气查询功能。其他的功能还有渐变动画及缩放动画,以及定时更新天气及定位信息,存储天气信息到SharedPreference文件中。这些都是在后台执行的哦。
1.获取百度地图密钥并导入开发包
看看这个标题,我们就知道必须要获得自己所在的区域,才知道怎么定位本地,定位本地之后才能确定获取的天气信息。那么用谷歌地图?中国有一个强大的局域网,你不翻墙,貌似不能用把,在中国想要定位无外乎两大厂商,一个高德地图,一个百度地图,你可以根据习惯自己选择自己适合的地图。我们这里用的是百度地图的定位功能。下面我们来使用百度提供的功能。
①申请百度帐号
这个不用多说,不然你怎么管理应用呢?在这个网址注册:http://developer.baidu.com/map/
②创建应用
在第一个步骤中的网址中选择如下所示的信息:
然后点击申请密钥(ak),如图:
点击申请密钥后,得到如下图所示的结果:
那么下一步就是创建应用了。如下:
数字签名的获取方式如下所示:
输入的密钥口令是android,这里没显示,具体原因我也不清楚,不过是一定要输入的。获取的SHA1证书指纹。
那么百度创建应用里面的安全码就是这个指纹+;+包名(当然没有+号),包名在AndroidManifest.xml中的<manifest>标签属性中的package值。
提交后就会得到访问应用的AK:
③下载SDK
如图点击全部下载:
我们只需要定位功能,下载一个开发包就够了,如下:
不会用下载示例代码参考,好了,百度开发者中心的任务到这里就完成了。
④导入开发包
我将BaiduLBS_Android.jar放在app/src/libs中,将所有的so文件放在app/libs中。
打开项目app目录下的build.gragle添加如下代码才算导入所有文件都成功:
sourceSets {main {jniLibs.srcDirs = ['libs']} }
这个是android标签的直接子标签。当然上面的jar文件还要add as library才能成功。
2.定位到本市(也就是本地)
①添加密钥
在配置文件AndroidManifest.xml中application标签中添加如下代码:
<meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="qxpnHHLKBgCrTEUNPAwDF7Df" /> //key:开发者申请的key
②在application标签中声明service组件,每个app拥有自己单独的定位service
<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"> <intent-filter> <action android:name="com.baidu.location.service_v2.2"> </action> </intent-filter> </service>
③申明使用权限
<!-- 这个权限用于进行网络定位--> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> <!-- 这个权限用于访问GPS定位--> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位--> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> <!-- 获取运营商信息,用于支持提供运营商信息相关的接口--> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位--> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> <!-- 用于读取手机当前的状态--> <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> <!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据--> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <!-- 访问网络,网络定位需要上网--> <uses-permission android:name="android.permission.INTERNET" /> <!-- SD卡读取权限,用户写入离线定位数据--> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
④合理使用定位及天气
我们知道,怎么获取天气,不造成应用程序的卡盾,又可以在用户神不知鬼不觉的时候更新天气呢?
答案是定时更新定位天气信息。
那么广播和Service就可以达到你想要的效果。
我们在Service里面定时定位和更新天气并存储到SharedPreferences文件中。我们在这里设置每8个小时更新一次天气。
我们在启动一个Service执行定位功能。
WeatherService代码如下:
首先定义两个成员变量:
private LocationClient mLocationClient = null; private BDLocationListener myListener = new MyLocationListener();
当启动Service后会首先执行onCreate(),我们在里面初始化成员变量:
@Override public void onCreate() {super.onCreate(); mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类 mLocationClient.registerLocationListener(myListener); //注册监听函数 initLocation(); }
这里设置了定位的监听函数,当请求定位的时候,在监听函数onReceiveLocation中返回定位的结果信息。
监听函数的代码如下:
public class MyLocationListener implements BDLocationListener {@Override public void onReceiveLocation(BDLocation location) {//Receive Location StringBuffer sb = new StringBuffer(256); sb.append("time : "); sb.append(location.getTime()); sb.append("\nerror code : "); sb.append(location.getLocType()); sb.append("\nlatitude : "); sb.append(location.getLatitude()); sb.append("\nlontitude : "); sb.append(location.getLongitude()); sb.append("\nradius : "); sb.append(location.getRadius()); if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果 sb.append("\nspeed : "); sb.append(location.getSpeed());// 单位:公里每小时 sb.append("\nsatellite : "); sb.append(location.getSatelliteNumber()); sb.append("\nheight : "); sb.append(location.getAltitude());// 单位:米 sb.append("\ndirection : "); sb.append(location.getDirection());// 单位度 sb.append("\naddr : "); sb.append(location.getAddrStr()); sb.append("\ndescribe : "); sb.append("gps定位成功"); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果 sb.append("\naddr : "); sb.append(location.getAddrStr()); //运营商信息 sb.append("\noperationers : "); sb.append(location.getOperators()); sb.append("\ndescribe : "); sb.append("网络定位成功"); } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果 sb.append("\ndescribe : "); sb.append("离线定位成功,离线定位结果也是有效的"); } else if (location.getLocType() == BDLocation.TypeServerError) {sb.append("\ndescribe : "); sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因"); } else if (location.getLocType() == BDLocation.TypeNetWorkException) {sb.append("\ndescribe : "); sb.append("网络不同导致定位失败,请检查网络是否通畅"); } else if (location.getLocType() == BDLocation.TypeCriteriaException) {sb.append("\ndescribe : "); sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机"); }sb.append("\nlocationdescribe : "); sb.append(location.getLocationDescribe());// 位置语义化信息 Log.i("liyuanjinglyj",sb.toString()); //Log.i("liyuanjinglyj", location.getCity()); //Log.i("liyuanjinglyj",location.getCity().substring(0,location.getCity().length()-1)); mLocationClient.stop(); } }
在后面使用location.getCity()返回你所定位的市级信息,比如你是宜昌市的就返回的是宜昌市。
start:启动定位SDK。 stop:关闭定位SDK。调用start之后只需要等待定位结果自动回调即可。
开发者定位场景如果是单次定位的场景,在收到定位结果之后直接调用stop函数即可。
如果stop之后仍然想进行定位,可以再次start等待定位结果回调即可。
我们基本8个小时才执行一次,所以记得用完后关闭定位SDK。节省系统资源。
initLocation为初始化定位所需要的参数,代码如下:
private void initLocation(){LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy );//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备 option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系 int span=1000; option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的 option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要 option.setOpenGps(true);//可选,默认false,设置是否使用gps option.setLocationNotify(true);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果 option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近” option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到 option.setIgnoreKillProcess(false);//可选,默认false,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认杀死 option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集 option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要 mLocationClient.setLocOption(option); }
根据你的需要可以适当的更改数据,或删除数据。
@Override public int onStartCommand(Intent intent, int flags, int startId) {updateWeather(); AlarmManager manager=(AlarmManager)getSystemService(ALARM_SERVICE); int anHour=8*60*60*1000; long triggerAtTime= SystemClock.elapsedRealtime()+anHour; Intent i=new Intent(this, AutoUpdateReceiver.class); PendingIntent pi=PendingIntent.getBroadcast(this,0,i,0); manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pi); return super.onStartCommand(intent,flags,startId); }/** * 开始定位 */ private void updateWeather(){mLocationClient.start(); }
Intent i=new Intent(this,WeatherService.class); startService(i);
public class AutoUpdateReceiver extends BroadcastReceiver {@Override public void onReceive(Context context, Intent intent) {Intent i=new Intent(context, WeatherService.class); context.startService(i); } }
⑤配置广播与服务
<service android:name="com.example.liyuanjing.service.WeatherService"/> <receiver android:name="com.example.liyuanjing.receiver.AutoUpdateReceiver"/>
3.获取天气信息
try {String url="http://api.map.baidu.com/telematics/v3/weather?location="+ URLEncoder.encode(location.getCity().substring(0, location.getCity().length() - 1), "UTF-8")+"&output=json&ak=" +"qxpnHHLKBgCrTEUNPAwDF7Df"+"&mcode="+"9E:8B:66:E6:5E:73:FD:3A:AE:56:E4:22:68:DE:8E:6C:FC:16:E0:22;com.example.liyuanjing.demowy"; new CoolAsyncTask().execute(url); } catch (UnsupportedEncodingException e) {e.printStackTrace(); }
private class CoolAsyncTask extends AsyncTask<String,Integer,String> {@Override protected String doInBackground(String... params) {String str=null; try {str=new String(ConnectNetwork.getImageDat(params[0])); } catch (Exception e) {e.printStackTrace(); }return str; }@Override protected void onPostExecute(String s) {WeatherItem item =WeatherJson.readWeatherFromInputStream(s); SharedPreferences.Editor editor= PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit(); editor.putString("pm",item.getPm()); editor.putString("current_city", item.getCurrentCity()); editor.putString("current_date",item.getDate()); editor.putString("weather",item.getWeather()); editor.putString("wind",item.getWind()); editor.putString("temperature",item.getTemperature()); editor.commit(); } }
4.Json解析百度天气
public class WeatherJson {public static WeatherItem readWeatherFromInputStream(String str){WeatherItem weatherItem=null; try {weatherItem=new WeatherItem(); //遍历第一层数据 JSONObject rootJson = new JSONObject(str); JSONArray jsonArray=rootJson.getJSONArray("results"); //遍历第二层数据 JSONObject resultsJsonObject= (JSONObject) jsonArray.get(0); //Log.i("liyuanjinglyj",resultsJsonObject.getString("pm25")); //Log.i("liyuanjinglyj",resultsJsonObject.getString("currentCity")); weatherItem.setPm(resultsJsonObject.getString("pm25")); weatherItem.setCurrentCity(resultsJsonObject.getString("currentCity")); JSONArray weatherDataJsonArray=resultsJsonObject.getJSONArray("weather_data"); //遍历第三层数据 JSONObject nowJsonObject= (JSONObject) weatherDataJsonArray.get(0); //Log.i("liyuanjinglyj",nowJsonObject.getString("wind")); //Log.i("liyuanjinglyj",nowJsonObject.getString("weather")); //Log.i("liyuanjinglyj",nowJsonObject.getString("date")); //Log.i("liyuanjinglyj",nowJsonObject.getString("temperature")); weatherItem.setWind(nowJsonObject.getString("wind")); weatherItem.setWeather(nowJsonObject.getString("weather")); weatherItem.setDate(nowJsonObject.getString("date")); weatherItem.setTemperature(nowJsonObject.getString("temperature")); } catch (JSONException e) {e.printStackTrace(); }return weatherItem; } }
error: 0,
status: "success",
date: "2013-07-17",
results:
[
{
currentCity: "北京市",
pm25: "166",
index: [
{
title: "穿衣",
zs: "舒适",
tipt: "穿衣指数",
des: "建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。"
},
{
title: "洗车",
zs: "不宜",
tipt: "洗车指数",
des: "不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。"
},
{
title: "感冒",
zs: "较易发",
tipt: "感冒指数",
des: "相对今天出现了较大幅度降温,较易发生感冒,体质较弱的朋友请注意适当防护。"
},
{
title: "运动",
zs: "较不宜",
tipt: "运动指数",
des: "有降水,推荐您在室内进行健身休闲运动;若坚持户外运动,须注意携带雨具并注意避雨防滑。"
},
{
title: "紫外线强度",
zs: "弱",
tipt: "紫外线强度指数",
des: "紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。"
}
],
weather_data:
[
{
date: "周三(今天, 实时:24℃)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png",
weather: "多云",
wind: "微风",
temperature: "23℃~ 15℃"
},
{
date: "明天(周四)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/leizhenyu.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/zhongyu.png",
weather: "雷阵雨转中雨",
wind: "微风",
temperature: "29~22℃"
},
{
date: "后天(周五)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/yin.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png",
weather: "阴转多云",
wind: "微风",
temperature: "31~23℃"
},
{
date: "大后天(周六)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png",
weather: "多云",
wind: "微风",
temperature: "31~24℃"
}
]
},
{
currentCity: "合肥市",
weather_data:
[
{
date: "今天(周三)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png",
weather: "多云",
wind: "东风3-4级",
temperature: "27℃"
},
{
date: "明天(周四)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png",
weather: "多云",
wind: "东北风3-4级",
temperature: "35~27℃"
},
{
date: "后天(周五)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png",
weather: "多云",
wind: "南风",
temperature: "35~27℃"
},
{
date: "大后天(周六)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png",
weather: "多云",
wind: "东风",
temperature: "34~27℃"
}
]
}
]
}
5.天气布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/weather_layout_main" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/lyj_title_bar_layout_main" android:layout_marginTop="-6dp" android:visibility="gone"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#00000000"> <ImageView android:layout_width="12dp" android:layout_height="6dp" android:layout_alignParentRight="true" android:layout_marginRight="18dp" android:background="@drawable/biz_main_more_menu_indication"/> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="20dp" android:layout_marginTop="40dp" android:background="#88ffffff"> <LinearLayout android:id="@+id/dushu" android:layout_width="match_parent" android:layout_height="83dp" android:orientation="horizontal"> <ImageView android:id="@+id/weather_layout_current_one_image" android:layout_width="50dp" android:layout_height="83dp" android:background="@drawable/biz_more_menu_t2"/> <ImageView android:id="@+id/weather_layout_current_two_image" android:layout_width="50dp" android:layout_height="83dp" android:background="@drawable/biz_more_menu_t1"/> <RelativeLayout android:layout_width="wrap_content" android:layout_height="83dp"> <ImageView android:layout_width="30dp" android:layout_height="24dp" android:background="@drawable/biz_more_menu_weather_symbol"/> <TextView android:id="@+id/weather_layout_temperature" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="@string/weather_layout_section" android:textSize="18sp"/> </RelativeLayout> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:gravity="right" android:layout_marginRight="20dp" android:orientation="vertical"> <ImageView android:layout_width="32dp" android:layout_height="32dp" android:background="@drawable/biz_pc_plugin_weather_duoyun"/> <TextView android:id="@+id/weather_layout_weatherwind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/weather_layout_desc" android:textSize="18sp"/> <TextView android:id="@+id/weather_layout_location" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/weather_layout_location" android:textSize="18sp"/> </LinearLayout> <LinearLayout android:id="@+id/weather_layout_desc" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/dushu" android:orientation="vertical"> <TextView android:id="@+id/weather_layout_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/weather_layout_time" android:textSize="18sp"/> <TextView android:id="@+id/weather_layout_pm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/weather_layout_pm" android:textSize="18sp"/> </LinearLayout> <LinearLayout android:id="@+id/weather_layout_one_images" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/weather_layout_desc" android:layout_marginTop="50dp" android:layout_marginLeft="-10dp" android:orientation="horizontal"> <LinearLayout android:id="@+id/weather_layout_search" android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" android:layout_weight="1"> <ImageButton android:layout_width="70dp" android:layout_height="70dp" android:scaleType="fitXY" android:background="@drawable/ic_main_more_menu_search_icon"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="@string/weather_layout_search_txt"/> </LinearLayout> <LinearLayout android:id="@+id/weather_layout_headline" android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" android:layout_weight="1"> <ImageButton android:layout_width="70dp" android:layout_height="70dp" android:scaleType="fitXY" android:background="@drawable/ic_main_more_menu_headline_icon"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="@string/weather_layout_headline_txt"/> </LinearLayout> <LinearLayout android:id="@+id/weather_layout_offline" android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center" android:layout_weight="1"> <ImageButton android:layout_width="70dp" android:layout_height="70dp" android:scaleType="fitXY" android:background="@drawable/ic_main_more_menu_offline_icon"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="@string/weather_layout_offline_txt"/> </LinearLayout> </LinearLayout> <LinearLayout android:layout_below="@+id/weather_layout_one_images" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_marginLeft="-10dp" android:orientation="horizontal"> <LinearLayout android:id="@+id/weather_layout_theme" android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" android:layout_weight="1"> <ImageButton android:layout_width="70dp" android:layout_height="70dp" android:scaleType="fitXY" android:background="@drawable/ic_main_more_menu_theme_icon"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="@string/weather_layout_theme_txt"/> </LinearLayout> <LinearLayout android:id="@+id/weather_layout_scan" android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" android:layout_weight="1"> <ImageButton android:layout_width="70dp" android:layout_height="70dp" android:scaleType="fitXY" android:background="@drawable/ic_main_more_menu_scan_icon"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="@string/weather_layout_scan_txt"/> </LinearLayout> <LinearLayout android:id="@+id/weather_layout_invitation" android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center" android:layout_weight="1"> <ImageButton android:layout_width="70dp" android:layout_height="70dp" android:scaleType="fitXY" android:background="@drawable/ic_main_more_menu_invitation_icon"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="@string/weather_layout_invitation_txt"/> </LinearLayout> </LinearLayout> </RelativeLayout> </LinearLayout>
6.渐变动画和缩放动画
<set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:duration="500" android:fromAlpha="0.0" android:toAlpha="1.0"/> </set>
<set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:duration="500" android:fromAlpha="1.0" android:toAlpha="0.0"/> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:duration="500" android:startOffset="500" android:fillAfter="false" android:fromXScale="0.0" android:toXScale="1.1" android:fromYScale="0.0" android:toYScale="1.1" android:pivotX="50%" android:pivotY="50%"/> </set>
//天气代码片段 private LinearLayout weatherLayout;//天气界面整体布局 //六个按钮 private LinearLayout weatherSearchLayout; private LinearLayout weatherHeadlineLayout; private LinearLayout weatherOfflineLayout; private LinearLayout weatherThemeLayout; private LinearLayout weatherScanLayout; private LinearLayout weatherInvitationLayout; //显示和隐藏天气界面的按钮,也就是标题栏上面的三个点 private ImageButton weatherBut; //标记界面是显示还是隐藏,使按钮点击显示和关闭 private boolean weatherDialogFlag = false;
//天气代码片段 this.weatherLayout = (LinearLayout) view.findViewById(R.id.weather_layout_main); this.weatherSearchLayout = (LinearLayout) view.findViewById(R.id.weather_layout_search); this.weatherHeadlineLayout = (LinearLayout) view.findViewById(R.id.weather_layout_headline); this.weatherOfflineLayout = (LinearLayout) view.findViewById(R.id.weather_layout_offline); this.weatherThemeLayout = (LinearLayout) view.findViewById(R.id.weather_layout_theme); this.weatherScanLayout = (LinearLayout) view.findViewById(R.id.weather_layout_scan); this.weatherInvitationLayout = (LinearLayout) view.findViewById(R.id.weather_layout_invitation); this.weatherBut = (ImageButton) view.findViewById(R.id.weather); final Animation alphaShow = AnimationUtils.loadAnimation(getActivity(), R.anim.weather_layout_alpha_show_anim); final Animation alphaHide = AnimationUtils.loadAnimation(getActivity(), R.anim.weather_layout_alpha_hide_anim); final Animation scaleAnim = AnimationUtils.loadAnimation(getActivity(), R.anim.weather_layout_scale_anim); this.weatherBut.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {if (weatherDialogFlag == false) {weatherLayout.setAnimation(alphaShow); alphaShow.start();//显示天气界面 weatherLayout.setVisibility(View.VISIBLE); weatherDialogFlag = true;//标记界面已经显示 //立即执行按钮缩放动画 weatherSearchLayout.startAnimation(scaleAnim); weatherHeadlineLayout.setAnimation(scaleAnim); weatherOfflineLayout.startAnimation(scaleAnim); weatherThemeLayout.startAnimation(scaleAnim); weatherScanLayout.startAnimation(scaleAnim); weatherInvitationLayout.startAnimation(scaleAnim); showWeather(view); } else {weatherLayout.setAnimation(alphaHide); alphaHide.start();//执行渐变隐藏动画 weatherLayout.setVisibility(View.GONE); weatherDialogFlag = false;//标记界面没有显示 }} });
7.设置天气界面
private void showWeather(View view) {TextView temperatureTxt = (TextView) view.findViewById(R.id.weather_layout_temperature); TextView weatherWindTxt = (TextView) view.findViewById(R.id.weather_layout_weatherwind); TextView dateTxt = (TextView) view.findViewById(R.id.weather_layout_date); TextView pmTxt = (TextView) view.findViewById(R.id.weather_layout_pm); TextView locationTxt = (TextView) view.findViewById(R.id.weather_layout_location); ImageView oneImages = (ImageView) view.findViewById(R.id.weather_layout_current_one_image); ImageView twoImages = (ImageView) view.findViewById(R.id.weather_layout_current_two_image); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); temperatureTxt.setText(prefs.getString("temperature", "")); weatherWindTxt.setText(prefs.getString("weather", "") + " " + prefs.getString("wind", "")); String desc=null; if(prefs.getString("pm","")!=""){int number=Integer.valueOf(prefs.getString("pm","")); if(number<=35){desc="优质空气"; }else if(number<=75){desc="无污染"; }else if(number<=115){desc="轻度污染"; }else if(number<=150){desc="中度污染"; }else if(number<=250){desc="重度污染"; }else if(number<=115){desc="严重污染"; }pmTxt.setText(prefs.getString("pm", "")+" "+desc); }locationTxt.setText(prefs.getString("current_city", "")); if (prefs.getString("current_date", "") != "") {String str = prefs.getString("current_date", ""); int index = str.indexOf(")"); String currentTemp = str.substring(index - 3, index - 1);//截取实时温度 index = str.indexOf("("); String currentDate = str.substring(0, index - 1);//截取实时温度前面的时间 dateTxt.setText(currentDate); char c1 = currentTemp.charAt(0);//截取两位数温度的第一位数 char c2 = currentTemp.charAt(1);//截取两位数温度的第二位数 for (int i = 0; i < 10; i++) {if (String.valueOf(i).equals(String.valueOf(c1))) {//比较温度等于哪个数字oneImages.setBackgroundResource(imageRes[i]); }if(String.valueOf(i).equals(String.valueOf(c2))){//与上解释同理twoImages.setBackgroundResource(imageRes[i]); }}} }
仿网易新闻APP(四)——标题栏之本市天气(百度定位与车联网之天气查询)相关推荐
- 仿网易新闻APP(三)——标题栏之24小时要闻
感冒了几天,今天总算正常了点,想了想我还有未完成的最后一类博文,马上就跳起来,接着奋斗了.废话就不多说了先上图看看网易新闻APP的标题栏24小时要闻如下: 我们即将完成的效果图: 图片显示0是默认图片 ...
- 本市天气(百度定位与车联网之天气查询)
废话不多说,上图: 下面用到的知识有,百度定位及车联网API的使用,当然车联网API看起来高大上,其实我们这里只用来获取车联网中的天气查询功能.其他的功能还有渐变动画及缩放动画,以及定时更新天气及定位 ...
- 仿网易新闻APP(五)——无限横向滑动菜单(自定义HorizontalScrollView+ViewPager)
自从Gallery被谷歌废弃以后,Google推荐使用ViewPager和HorizontalScrollView来实现Gallery的效果.的确HorizontalScrollView可以实现Gal ...
- 【快速开发App实战】BUI高仿网易新闻App系列一、搭建App开发环境和工作空间
一. 搭建App开发环境和工作空间 前言 我们的目标是要做一个真实的案例, 着重通过BUI框架及其相关工具的使用, 结合原生打包平台, 帮助大家理解一个App的开发过程. 以最新网易新闻的App为例, ...
- android 仿网易标题栏,仿网易新闻可滑动标题栏TabLayout(文字或图标)
近期有需要,要做一个类似于网易新闻首页中的可滑动标题栏 TabLayout,根据大神写的 FlycoTabLayout 改造了一下,可以加载网络图片,主要实现内容如下: 1. 可配置标题选中效果(下划 ...
- Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻客户端Tab标签
转载请注明出处:http://blog.csdn.net/xiaanming/article/details/10766053 之前用JakeWharton的开源框架ActionBarSherl ...
- 网易新闻 html5,HTML5+SWIPER仿网易新闻横滑翻页及联动
[实例简介] 一套仿网易新闻的闻横滑翻页,联动导航,用到了swiper.js zepto.js scroll.js效果不错 [实例截图] [核心代码] swiper └── swiper ├── cs ...
- Android高仿网易新闻客户端之动态添加标签
承接上一篇文章:Android高仿网易新闻客户端之首页,今天来实现动态添加标签效果. 动态标签页是一个流式布局,实现了宽度自动换行高度自动分配的功能,代码如下: FlowLayout.java pac ...
- android 仿网易标签切换,Android 仿网易新闻客户端Tab标签
Android 开源框架ViewPageIndicator和ViewPager仿网易新闻客户端Tab标签 http://blog.csdn.net/xiaanming/article/details/ ...
最新文章
- GATB=The Genome Analysis Toolbox with de-Bruijn graph 带有de-Bruijn图的基因组分析工具箱
- solr配置-Schema.xml
- matlab guide对话框+滑动条+弹出式菜单+列表框的使用
- python 表示图论_Python 图论工具 | 学步园
- 第一次写oracle SQL 两个表链接查询
- matlab rsenc函数,Xilinx RS编码IP核仿真验证
- css的样式局部作用,局部套用CSS样式.PPT
- k2p刷机丢失eeprom_刷机经验分享,K2P无线路由器救砖必备,无线和MAC恢复窍门
- 363、Java中级18 -【JDBC - 增、删、改】 2020.06.28
- lintcode 873 模拟松鼠(JavaScript)
- 006 研究生学信网的电子注册备案表在哪里弄
- 2020奶茶行业研究报告
- 学会这两招将知网下载的CAJ转成Word形式
- 阿里巴巴数字化运营能力
- pandas合并groupby_pandas数据聚合与分组运算——groupby方法
- 顶刊实证复现:扶贫改革试验区的经济增长效应及政策有效性评估!思路梳理+全数据源+python代码
- 丰田汽车音频总线AVC-LAN
- 长安大学C语言程序设计作业,2017春C语言程序设计 上(长安大学)
- 在少儿编程中使用easygui(3):enterbox和multenterbox
- hdl四位二进制计数器_quartus4位二进制加减法计数器.doc
热门文章
- u大师u盘装系统win7_海尔简爱1406w笔记本安装win7|U盘安装Win7系统
- 浪涌/ESD的基础与对策零部件
- ⭐算法入门⭐《二分枚举》简单15 —— LeetCode LCP 18. 早餐组合
- Java根据地址计算日出日落时间(百度地图API)
- buck同步整流sw点负压问题
- linux下稳定性测试软件详解
- npm淘宝镜像cnpm安装使用(最新版),cnpm临时单次/永久使用
- nginx的keepalive和keepalive_requests
- Python自动化办公之Word,全网最全看这一篇就够了
- Structural patterns (proxy、bridge、decorator)学习笔记(一)