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

自定義view畫轉盤

public class LinView extends View implements View.OnClickListener {

private Paint mPaint;
private int mX;
private int mY;
private boolean flag;

// private int[] ColorA=new int[]{Color.GRAY,Color.MAGENTA,Color.DKGRAY,Color.LTGRAY,Color.BLUE,Color.GREEN};
private RotateAnimation rotateAnimation;
private String[] TextA = new String[]{“事件一”, “事件二”, “事件三”, “事件四”, “事件五”, “事件六”};
private int color, color1, color2, color3, color4, color5;
private Integer[] ColorA = new Integer[]{color, color1, color2, color3, color4, color5};

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

public LinView(Context context, AttributeSet attrs) {
    this(context, attrs, -1);
}

public LinView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    //獲取螢幕資訊
    DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
    //getResources().getStringArray()
    //獲取螢幕的寬高
    int widthPixels = displayMetrics.widthPixels;
    int heightPixels = displayMetrics.heightPixels;
    //得到寬高的一半
    mX = widthPixels / 2;
    mY = heightPixels / 2;
    //初始化畫筆
    initPaint();
    //初始化旋轉
    initAmn();
    //設定點選事件
    this.setOnClickListener(this);

    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LinView);
    color = typedArray.getColor(R.styleable.LinView_title_color, 0);
    color1 = typedArray.getColor(R.styleable.LinView_title_color1, 0);
    color2 = typedArray.getColor(R.styleable.LinView_title_color2, 0);
    color3 = typedArray.getColor(R.styleable.LinView_title_color3, 0);
    color4 = typedArray.getColor(R.styleable.LinView_title_color4, 0);
    color5 = typedArray.getColor(R.styleable.LinView_title_color5, 0);


}

private void initAmn() {
    //獲取RotateAnimation物件
    rotateAnimation = new RotateAnimation(0, 360, mX, mY);
    //設定旋轉時間
    rotateAnimation.setDuration(2000);
rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
    @Override
    //開始
    public void onAnimationStart(Animation animation) {
        //Toast.makeText(getContext(),"開始",Toast.LENGTH_SHORT).show();
        if (getData!=null){
            getData.Data(animation);
        }

    }

    @Override
    //結束
    public void onAnimationEnd(Animation animation) {
       // Toast.makeText(getContext(),"結束",Toast.LENGTH_SHORT).show();
        if (getAD!=null){
            getAD.Ad(animation);
        }
    }

    @Override
    //重複
    public void onAnimationRepeat(Animation animation) {
        Toast.makeText(getContext(),"重複",Toast.LENGTH_SHORT).show();
    }
});

}

private void initPaint() {
    mPaint = new Paint();
    //設定畫筆顏色
    mPaint.setColor(Color.RED);
    //設定抗鋸齒
    mPaint.setAntiAlias(true);
    //設定畫筆樣式
    mPaint.setStyle(Paint.Style.FILL);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //平移原點到螢幕中心
    canvas.translate(mX, mY);
    //設定大圓對應的矩形
    RectF rectF = new RectF(-300, -300, 300, 300);
    //畫圓弧
  //  for (int i = 0; i < 6; i++) {
        //給圓弧設定顏色
        //  mPaint.setColor(ColorA[i]);


        if (color !=-1  ) {
            mPaint.setColor(color);
            canvas.drawArc(rectF, 60 * 0, 60, true, mPaint);
        }
        if (color1 !=-1  ) {
            mPaint.setColor(color1);
            canvas.drawArc(rectF, 60 * 1, 60, true, mPaint);
        }
        if (color2 !=-1  ) {
            mPaint.setColor(color2);
            canvas.drawArc(rectF, 60 * 2, 60, true, mPaint);
        }
        if (color3 !=-1  ) {
            mPaint.setColor(color3);
            canvas.drawArc(rectF, 60 * 3, 60, true, mPaint);
        }if (color4 !=-1  ) {
            mPaint.setColor(color4);
            canvas.drawArc(rectF, 60 * 4, 60, true, mPaint);
        }
        if (color5 !=-1 ) {
            mPaint.setColor(color5);
            canvas.drawArc(rectF, 60 * 5, 60, true, mPaint);
        }


        //畫圓弧
     //   canvas.drawArc(rectF, 60 * i, 60, true, mPaint);
//    }
    //給小圓設定顏色
    mPaint.setColor(Color.RED);
    //畫小圓
    canvas.drawCircle(0, 0, 100, mPaint);
    //設定文字大小及顏色
    mPaint.setTextSize(40);
    mPaint.setColor(Color.WHITE);
    //給小圓寫文字
    Rect rect = new Rect();
    mPaint.getTextBounds("start", 0, 5, rect);
    //獲取字型的寬高
    int width = rect.width();
    int height = rect.height();
    //設定字型zwl15901304107
    canvas.drawText("start", -width / 2, height / 2, mPaint);

    //給大圓寫文字
    RectF rectF1 = new RectF(-200, -200, 200, 200);
    for (int a = 0; a < 6; a++) {
        //設定字型位置
        Path path = new Path();
        path.addArc(rectF1, 60 * a + 15, 60);
        //設定圓弧字型
        canvas.drawTextOnPath(TextA[a], path, 0, 0, mPaint);
    }
}

//設定停止轉盤的方法
public void stopView() {
    flag = false;
    clearAnimation();
}

//設定開始轉盤的方法
public void startView() {
    flag = true;
    startAnimation(rotateAnimation);
}

//點選事件的方法
@Override
public void onClick(View v) {

    if (flag) {
        stopView();
    } else {


        startView();
    }
}


private GetData getData;

public void setGetData(GetData getData) {
    this.getData = getData;
}

public interface GetData{
    void Data(Animation str);
}

public void setGetAD(GetAD getAD) {
    this.getAD = getAD;
}

private GetAD getAD;
public  interface  GetAD{
    void  Ad(Animation ss);
}