1. 程式人生 > >自定義view輪盤

自定義view輪盤

private RotateAnimation rotateAnimation;
private Paint mPaint;
private Paint strPaint;
private int mWidth;
private int mPadding;
private boolean isStart = false;
private RectF rectF;
private String str=“start”;
private String[] contents = new String[]{“1”,“2”,“3”,“4”,“5”,“6”};
private int viewX;
private int viewY;
private int mRawX;
private int mRawY;

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

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

public DiscView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initPaint();
    initAnim();
    setOnClickListener(this);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    mRawX= (int) event.getRawX();
    mRawY= (int) event.getRawY();
    return super.onTouchEvent(event);
}

private void initAnim() {
    //找到控制元件的中心點,
    double random = Math.random();
    rotateAnimation = new RotateAnimation(0f,360f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
    rotateAnimation.setRepeatCount(-1);
    rotateAnimation.setFillAfter(true);
}

private void initPaint() {
    strPaint = new Paint();
    strPaint.setStyle(Paint.Style.STROKE);
    strPaint.setAntiAlias(true);
    strPaint.setColor(Color.WHITE);
    strPaint.setStrokeWidth(5);

    mPaint = new Paint();
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setAntiAlias(true);
    mPaint.setColor(Color.WHITE);
    mPaint.setStrokeWidth(3);
}
//繪製自定義view的樣式
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(1000,1000);

    mWidth = getMeasuredWidth();
    mPadding = 5;
    initRect();
}

private void initRect() {
    rectF = new RectF(0,0,mWidth,mWidth);
}

@Override
protected void onDraw(Canvas canvas) {
    viewX = (int) this.getX();
    viewY = (int) this.getY();

    super.onDraw(canvas);
    //繪製圓
    mPaint.setStyle(Paint.Style.STROKE);
    canvas.drawCircle(mWidth/2,mWidth/2,mWidth/2-mPadding,mPaint);
    //繪製 6個橢圓
    mPaint.setStyle(Paint.Style.FILL);

    initArc(canvas);

    //繪製裡面的小圓
    mPaint.setColor(Color.RED);
    mPaint.setStyle(Paint.Style.FILL);
    canvas.drawCircle(mWidth/2,mWidth/2,50,mPaint);


    mPaint.setColor(Color.WHITE);
    mPaint.setTextSize(24);
    Rect rect = new Rect();
    mPaint.getTextBounds(str, 0, str.length(), rect);
    int strWidth = rect.width();//文字的寬度
    int textHeight = rect.height();//文字的高度
    canvas.drawText(str,mWidth/2-25+25-strWidth/2,mWidth/2+textHeight/2,mPaint);


}

private void initArc(Canvas canvas) {
    for(int i=0;i<6;i++){
        mPaint.setColor(colors[i]);
        canvas.drawArc(rectF,(i-1)*60+60,60,true,mPaint);
    }
    for(int i=0;i<6;i++){
        mPaint.setColor(Color.BLACK);
        Path path = new Path();
        path.addArc(rectF,(i-1)*60+60,60);
        canvas.drawTextOnPath(contents[i],path,60,60,mPaint);
    }
}
public  int[] colors = new int[]{Color.parseColor("#8EE5EE"),Color.parseColor("#FFD700"),Color.parseColor("#FFD39B"),Color.parseColor("#FF8247"),Color.parseColor("#FF34B3"),Color.parseColor("#F0E68C")};
@Override
public void onClick(View view) {
    if(!isStart){
        isStart = true;
        rotateAnimation.setDuration(800);
        rotateAnimation.setInterpolator(new LinearInterpolator());//不停頓
        rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                       isStart=false;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        startAnimation(rotateAnimation);
    }else{
        isStart = false;
        stopAnim();
    }
}

public void stopAnim() {
    clearAnimation();
}

/*Handler mHandler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what){
            case 1:
                setRoundDom();
                break;
        }
    }
};

private void setRoundDom() {
    double random = Math.random();
    RotateAnimation rotateAnimation = new RotateAnimation(-100, (float) (180 * random), 200, 200)
            ;
    rotateAnimation.setFillAfter(true);
    for(int i=0;i<1000l)
}*/