我无法旋转画布。我在里面画了一个圆和一个圆弧。
我正在尝试沿手机摄像头旋转的方向旋转。代码如下。
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
值来旋转画布。但是画布没有旋转或更新。
我错过了什么?