Tôi sắp xếp về cách thực hiện "la bàn cá nhân", nghĩa là la bàn trỏ đến vòng bi cụ thể thay vì tiêu chuẩn "cực bắc" ... thật không may, nỗ lực hiện tại của tôi đã xảy ra sai (không chỉ vào vòng bi đã cho). Nó cũng được kết nối với máy gia tốc để có thể tự điều chỉnh chính nó dựa trên cách người dùng đang chuyển.Xoay một ImageView giống như la bàn (với "cực bắc" được đặt ở nơi khác)
Dưới đây là nỗ lực hiện tại của tôi vào nó (các onSensorChanged()
-method mà cập nhật vào mũi tên):
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("?");
}
Và đây là phương pháp mà xoay 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);
}
Bất kỳ trợ giúp sẽ được nhiều đánh giá cao, vì tôi không hoàn toàn biết cách tiến hành. Các "bài đọc" tôi nhận được trong khi thử nó ra là hơi không chính xác và điểm sai hướng. Tôi có làm điều gì đó thực sự không, hay tôi chỉ có một thử nghiệm thực sự tồi tệ?
Tôi có thể hỏi liệu phương pháp này cập nhật hình ảnh quá CPU-costy? Khi tôi làm theo mã, màn hình cực kỳ chậm và tôi thậm chí không thể phóng to hoặc thu nhỏ. –
@ perfectionm1ng Tôi đã không nhận thấy bất cứ điều gì như thế, và đó là gần hai năm trở lại. Có thể là sự kết hợp của sự vật? :-) – ninetwozero