一个简单的自定义View 大风车

先上效果图
这里写图片描述

附代码

package view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import utils.DensityUtils;

//风车View

public class WindmillView extends View {

    //中心点坐标
    private Paint mCirclePaint;
    private int mCenterX;
    private int mCenterY;
    private int mRadius;

    private int FuZhuR;

    private Paint mArcPaint;//底座view
    private RectF mArcRect;


    private int mSpeed;
    private boolean isStart;

    private int mStartAngel = 30;
    private int mStartAngel2 = 150;
    private int mStartAngel3 = 270;




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

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

    public WindmillView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {
        //中心点坐标
        mCirclePaint = new Paint();
        mCirclePaint.setDither(true);
        mCirclePaint.setColor(Color.WHITE);
        mCirclePaint.setStyle(Paint.Style.FILL);


        //底座paint
        mArcPaint = new Paint();
        mArcPaint.setColor(Color.WHITE);
        mArcPaint.setStyle(Paint.Style.FILL);
        mArcPaint.setAntiAlias(true);
        mArcPaint.setDither(true);

        mArcPaint.setStrokeWidth(DensityUtils.dip2px(this.getContext(),1));

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width;
        int height;
        int defaultW = DensityUtils.dip2px(this.getContext(), 300);
        int defaultH = DensityUtils.dip2px(this.getContext(), 300);

        int measureW = MeasureSpec.getSize(widthMeasureSpec);
        int measureH = MeasureSpec.getSize(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {
            width = measureW;
        } else {
            width = defaultW;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = measureH;
        } else {
            height = defaultH;
        }

        setMeasuredDimension(width, height);

        mCenterX = getHorizontalSpace() / 2;
        mCenterY = getVerticalSpace() / 2;
        FuZhuR = Math.min(mCenterX, mCenterY);
        mRadius = DensityUtils.dip2px(this.getContext(), 3);


    }

    private int getHorizontalSpace() {
        return getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
    }

    private int getVerticalSpace() {
        return getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawCenterCircle(canvas);
        drawArcView(canvas);
        drawItems(canvas);
    }

    private void drawItems(Canvas canvas) {

        int tempStartAngel1 = mStartAngel;

        int tempStartAngel2 = mStartAngel2;

        int tempStartAngel3 = mStartAngel3;


        double startX = getXpoint(FuZhuR - DensityUtils.dip2px(this.getContext(), 50), tempStartAngel1);
        double startY = getYpoint(FuZhuR - DensityUtils.dip2px(this.getContext(), 50), tempStartAngel1);

        canvas.drawLine((float) startX, (float) startY, mCenterX, mCenterY, mArcPaint);


        double startX1 = getXpoint(FuZhuR - DensityUtils.dip2px(this.getContext(), 50), tempStartAngel2);
        double startY1 = getYpoint(FuZhuR - DensityUtils.dip2px(this.getContext(), 50), tempStartAngel2);
        canvas.drawLine((float) startX1, (float) startY1, mCenterX, mCenterY, mArcPaint);


        double startX2 = getXpoint(FuZhuR - DensityUtils.dip2px(this.getContext(), 50), tempStartAngel3);
        double startY2 = getYpoint(FuZhuR - DensityUtils.dip2px(this.getContext(), 50), tempStartAngel3);
        canvas.drawLine((float) startX2, (float) startY2, mCenterX, mCenterY, mArcPaint);

        if (isStart) {

            mStartAngel += mSpeed;
            mStartAngel2 += mSpeed;
            mStartAngel3 += mSpeed;
            if (mStartAngel > 390) {
                mStartAngel = 30;
            }
            if (mStartAngel2 > 510) {
                mStartAngel2 = 150;
            }
            if (mStartAngel3 > 630) {
                mStartAngel3 = 270;
            }
            invalidate();
        }
    }


    private void drawCenterCircle(Canvas canvas) {
        canvas.drawCircle(mCenterX, mCenterY, mRadius, mCirclePaint);
    }

    private void drawArcView(Canvas canvas) {
        if (mArcRect == null) {
            mArcRect = new RectF(getHorizontalSpace() / 2 - FuZhuR, getVerticalSpace() / 2 - FuZhuR, getHorizontalSpace() / 2 + FuZhuR, getVerticalSpace() / 2 + FuZhuR);
        }
        canvas.drawArc(mArcRect, 82.5f, 15, true, mArcPaint);
    }

    //根据半径和角度获得坐标
    private double getXpoint(double radius, double angel) {
        double rad = angel * 2 * Math.PI / 360;//弧度
        return (mCenterX + radius * Math.cos(rad));
    }

    private double getYpoint(double radius, double angel) {
        double rad = angel * 2 * Math.PI / 360;//弧度
        return (mCenterY + radius * Math.sin(rad));
    }

    public void startFly(int speed) {
        mSpeed = speed;
        isStart = true;
        invalidate();

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值