4
我正在使用簡單的指南針應用程序。我需要爲傳感器活動設置指南針視圖,以便選擇指南針視圖類。但它只是爲羅盤視圖繪製一個圓圈和一條線。我需要自定義這個視圖,通過替換這個圓圈和行與我自己的可繪製圖像有人請幫我解決這個問題。如何在android中製作自定義指南針視圖
我的視圖類
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class MyCompassView extends View {
private Paint paint;
private float position = 0;
public MyCompassView(Context context) {
super(context);
init();
}
private void init() {
paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(2);
paint.setTextSize(25);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.WHITE);
}
@Override
protected void onDraw(Canvas canvas) {
int xPoint = getMeasuredWidth()/2;
int yPoint = getMeasuredHeight()/2;
float radius = (float) (Math.max(xPoint, yPoint) * 0.6);
canvas.drawCircle(xPoint, yPoint, radius, paint);
canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), paint);
// 3.143 is a good approximation for the circle
canvas.drawLine(xPoint,
yPoint,
(float) (xPoint + radius
* Math.sin((double) (-position)/180 * 3.143)),
(float) (yPoint - radius
* Math.cos((double) (-position)/180 * 3.143)), paint);
canvas.drawText(String.valueOf(position), xPoint, yPoint, paint);
}
public void updateData(float position) {
this.position = position;
invalidate();
}
}
答案是多一點的鏈接不被認爲是很好的答案,並作爲評價更好。如果你想添加更多的細節,特別是代碼和如何實現解決方案,那麼這將是一個更好的答案。 – psubsee2003 2013-11-30 16:23:15
我在創建的應用程序中使用此指南針代碼,需要一些編輯幫助以使其適用於我的應用程序。你能檢查我的問題並嘗試幫忙嗎? http://stackoverflow.com/questions/22724671/how-to-point-a-compass-to-a-given-bearing-angle – Bob21 2014-03-31 04:42:56