文章目录

        • 2、xml 文件

####1、功能展示
canvas 画一个圆上有 旋转角度 的直线_第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

更多相关文章

  1. Android 自定义View之坐标系(一)
  2. android坐标系相关知识点
  3. Android中的坐标系统
  4. android中的坐标系以及获取坐标的方法
  5. Android OnTouchListener实时监听触点坐标,需要同时监听OnClickLi
  6. Android坐标系、视图坐标系与触控事件(MotionEvent)
  7. 超级炫酷!HTML5 Canvas火焰画笔动画
  8. 1 简介 在上一篇文章中我们对geopandas中的坐标参考系有了较为
  9. Python地信专题 | 基于geopandas的空间数据分析-坐标参考系篇

随机推荐

  1. thinkphp环境变量.env配置
  2. 关于php5.*后的时区问题 date_default_ti
  3. 使用没有实体类的本机SQL查询
  4. 如果作者是Post的作者那么做点什么?
  5. 数据库是存储我的Web应用程序数据的最佳
  6. How can clear screen in php cli (like
  7. PHP / MySQL - 有时会将空白条目添加到
  8. Laravel手把手系列教程之一环安装和环境
  9. php和django位于同一个lighttpd服务器上
  10. 如何加载json文件?