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">

    <LinearLayout

        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <com.example.menglux.mycanvasline.LineView
            android:id="@+id/view_id"
            android:layout_width="800dp"
            android:layout_height="800dp"
            android:gravity="center">

        </com.example.menglux.mycanvasline.LineView>

    </LinearLayout>
</LinearLayout>

####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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值