自定义View编写圆形轨迹图

本文介绍了一种使用Android自定义View实现动态圆环进度条的方法。通过代码演示了如何设置圆环的颜色、宽度及进度,并实现了随着进度变化而改变颜色的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;

import android.util.AttributeSet;

import android.view.View;

public class MyView extends View {


    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        paint = new Paint();
        mPaint = new Paint();
    }

    /**
     * 画笔对象的引用
     */
    private Paint paint,mPaint,paint2;
    int i = 0;
    /**
     * 圆环的宽度
     */
    private float roundWidth = 10;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        /**
         * 画最外层的大圆环
         */
        int centre = getWidth() / 2; // 获取圆心的x坐标
        int radius = (int) (centre - roundWidth / 2); // 圆环的半径
        paint.setColor(Color.CYAN);

        paint.setStyle(Paint.Style.FILL); // 设置空心
        paint.setStrokeWidth(roundWidth); // 设置圆环的宽度
        paint.setAntiAlias(true); // 消除锯齿
        canvas.drawCircle(centre, centre, radius, paint); // 画出圆环


        i += 1;
        /**
         * 画圆弧 ,画圆环的进度
         */

        // 设置进度是实心还是空心
        paint.setStrokeWidth(roundWidth); // 设置圆环的宽度

        RectF oval = new RectF(centre - radius, centre - radius, centre
                + radius, centre + radius); // 用于定义的圆弧的形状和大小的界限
        int degree = 360 * i / 360;







     if (degree%360 == 0) {
            int x = (int) (Math.random() * 16777777);
            String hex = Integer.toHexString(x);
            if (hex.length() == 6) {
                mPaint.setColor(Color.parseColor("#" + hex));


            } else {
                mPaint.setColor(Color.RED);

            }
            i=0;

    }

        canvas.drawArc(oval, 0, degree, true, mPaint);
        invalidate();

    }
}
布局如下:
<RelativeLayout xmlns:android=" http://schemas.android.com/apk/res/android"
    xmlns:tools=" http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <com.example.day30_shanview.MyView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
          />

</RelativeLayout>
Vimport android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;

import android.util.AttributeSet;

import android.view.View;

public class MyView extends View {


    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        paint = new Paint();
        mPaint = new Paint();
    }

    /**
     * 画笔对象的引用
     */
    private Paint paint,mPaint,paint2;
    int i = 0;
    /**
     * 圆环的宽度
     */
    private float roundWidth = 10;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        /**
         * 画最外层的大圆环
         */
        int centre = getWidth() / 2; // 获取圆心的x坐标
        int radius = (int) (centre - roundWidth / 2); // 圆环的半径
        paint.setColor(Color.CYAN);

        paint.setStyle(Paint.Style.FILL); // 设置空心
        paint.setStrokeWidth(roundWidth); // 设置圆环的宽度
        paint.setAntiAlias(true); // 消除锯齿
        canvas.drawCircle(centre, centre, radius, paint); // 画出圆环


        i += 1;
        /**
         * 画圆弧 ,画圆环的进度
         */

        // 设置进度是实心还是空心
        paint.setStrokeWidth(roundWidth); // 设置圆环的宽度

        RectF oval = new RectF(centre - radius, centre - radius, centre
                + radius, centre + radius); // 用于定义的圆弧的形状和大小的界限
        int degree = 360 * i / 360;







     if (degree%360 == 0) {
            int x = (int) (Math.random() * 16777777);
            String hex = Integer.toHexString(x);
            if (hex.length() == 6) {
                mPaint.setColor(Color.parseColor("#" + hex));


            } else {
                mPaint.setColor(Color.RED);

            }
            i=0;

    }

        canvas.drawArc(oval, 0, degree, true, mPaint);
        invalidate();

    }
}
布局如下:
<RelativeLayout xmlns:android=" http://schemas.android.com/apk/res/android"
    xmlns:tools=" http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <com.example.day30_shanview.MyView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
          />

</RelativeLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值