自定义View之交替圆环

效果:

这里写图片描述


代码:

attr

<resources>

    <declare-styleable name="MyView02">
            <attr name="secondColor" format="color"></attr>
        <attr name="firstColor" format="color"></attr>
        <attr name="speed" format="integer"></attr>
        <attr name="radius" format="dimension"></attr>
    </declare-styleable>

</resources>

main_activity

<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"
    tools:context="com.example.zidingyiview.MainActivity" >

   <com.example.zidingyiview.MyView02
       android:layout_centerInParent="true"
       android:layout_width="400dp"
       android:layout_height="400dp"
       android:id="@+id/view01"/>


</RelativeLayout>

MyView02

package com.example.zidingyiview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

public class MyView02 extends View {

    Paint mPaint1;
    Paint mPaint2;
    private int color1;
    private int color2;
    int radius;
    int speed;
    int progress;
    boolean isNext;

    boolean flag = true;

    // 防止类存泄漏
    // 在程序退出时,调用该方法结束线程
    public void closeView() {
        flag = false;
    }

    public MyView02(Context context) {
        this(context, null);
    }

    public MyView02(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyView02(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        // 拿到自定义属性
        TypedArray ta = context.obtainStyledAttributes(attrs,
                R.styleable.MyView02, defStyleAttr, 0);
        color1 = ta.getColor(R.styleable.MyView02_firstColor, Color.GREEN);
        color2 = ta.getColor(R.styleable.MyView02_secondColor, Color.BLUE);
        radius = ta.getDimensionPixelSize(R.styleable.MyView02_radius,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,
                        100, getResources().getDisplayMetrics()));
        speed = ta.getInteger(R.styleable.MyView02_speed, 10);
        ta.recycle();


        // 设置画笔
        mPaint1 = new Paint();
        mPaint1.setStyle(Style.STROKE);
        mPaint1.setStrokeWidth(radius/2);
        mPaint1.setAntiAlias(true);

        mPaint2 = new Paint();
        mPaint2.setStyle(Style.STROKE);
        mPaint2.setStrokeWidth(radius/2);
        mPaint2.setAntiAlias(true);

        mPaint1.setColor(color1);
        mPaint2.setColor(color2);


        // 开启一个线程来画圈圈
        new Thread(new Runnable() {

            @Override
            public void run() {
                while (flag) {
                    try {
                        Thread.sleep(speed);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    progress += 1;
                    if (progress >= 360) {
                        progress = 0;
                        if (!isNext) {
                            isNext = true;
                        } else {
                            isNext = false;
                        }
                    }
                    postInvalidate();
                }

            }
        }).start();

    }

    @SuppressLint("NewApi")
    @Override
    protected void onDraw(Canvas canvas) {
        int centre = getWidth() / 2;
        RectF oval = new RectF(centre - radius, centre - radius, centre
                + radius, centre + radius);
        canvas.drawArc(oval, 0, 360, false, mPaint2);
        if (isNext) {
            canvas.drawArc(oval, 0, 360, false, mPaint1);
            canvas.drawArc(oval, 0, progress, false, mPaint2);
        } else {
            canvas.drawArc(oval, 0, progress, false, mPaint1);
        }
    }

}

转载于:http://blog.csdn.net/lmj623565791/article/details/24500107

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值