Sono perplesso su come implementare una "bussola personale", ovvero una bussola che punta a un cuscinetto specifico anziché al "polo nord" standard ... sfortunatamente, il mio attuale tentativo è venuto fuori sbagliato (non indica il rilevamento dato). È anche collegato all'acceleratore per essere in grado di adattarsi dinamicamente in base al modo in cui l'utente sta girando.Rotazione di un ImageView come una bussola (con il "polo nord" impostato altrove)
Ecco il mio attuale tentativo di essa (la onSensorChanged()
-Metodo che aggiorna la freccia):
public void onSensorChanged(SensorEvent event) {
// If we don't have a Location, we break out
if (LocationObj == null) return;
float azimuth = event.values[0];
float baseAzimuth = azimuth;
GeomagneticField geoField = new GeomagneticField(Double
.valueOf(LocationObj.getLatitude()).floatValue(), Double
.valueOf(LocationObj.getLongitude()).floatValue(),
Double.valueOf(LocationObj.getAltitude()).floatValue(),
System.currentTimeMillis());
azimuth += geoField.getDeclination(); // converts magnetic north into true north
//Correct the azimuth
azimuth = azimuth % 360;
//This is where we choose to point it
float direction = azimuth + LocationObj.bearingTo(destinationObj);
rotateImageView(arrow, R.drawable.arrow, direction);
//Set the field
if(baseAzimuth > 0 && baseAzimuth < 45) fieldBearing.setText("S");
else if(baseAzimuth >= 45 && baseAzimuth < 90) fieldBearing.setText("SW");
else if(baseAzimuth > 0 && baseAzimuth < 135) fieldBearing.setText("W");
else if(baseAzimuth > 0 && baseAzimuth < 180) fieldBearing.setText("NW");
else if(baseAzimuth > 0 && baseAzimuth < 225) fieldBearing.setText("N");
else if(baseAzimuth > 0 && baseAzimuth < 270) fieldBearing.setText("NE");
else if(baseAzimuth > 0 && baseAzimuth < 315) fieldBearing.setText("E");
else if(baseAzimuth > 0 && baseAzimuth < 360) fieldBearing.setText("SE");
else fieldBearing.setText("?");
}
Ed ecco il metodo che fa ruotare l'ImageView (rotateImageView()
):
private void rotateImageView(ImageView imageView, int drawable, float rotate) {
// Decode the drawable into a bitmap
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
drawable);
// Get the width/height of the drawable
DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = bitmapOrg.getWidth(), height = bitmapOrg.getHeight();
// Initialize a new Matrix
Matrix matrix = new Matrix();
// Decide on how much to rotate
rotate = rotate % 360;
// Actually rotate the image
matrix.postRotate(rotate, width, height);
// recreate the new Bitmap via a couple conditions
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true);
//BitmapDrawable bmd = new BitmapDrawable(rotatedBitmap);
//imageView.setImageBitmap(rotatedBitmap);
imageView.setImageDrawable(new BitmapDrawable(getResources(), rotatedBitmap));
imageView.setScaleType(ScaleType.CENTER);
}
Qualsiasi aiuto sarebbe essere molto apprezzato, in quanto non so come procedere. Le "letture" che sto ricevendo mentre lo provano sono alquanto imprecise e indicano la direzione sbagliata. Sto facendo qualcosa di veramente fuori, o ho appena fatto una brutta prova?
Posso chiedere se questo metodo di aggiornamento dell'immagine è troppo CPU-costy? Come quando seguo il codice, lo schermo è estremamente lento e non riesco nemmeno a ingrandire o ridurre. –
@ perfectionm1ng Non ho notato niente del genere, e questo era quasi due anni fa. Potrebbe essere la combinazione di cose? :-) – ninetwozero