canvas 画一个圆上有 旋转角度 的直线
16lz
2021-01-23
文章目录
- 2、xml 文件
####1、功能展示
2、xml 文件
activity_main.xml
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.menglux.mycanvasline.MainActivity">
####3、代码文件
LineView.java文件
public class LineView extends View { private String TAG = "LineView: "; private Paint paintCircle; //圆的画笔 private Paint paintShortLine; // 短线的画笔 private Paint paintLongLine; //长线的画笔 private int radius = 200; //圆的半径 private int width = 400 ; //x 中心位置 private int height = 400 ; //y 中心位置 @SuppressLint("ResourceAsColor") public LineView(Context context) { super(context); } public LineView(Context context, AttributeSet attrs) { super(context, attrs, 0); } //在构造函数里初始化 数据 public LineView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); //新建 圆的画笔 paintCircle = new Paint(); paintCircle.setAlpha(300); //透明度设置 paintCircle.setColor(getResources().getColor(R.color.colorPrimary)); //画笔颜色设置 paintCircle.setAntiAlias(true); //抗齿距设置 paintCircle.setStyle(Paint.Style.STROKE); //设置画笔样式 空心 paintCircle.setStrokeWidth(10); //设置画笔宽度 //新建 短线的画笔 paintShortLine = new Paint(); paintShortLine.setAlpha(300); //透明度设置 paintShortLine.setColor(getResources().getColor(R.color.colorAccent)); //画笔颜色设置 paintShortLine.setAntiAlias(true); //抗齿距设置 paintShortLine.setStyle(Paint.Style.STROKE); //设置画笔样式 空心 paintShortLine.setStrokeWidth(10); //设置画笔宽度 //新建 长线的画笔 paintLongLine = new Paint(); paintLongLine.setAlpha(300); //透明度设置 paintLongLine.setColor(getResources().getColor(R.color.color)); //画笔颜色设置 paintLongLine.setAntiAlias(true); //抗齿距设置 paintLongLine.setStyle(Paint.Style.STROKE); //设置画笔样式 空心 paintLongLine.setStrokeWidth(10); //设置画笔宽度 } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); drawCircle(canvas); //画圆 drawShortLine(canvas); //画短线 drawLongLine(canvas); //画长线 } private void drawLongLine(Canvas canvas) { //先保存画布现在的状态 canvas.save(); int startX = width; int startY = height - radius + 100;//y坐标即园中心点的y坐标-半径 int stopX = startX; int stopY1 = startY - 300;//整点处的线长度为 300 canvas.rotate(30, width, height);//以圆中心进行旋转 canvas.drawLine(startX, startY, stopX, stopY1, paintLongLine);//绘制长的线 //绘制完后,记得把画布状态复原 canvas.restore(); } private void drawShortLine(Canvas canvas) { //先保存画布现在的状态 canvas.save(); int startX = width; int startY = height - radius + 50;//y坐标即园中心点的y坐标-半径 int stopX = startX; int stopY1 = startY - 100;//整点处的线长度为 100 canvas.rotate(222, width, height);//以圆中心进行旋转 222度 canvas.drawLine(startX, startY, stopX, stopY1, paintShortLine);//绘制短线 //绘制完后,记得把画布状态复原 canvas.restore(); } /** * 绘圆形部分 * @param canvas */ private void drawCircle(Canvas canvas) { //获得圆的圆点坐标 int x = width; int y = height; canvas.drawCircle(x, y, radius, paintCircle); }}
demo下载
文件参考:
Android 2D绘图(Canvas+paint)详解
https://www.2cto.com/kf/201610/557341.html
Android之canvas详解
https://blog.csdn.net/whuhan2013/article/details/51404737
Android 自定义绘制圆形进度条
https://blog.csdn.net/baidu_23478311/article/details/46238973
更多相关文章
- Android 自定义View之坐标系(一)
- android坐标系相关知识点
- Android中的坐标系统
- android中的坐标系以及获取坐标的方法
- Android OnTouchListener实时监听触点坐标,需要同时监听OnClickLi
- Android坐标系、视图坐标系与触控事件(MotionEvent)
- 超级炫酷!HTML5 Canvas火焰画笔动画
- 1 简介 在上一篇文章中我们对geopandas中的坐标参考系有了较为
- Python地信专题 | 基于geopandas的空间数据分析-坐标参考系篇