Android studio制作旋转直线
时间: 2023-11-24 15:08:22 浏览: 29
要制作旋转直线,您可以使用Android Studio中的Canvas和Paint类来绘制直线,并使用旋转变换旋转它。
下面是一个示例代码,可以在Activity的onCreate方法中使用:
```java
public class MainActivity extends AppCompatActivity {
private CustomView mCustomView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCustomView = new CustomView(this);
setContentView(mCustomView);
}
private class CustomView extends View {
private Paint mPaint;
private float mRotationAngle;
public CustomView(Context context) {
super(context);
mPaint = new Paint();
mPaint.setStrokeWidth(10f);
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Paint.Style.STROKE);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int cx = getWidth() / 2;
int cy = getHeight() / 2;
int length = 200;
canvas.rotate(mRotationAngle, cx, cy);
canvas.drawLine(cx - length / 2, cy, cx + length / 2, cy, mPaint);
}
public void setRotationAngle(float angle) {
mRotationAngle = angle;
invalidate();
}
}
}
```
此代码创建了一个自定义视图CustomView,它绘制蓝色的直线,并使用旋转变换将其旋转。在Activity的onCreate方法中,将该视图设置为ContentView。您可以使用setRotationAngle方法来更改旋转角度。
希望这可以帮助您开始制作旋转直线!