使用環境はAndroid Studioです。Hundlerを使って画像をレンダリングするようにプログラムを作りました。
しかし、赤文字エラーもなにもはかず安心していたのですが、実機nexus6で実行したら画面が起動されません。
何が原因か皆目見当もつかず困っています。
全体のコードが以下の通りとなっています。
コード真ん中が繰り返し処理で下部が加速度センサーの処理です。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,SensorEventListener {
SensorManager manager;
Sensor sensor;
private GoogleMap mMap = null;
private SupportMapFragment mapFragment;
private LatLng latlng;
private int width, height;
private double latitude, longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
manager = (SensorManager)getSystemService(SENSOR_SERVICE);
sensor = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// 皇居近辺の緯度経度
latitude = 35.68;
longitude = 139.76;
latlng = new LatLng(latitude, longitude);
// アイコン画像をマーカーに設定
setIcon(latitude, longitude);
}
private void setMarker(double latitude, double longitude) {
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latlng);
markerOptions.title("Marker");
mMap.addMarker(markerOptions);
// ズーム
zoomMap(latitude, longitude);
}
private void setIcon(final double latitude, final double longitude) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final float durationInMs = 3000;
// final Runnable runnable = new Runnable() {
handler.post(new Runnable() {
long elapsed;
float t;
float v;
public void run() {
elapsed = SystemClock.uptimeMillis() - start;
t = elapsed / durationInMs;
BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(R.drawable.data);
// 貼り付設定
GroundOverlayOptions overlayOptions = new GroundOverlayOptions();
overlayOptions.image(descriptor);
GroundOverlay overlay = mMap.addGroundOverlay(overlayOptions);
overlayOptions.anchor(ballX, ballY);
// ズーム
zoomMap(latitude, longitude);
// 透明度
overlay.setTransparency(0.0F);
if (t < 1) {
//1.初回実行
handler.postDelayed(this, 16);
}
}
});
}
private void zoomMap(double latitude, double longitude) {
// 表示する東西南北の緯度経度を設定
double south = latitude * (1 - 0.00005);
double west = longitude * (1 - 0.00005);
double north = latitude * (1 + 0.00005);
double east = longitude * (1 + 0.00005);
// LatLngBounds (LatLng southwest, LatLng northeast)
LatLngBounds bounds = LatLngBounds.builder()
.include(new LatLng(south, west))
.include(new LatLng(north, east))
.build();
width = getResources().getDisplayMetrics().widthPixels;
height = getResources().getDisplayMetrics().heightPixels;
// static CameraUpdate.newLatLngBounds(LatLngBounds bounds, int width, int height, int padding)
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, width, height, 0));
}
@Override
protected void onResume() {//センサーの値を入れる
super.onResume();
manager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);
}
private static final float ACCEL_WEIGHT = 1f;
private static final float ALPHA = 0.8f;
private float[] sensorValues;
private int ballX;
private int ballY;
@Override
public void onSensorChanged(SensorEvent event) {
if (sensorValues == null) {
sensorValues = new float[3];
sensorValues[0] = event.values[0];
sensorValues[1] = event.values[1];
sensorValues[2] = event.values[2];
return;
}
sensorValues[0] = sensorValues[0] * ALPHA + event.values[0] * (1f - ALPHA);
sensorValues[1] = sensorValues[1] * ALPHA + event.values[1] * (1f - ALPHA);
sensorValues[2] = sensorValues[2] * ALPHA + event.values[2] * (1f - ALPHA);
ballX += -sensorValues[0] * ACCEL_WEIGHT;
ballY += sensorValues[1] * ACCEL_WEIGHT;
}
@Override
protected void onPause() {
super.onPause();
manager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}