由于Android opengl绘制机制的原因,在使用slidingMenu和百度地图在切换的时候会出现黑边的现象,查了百度地图官方也没有找到好的解决办法,于是想到使用截图的方式来解决,大致的步骤如下:
1.slidingMenu打开的时候调用BaiduMap的snapshot方法截图获取Bitmap对象;
2.使用ImageView替换BaiduMap View并显示截取的图片;
3.slidingMenu关闭后再显示BaiduMap VIew。
主要代码:
fragmetn_map.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/image_temp" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone"/> <com.baidu.mapapi.map.MapView android:id="@+id/bmapView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" /> </LinearLayout>
FragmentMap.javapackage com.ranfeng.ygty.slidingmeunmap; import android.graphics.Bitmap; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import com.baidu.mapapi.SDKInitializer; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.MyLocationConfiguration; import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu; /** * Created by rf on 2015/9/7. */ public class FragmentMap extends Fragment { private View view; private MapView mMapView = null; private BaiduMap mBaiduMap = null; private SlidingMenu slidingMenu; private ImageView imageTemp; private Bitmap tempBitmap; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { SDKInitializer.initialize(getActivity().getApplicationContext()); view = inflater.inflate(R.layout.fragmetn_map, container,false); imageTemp = (ImageView) view.findViewById(R.id.image_temp); //map相关 mMapView = (MapView) view.findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); // 开启定位图层 mBaiduMap.setMyLocationEnabled(true); mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL, true, null)); slidingMenu =((MainActivity)getActivity()).getSlidingMenu(); //打开的时候截图并隐藏map slidingMenu.setOnOpenListener(new SlidingMenu.OnOpenListener() { @Override public void onOpen() { Log.d("map", "slidingMenu is opening"); mBaiduMap.snapshot(new BaiduMap.SnapshotReadyCallback() { @Override public void onSnapshotReady(Bitmap bitmap) { FragmentMap.this.tempBitmap = bitmap; imageTemp.setImageBitmap(tempBitmap); imageTemp.setVisibility(View.VISIBLE); mMapView.setVisibility(View.GONE); } }); } }); //关闭以后再还原回来 slidingMenu.setOnClosedListener(new SlidingMenu.OnClosedListener() { @Override public void onClosed() { Log.d("map","slidingMenu is closed"); if(tempBitmap!=null&&!tempBitmap.isRecycled()) { tempBitmap.recycle(); tempBitmap = null; } imageTemp.setVisibility(View.GONE); mMapView.setVisibility(View.VISIBLE); } }); return view; } }
MainActivity.Java
package com.ranfeng.ygty.slidingmeunmap; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu; public class MainActivity extends AppCompatActivity { private FragmentManager fManager; private Fragment fragmentMap; private SlidingMenu slidingMenu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fManager = getSupportFragmentManager(); FragmentTransaction transaction = fManager.beginTransaction(); fragmentMap = new FragmentMap(); transaction.add(R.id.content,fragmentMap); transaction.show(fragmentMap); transaction.commit(); initSlidingMenu(); } public void initSlidingMenu(){ // configure the SlidingMenu slidingMenu = new SlidingMenu(this); slidingMenu.setMode(SlidingMenu.LEFT); // 设置触摸屏幕的模式 slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN ); slidingMenu.setShadowWidthRes(R.dimen.shadow_width); //menu.setShadowDrawable(R.drawable.shadow); // 设置滑动菜单视图的宽度 slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset); // 设置渐入渐出效果的值 slidingMenu.setFadeDegree(0.35f); /** * SLIDING_WINDOW will include the Title/ActionBar in the content * section of the SlidingMenu, while SLIDING_CONTENT does not. */ slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT); //为侧滑菜单设置布局 slidingMenu.setMenu(R.layout.layout_menu); } public void menuToggle(View v){ slidingMenu.toggle(); } public SlidingMenu getSlidingMenu(){ return slidingMenu; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
源码下载地址:
http://download.csdn.net/detail/cqrf2006/9086021
转自:http://blog.csdn.net/cqrf2006/article/details/48265211