キャンバスを回転できません。その中に円と弧を描きました。
電話のカメラが回転する方向に回転しようとしています。コードは次のとおりです。
ViewSearch.java
public void onSensorChanged(SensorEvent event) {
// RelativeLayout rl = (RelativeLayout)
// findViewById(R.layout.visualsearch);
aziumth_angle = event.values[0]; // degrees of rotation around the
// z axis
pitch_angle = event.values[1]; // degrees of rotation around the x
// axis
float roll_angle = event.values[2]; // degrees of rotation around the y
// axis
TextView txtangle = (TextView) findViewById(R.id.txtangle);
txtangle.setText("Z : " + aziumth_angle + ", X : " + pitch_angle
+ ", Y : " + roll_angle);
CustomView cv = new CustomView(this);
cv.updateData(aziumth_angle);
}
CustomView.java
public class CustomView extends View {
Paint paint;
Paint paintarc;
public float position;
private int rotationAngle = 0;
private int arcSweepAngle = 45;
public float azimuth;
public float startAngle;
public CustomView(Context context) {
super(context);
setWillNotDraw(false);
startdraw();
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
setWillNotDraw(false);
startdraw();
}
public CustomView(Context context, AttributeSet attrs, int defstyle) {
super(context, attrs, defstyle);
setWillNotDraw(false);
startdraw();
}
public void startdraw() {
paint = new Paint();
paintarc = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(2);
paint.setTextSize(25);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.WHITE);
Shader gradient = new SweepGradient(0, getMeasuredHeight() / 2,
Color.RED, Color.WHITE);
paintarc.setShader(gradient);
paintarc.setStrokeWidth(43);
}
@SuppressLint("NewApi")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int xPoint = getMeasuredWidth() / 2;
int yPoint = getMeasuredHeight() / 2;
float radius = (float) (Math.max(xPoint, yPoint) * 0.6); // why is this?
canvas.drawCircle(xPoint, yPoint, radius, paint);
// 3.143 is a good approximation for the circle
float width = (float) getWidth();
float height = (float) getHeight();
// determine center point to the circle.
float center_x;
float center_y;
center_x = width / 2;
center_y = height / 2;
VisualView vv = null;
azimuth = vv.aziumth_angle;
startAngle = vv.pitch_angle;
final RectF oval = new RectF();
oval.set(center_x - radius,center_y - radius,center_x + radius,center_y + radius);
canvas.drawArc(oval, 0, 60, true, paintarc);
canvas.translate(0, startAngle);
canvas.rotate(-position * 360 / (2 * 3.14159f), center_x, center_y);
}
public void updateData(float position) {
this.position = position;
invalidate();
}
}
センサーの方向が変更されると、値を取得し、azimuth
値を渡してキャンバスを回転させようとしていazimuth
ます。しかし、キャンバスは回転も更新もしていません。
私は何が欠けていますか?