模仿微信摇一摇
Activity
public class Main2Activity extends AppCompatActivity implements SensorEventListener {
ImageView up;
ImageView down;
private SensorManager sensorManager;
private Sensor sensor;
private Vibrator vibrator;
private SoundPool soundPool;
private TranslateAnimation animationup;
private TranslateAnimation animationdown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
up = findViewById(R.id.iv2);
down = findViewById(R.id.iv3);
initData();
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this,sensor,200);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
private void initData() {
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
soundPool = new SoundPool(5,AudioManager.STREAM_MUSIC,0);
soundPool.load(this,R.raw.wx,1);
animationup = new TranslateAnimation(0, 0, 0, -100);
animationdown = new TranslateAnimation(0, 0, 0, 100);
}
@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
float x = values[0];
float y = values[1];
float z = values[2];
if (Math.abs(x)>15 || Math.abs(y)>15 || Math.abs(z)>15){
long[] patten = {300,500};
vibrator.vibrate(patten,-1);
soundPool.play(1,1,1,1,1,1);
animationdown.setDuration(500);
animationup.setDuration(500);
up.startAnimation(animationup);
down.startAnimation(animationdown);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity">
<ImageView
android:layout_centerInParent="true"
android:id="@+id/iv1"
android:src="@drawable/shakehideimg_man2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:gravity="center"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv2"
android:src="@drawable/shake_logo_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/iv3"
android:src="@drawable/shake_logo_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
效果
百度地图实现
百度地图SDK申请密钥
权限
<uses-permission android:name="android.permission.VIBRATE" />
<!--允许程序打开网络套接字-->
<uses-permission android:name="android.permission.INTERNET" />
<!--允许程序设置内置sd卡的写权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--允许程序获取网络状态-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--允许程序访问WiFi网络信息-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--允许程序读写手机状态和身份-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--允许程序访问CellID或WiFi热点来获取粗略的位置-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!--用于访问GPS定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<!--用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<!--用于申请调用A-GPS模块-->
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
<!--用于申请获取蓝牙信息进行室内定位-->
<uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.GET_TASKS" />
build.gradle里额外配置一句
sourceSets {
main {
jniLibs.srcDir 'libs'
}
}
添加开发密钥
<application>
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="开发者 key" />
</application>
布局
<com.baidu.mapapi.map.MapView
android:id="@+id/myMap1"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.baidu.mapapi.map.MapView>
MyApplication类代码
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
SDKInitializer.initialize(this);
}
}
Activity
public class Main3Activity extends AppCompatActivity {
MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
mapView = findViewById(R.id.myMap1);
}
}
效果
百度地图路线规划
Activity
public class Main3Activity extends AppCompatActivity implements BaiduMap.OnMapClickListener, OnGetRoutePlanResultListener {
Button mBtnPre = null;//上一个节点
Button mBtnNext = null;//下一个节点
int nodeIndex = -1;//节点索引,供浏览节点时使用
RouteLine route = null;
OverlayManager routeOverlay = null;
boolean useDefaultIcon = false;
private TextView popupText = null;
//地图相关,使用继承MapView的MyRouteMapView目的是重写touch事件实现泡泡处理
//如果不处理touch事件,则无需继承,直接使用MapView即可
MapView mMapView = null; // 地图View
BaiduMap mBaidumap = null;
//搜索相关
RoutePlanSearch mSearch = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main3);
CharSequence titleLable = "路线规划功能";
setTitle(titleLable);
//初始化地图
mMapView = (MapView) findViewById(R.id.map);
mBaidumap = mMapView.getMap();
//设定中心点坐标
LatLng cenpt = new LatLng(37.52,121.39);
//定义地图状态
MapStatus mMapStatus = new MapStatus.Builder()
.target(cenpt)
.build();
//定义MapStatusUpdate对象,以便描述地图状态将要发生的变化
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
//改变地图状态
mBaidumap.setMapStatus(mMapStatusUpdate);
mBtnPre = (Button) findViewById(R.id.pre);
mBtnNext = (Button) findViewById(R.id.next);
mBtnPre.setVisibility(View.INVISIBLE);
mBtnNext.setVisibility(View.INVISIBLE);
//地图点击事件处理
mBaidumap.setOnMapClickListener(this);
// 初始化搜索模块,注册事件监听
mSearch = RoutePlanSearch.newInstance();
mSearch.setOnGetRoutePlanResultListener(this);
}
public void SearchButtonProcess(View v) {
//重置浏览节点的路线数据
route = null;
mBtnPre.setVisibility(View.INVISIBLE);
mBtnNext.setVisibility(View.INVISIBLE);
mBaidumap.clear();
// 处理搜索按钮响应
EditText editSt = (EditText) findViewById(R.id.start);
EditText editEn = (EditText) findViewById(R.id.end);
//设置起终点信息,对于tranist search 来说,城市名无意义
PlanNode stNode = PlanNode.withCityNameAndPlaceName("北京", editSt.getText().toString());
PlanNode enNode = PlanNode.withCityNameAndPlaceName("北京", editEn.getText().toString());
// 实际使用中请对起点终点城市进行正确的设定
if (v.getId() == R.id.drive) {
mSearch.drivingSearch((new DrivingRoutePlanOption())
.from(stNode)
.to(enNode));
} else if (v.getId() == R.id.transit) {
mSearch.transitSearch((new TransitRoutePlanOption())
.from(stNode)
.city("北京")
.to(enNode));
} else if (v.getId() == R.id.walk) {
mSearch.walkingSearch((new WalkingRoutePlanOption())
.from(stNode)
.to(enNode));
}
}
public void nodeClick(View v) {
if (route == null ||
route.getAllStep() == null) {
return;
}
if (nodeIndex == -1 && v.getId() == R.id.pre) {
return;
}
//设置节点索引
if (v.getId() == R.id.next) {
if (nodeIndex < route.getAllStep().size() - 1) {
nodeIndex++;
} else {
return;
}
} else if (v.getId() == R.id.pre) {
if (nodeIndex > 0) {
nodeIndex--;
} else {
return;
}
}
//获取节结果信息
LatLng nodeLocation = null;
String nodeTitle = null;
Object step = route.getAllStep().get(nodeIndex);
if (step instanceof DrivingRouteLine.DrivingStep) {
nodeLocation = ((DrivingRouteLine.DrivingStep) step).getEntrance().getLocation();
nodeTitle = ((DrivingRouteLine.DrivingStep) step).getInstructions();
} else if (step instanceof WalkingRouteLine.WalkingStep) {
nodeLocation = ((WalkingRouteLine.WalkingStep) step).getEntrance().getLocation();
nodeTitle = ((WalkingRouteLine.WalkingStep) step).getInstructions();
} else if (step instanceof TransitRouteLine.TransitStep) {
nodeLocation = ((TransitRouteLine.TransitStep) step).getEntrance().getLocation();
nodeTitle = ((TransitRouteLine.TransitStep) step).getInstructions();
}
if (nodeLocation == null || nodeTitle == null) {
return;
}
//移动节点至中心
mBaidumap.setMapStatus(MapStatusUpdateFactory.newLatLng(nodeLocation));
// show popup
popupText = new TextView(Main3Activity.this);
popupText.setBackgroundResource(R.drawable.icon_st);
popupText.setTextColor(0xFF000000);
popupText.setText(nodeTitle);
mBaidumap.showInfoWindow(new InfoWindow(popupText,nodeLocation,0));
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
@Override
public void onGetWalkingRouteResult(WalkingRouteResult result) {
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(Main3Activity.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
}
if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
//起终点或途经点地址有岐义,通过以下接口获取建议查询信息
//result.getSuggestAddrInfo()
return;
}
if (result.error == SearchResult.ERRORNO.NO_ERROR) {
nodeIndex = -1;
mBtnPre.setVisibility(View.VISIBLE);
mBtnNext.setVisibility(View.VISIBLE);
route = result.getRouteLines().get(0);
WalkingRouteOverlay overlay = new MyWalkingRouteOverlay(mBaidumap);
mBaidumap.setOnMarkerClickListener(overlay);
routeOverlay = overlay;
overlay.setData(result.getRouteLines().get(0));
overlay.addToMap();
overlay.zoomToSpan();
}
}
@Override
public void onGetTransitRouteResult(TransitRouteResult result) {
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(Main3Activity.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
}
if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
//起终点或途经点地址有岐义,通过以下接口获取建议查询信息
//result.getSuggestAddrInfo()
return;
}
if (result.error == SearchResult.ERRORNO.NO_ERROR) {
nodeIndex = -1;
mBtnPre.setVisibility(View.VISIBLE);
mBtnNext.setVisibility(View.VISIBLE);
route = result.getRouteLines().get(0);
TransitRouteOverlay overlay = new MyTransitRouteOverlay(mBaidumap);
mBaidumap.setOnMarkerClickListener(overlay);
routeOverlay = overlay;
overlay.setData(result.getRouteLines().get(0));
overlay.addToMap();
overlay.zoomToSpan();
}
}
@Override
public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {
}
@Override
public void onGetDrivingRouteResult(DrivingRouteResult result) {
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(Main3Activity.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
}
if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
//起终点或途经点地址有岐义,通过以下接口获取建议查询信息
//result.getSuggestAddrInfo()
return;
}
if (result.error == SearchResult.ERRORNO.NO_ERROR) {
nodeIndex = -1;
mBtnPre.setVisibility(View.VISIBLE);
mBtnNext.setVisibility(View.VISIBLE);
route = result.getRouteLines().get(0);
DrivingRouteOverlay overlay = new MyDrivingRouteOverlay(mBaidumap);
routeOverlay = overlay;
mBaidumap.setOnMarkerClickListener(overlay);
overlay.setData(result.getRouteLines().get(0));
overlay.addToMap();
overlay.zoomToSpan();
}
}
@Override
public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {
}
@Override
public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {
}
//定制RouteOverly
private class MyDrivingRouteOverlay extends DrivingRouteOverlay {
public MyDrivingRouteOverlay(BaiduMap baiduMap) {
super(baiduMap);
}
@Override
public BitmapDescriptor getStartMarker() {
if (useDefaultIcon) {
return BitmapDescriptorFactory.fromResource(R.drawable.icon_st);
}
return null;
}
@Override
public BitmapDescriptor getTerminalMarker() {
if (useDefaultIcon) {
return BitmapDescriptorFactory.fromResource(R.drawable.icon_en);
}
return null;
}
}
private class MyWalkingRouteOverlay extends WalkingRouteOverlay {
public MyWalkingRouteOverlay(BaiduMap baiduMap) {
super(baiduMap);
}
@Override
public BitmapDescriptor getStartMarker() {
if (useDefaultIcon) {
return BitmapDescriptorFactory.fromResource(R.drawable.icon_st);
}
return null;
}
@Override
public BitmapDescriptor getTerminalMarker() {
if (useDefaultIcon) {
return BitmapDescriptorFactory.fromResource(R.drawable.icon_en);
}
return null;
}
}
private class MyTransitRouteOverlay extends TransitRouteOverlay {
public MyTransitRouteOverlay(BaiduMap baiduMap) {
super(baiduMap);
}
@Override
public BitmapDescriptor getStartMarker() {
if (useDefaultIcon) {
return BitmapDescriptorFactory.fromResource(R.drawable.icon_st);
}
return null;
}
@Override
public BitmapDescriptor getTerminalMarker() {
if (useDefaultIcon) {
return BitmapDescriptorFactory.fromResource(R.drawable.icon_en);
}
return null;
}
}
@Override
public void onMapClick(LatLng point) {
mBaidumap.hideInfoWindow();
}
@Override
public boolean onMapPoiClick(MapPoi poi) {
return false;
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}
@Override
protected void onDestroy() {
mSearch.destroy();
mMapView.onDestroy();
super.onDestroy();
}
}
布局xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main3Activity">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="起点:" />
<EditText
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="西二旗地铁站" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="终点:" />
<EditText
android:id="@+id/end"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="百度科技园" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginTop="5dip"
android:orientation="horizontal" >
<Button
android:id="@+id/drive"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:layout_weight="1.0"
android:onClick="SearchButtonProcess"
android:text="驾车搜索" />
<Button
android:id="@+id/transit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:layout_weight="1.0"
android:onClick="SearchButtonProcess"
android:text="公交搜索" />
<Button
android:id="@+id/walk"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:layout_weight="1.0"
android:onClick="SearchButtonProcess"
android:text="步行搜索" />
</LinearLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.baidu.mapapi.map.MapView
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignWithParentIfMissing="false"
android:layout_centerHorizontal="true"
android:layout_centerVertical="false"
android:layout_marginBottom="10dip" >
<Button
android:id="@+id/pre"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:layout_weight="1.0"
android:background="@drawable/icon_line_node"
android:onClick="nodeClick" />
<Button
android:id="@+id/next"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:layout_weight="1.0"
android:background="@drawable/icon_bus_station"
android:onClick="nodeClick" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
效果