1.百度地图web静态图加载
参考:http://lbsyun.baidu.com/index.php?title=static
某界面需要一个简单的地图展示,若加载地图的话,有点大材小用,且影响页面加载速度;此处使用百度地图提供的静态图简化。直接由百度生成一张地图图片,开发中只需加载此图片即可。
private void addMap() {
if (houseDetailModel.getLng().equals("0") && houseDetailModel.getLat().equals("0")) {
house_detail_map_rl.setVisibility(View.GONE);
line_house_map.setVisibility(View.GONE);
house_detail_map_rl.setClickable(false);
} else {
house_detail_map_rl.setVisibility(View.VISIBLE);
line_house_map.setVisibility(View.VISIBLE);
//地址
if (!StringUtils.isEmpty(houseDetailModel.getXdistrict_name() + houseDetailModel.getXstreet_name() + houseDetailModel.getXname())) {
house_detail_address_tx.setText(houseDetailModel.getXdistrict_name() + " " + houseDetailModel.getXstreet_name() + " " + houseDetailModel.getXname());
} else {
house_detail_address_tx.setText("暂无地址信息");
}
if (!StringUtils.isEmpty(houseDetailModel.getLat())) {
lati = Double.parseDouble(houseDetailModel.getLat());
} else {
lati = 31;
}
if (!StringUtils.isEmpty(houseDetailModel.getLng())) {
longti = Double.parseDouble(houseDetailModel.getLng());
} else {
longti = 118;
}
weight = house_detail_map_img.getWidth();
height = (house_detail_map_img.getHeight() * 1.0f / weight) * 1024;
String url = "http://api.map.baidu.com/staticimage/v2?ak=OoGTpMBFWDSq8KZrUnnGdkwScG1hKupj&zoom=18" +
"¢er=" + longti + "," + lati +
"&width=1024" + "&height=" + height +
"&scale=1" +
"&markerStyles=-1,http://7b1g8u.com1.z0.glb.clouddn.com/map_point.png,-1,23,25" +
"&markers=" + longti + "," + lati +
"&mcode=B0:F5:66:F0:4C:D9:1F:FC:E6:E1:59:43:30:0F:08:F4:E5:98:3E:3F;com.XXXX.sostarjob";
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
.setResizeOptions(new ResizeOptions(SizeUtils.dp2px(360), SizeUtils.dp2px(110))).build();
DraweeController draweeController = Fresco.newDraweeControllerBuilder()
.setImageRequest(request).setAutoPlayAnimations(true).build();
house_detail_map_img.setController(draweeController);
house_detail_map_rl.setClickable(true);
house_detail_map_rl.setOnClickListener(v -> {
AnalyticsAgent.onCustomClickMap("com.house365.rent.ui.activity.home.HouseDetail1Activity", "HouseDetail-HousingLocation", new HashMap());
Intent intentMap = new Intent(HouseDetail1Activity.this, MapHouseActivity.class);
intentMap.putExtra("name", houseDetailModel.getHouse_title());
intentMap.putExtra("lati", lati);
intentMap.putExtra("longti", longti);
intentMap.putExtra("addressdetail", houseDetailModel.getXdistrict_name() + houseDetailModel.getXstreet_name() + houseDetailModel.getXname());
intentMap.putExtra("address", " " + houseDetailModel.getXname() + " ");
startActivity(intentMap);
});
}
}
问题1:实际应用中发现,url中markerStyles参数提供的自定义定位图片偶尔会加载不出来,从而显示百度默认的定位图片,与需求不符。
解决方案,在xml布局文件中,画好定位图(此方案只适用于定位在地图的固定位置,比如中心)。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/house_detail_address_ll">
<com.facebook.drawee.view.SimpleDraweeView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:id="@+id/house_detail_map_img"
android:layout_width="match_parent"
android:layout_height="110dp"
fresco:failureImage="@mipmap/img_detail_bg"
fresco:failureImageScaleType="focusCrop"
fresco:placeholderImage="@mipmap/img_detail_bg"
fresco:placeholderImageScaleType="centerCrop"
fresco:retryImage="@mipmap/img_detail_bg"
fresco:retryImageScaleType="focusCrop" />
<ImageView
android:id="@+id/house_detail_point_img"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_centerInParent="true"
android:src="@mipmap/map_point"
android:visibility="gone" />
</RelativeLayout>
问题2:从百度获取静态图片时需要一定的时间,然而xml中的定位图会先展示出来,用户体验不是很好
解决方案:需要图片加载引擎做加载完图片再展示定位点图片的处理。
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
.setResizeOptions(new ResizeOptions(SizeUtils.dp2px(360), SizeUtils.dp2px(110))).build();
DraweeController draweeController = Fresco.newDraweeControllerBuilder()
.setControllerListener(new ControllerListener<ImageInfo>() {
@Override
public void onSubmit(String id, Object callerContext) {
}
@Override
public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
house_detail_point_img.setVisibility(View.VISIBLE);
}
@Override
public void onIntermediateImageSet(String id, ImageInfo imageInfo) {
}
@Override
public void onIntermediateImageFailed(String id, Throwable throwable) {
}
@Override
public void onFailure(String id, Throwable throwable) {
house_detail_point_img.setVisibility(View.GONE);
}
@Override
public void onRelease(String id) {
}
})
.setImageRequest(request).setAutoPlayAnimations(true).build();
house_detail_map_img.setController(draweeController);
String url = "http://api.map.baidu.com/staticimage/v2?ak=OoGTpMBFWDSq8KZrUnnGdkwScG1hKupj&zoom=18" +
"¢er=" + longti + "," + lati +
"&width=1024" + "&height=" + height +
"&scale=1" +
// "&markerStyles=-1,http://7b1g8u.com1.z0.glb.clouddn.com/map_point.png,-1,23,25" +
// "&markers=" + longti + "," + lati +
"&mcode=B0:F5:66:F0:4C:D9:1F:FC:E6:E1:59:43:30:0F:08:F4:E5:98:3E:3F;com.renyu.sostarjob";
ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
.setResizeOptions(new ResizeOptions(SizeUtils.dp2px(1024 / 3), SizeUtils.dp2px((int) height) / 3))
.setProgressiveRenderingEnabled(true).build();
ImagePipeline imagePipeline = Fresco.getImagePipeline();
DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, this);
dataSource.subscribe(new BaseBitmapDataSubscriber() {
@Override
protected void onNewResultImpl(final Bitmap bitmap) {
Observable.just(bitmap).observeOn(AndroidSchedulers.mainThread()).subscribe(bitmap1 -> {
house_detail_map_img.setImageBitmap(bitmap1);
house_detail_point_img.setVisibility(View.VISIBLE);
});
}
@Override
protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
house_detail_point_img.setVisibility(View.GONE);
}
}, CallerThreadExecutor.getInstance());
3.弹窗(覆盖整个页面)
//在xml布局文件加如下代码
<FrameLayout
android:id="@+id/layout_house_detail_jump"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/house_detail_visit_record_all_ll"
android:background="@color/translate"></FrameLayout>
//java
HouseDetailConfFragment houseDetailConfFragment = new HouseDetailConfFragment();
Bundle bundle = new Bundle();
bundle.putString("house_conf", houseDetailModel.getFacilities());
houseDetailConfFragment.setArguments(bundle);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// transaction.setCustomAnimations(R.anim.fragment_bottom_in, 0, 0, R.anim.fragment_bottom_out);
transaction.add(R.id.layout_house_detail_jump, houseDetailConfFragment).addToBackStack(null).commit();
4.webView 部分html的图片加载不出来
1.MIXED_CONTENT_NEVER_ALLOW=1:Webview不允许一个安全的站点(https)去加载非安全的站点内容(http),比如,https网页内容的图片是http链接。强烈建议App使用这种模式,因为这样更安全。
2.MIXED_CONTENT_ALWAYS_ALLOW=0:在这种模式下,WebView是可以在一个安全的站点(Https)里加载非安全的站点内容(Http),这是WebView最不安全的操作模式,尽可能地不要使用这种模式。
3.MIXED_CONTENT_COMPATIBILITY_MODE=2:在这种模式下,当涉及到混合式内容时,WebView会尝试去兼容最新Web浏览器的风格。一些不安全的内容(Http)能被加载到一个安全的站点上(Https),而其他类型的内容将会被阻塞。这些内容的类型是被允许加载还是被阻塞可能会随着版本的不同而改变,并没有明确的定义。这种模式主要用于在App里面不能控制内容的渲染,但是又希望在一个安全的环境下运行
//从Android5.0开始,WebView默认不支持同时加载Https和Http混合模式。
//在Android5.0以下,默认是采用的MIXED_CONTENT_ALWAYS_ALLOW模式,即总是允许WebView同时加载Https和Http;
//而从Android5.0开始,默认用MIXED_CONTENT_NEVER_ALLOW模式,即总是不允许WebView同时加载Https和Http。
if(VERSION.SDK_INT > 19) {
settings.setMixedContentMode(0);
}
5.SwipeMenuRecyclerView
1.某item左划后删除,其它某一个item会被拉出删除按钮;可通过如下方法设置将其先关闭,再进行删除操作
private SwipeMenuRecyclerView mDeleteRecyclerView;
mDeleteRecyclerView.smoothCloseMenu();
//删除操作
2.刷新从location到最后的item
notifyItemRangeChanged(location, getItemCount());