模仿微信摇一摇/百度地图显示
程序员文章站
2022-05-29 21:14:23
...
模仿微信摇一摇
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>
效果
百度地图实现
<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);
}
}
效果