1. 程式人生 > >Android轉盤抽獎的簡單實現

Android轉盤抽獎的簡單實現

public class ZhuanPan2 extends View implements View.OnClickListener{
    private Paint mPaint;
    private int mwidth;
    private int mpidding;
    private boolean isstart=false;
    private RotateAnimation mRotateAnimation;
    private RectF mRectF;
    private String[] waitext=new String[]{"蘋果","橘子","香蕉","荔枝","榴蓮","葡萄"};
    private int[] colors=new int[]{Color.parseColor("#8EE5EE"),Color.parseColor("#FFD700"),Color.parseColor("#8EE5EE"),Color.parseColor("#FFD700"),Color.parseColor("#8EE5EE"),Color.parseColor("#FFD700")};
    private String neitext="start";
    public ZhuanPan2(Context context) {
        super(context);
    }

    public ZhuanPan2(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mypiant();
        myRotateAnimation();
        setOnClickListener(this);
    }


    public ZhuanPan2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void mypiant() {
        mPaint=new Paint();
        mPaint.setStrokeWidth(3);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.STROKE);
    }
    private void myRotateAnimation() {
        mRotateAnimation=new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f);
        mRotateAnimation.setFillAfter(true);
        mRotateAnimation.setRepeatCount(-1);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(300,300);
        mwidth = getMeasuredWidth();
        mpidding=5;
        mRectF=new RectF(0,0,mwidth,mwidth);
    }

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

        canvas.drawCircle(mwidth/2,mwidth/2,mwidth/2-mpidding,mPaint);
        mPaint.setStyle(Paint.Style.FILL);

        inityuan(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(neitext,0,neitext.length(),rect);
        int width = rect.width();
        int height = rect.height();

        canvas.drawText(neitext,mwidth/2-25+25-width/2,mwidth/2+height/2,mPaint);
    }

    private void inityuan(Canvas canvas) {
        for (int i=0;i<6;i++){
            mPaint.setColor(colors[i]);
            canvas.drawArc(mRectF,(i-1)*60+60,60,true,mPaint);
        }
        for (int i=0;i<6;i++){
            mPaint.setColor(Color.BLACK);
            Path path=new Path();
            path.addArc(mRectF,(i-1)*60+60,60);
            canvas.drawTextOnPath(waitext[i],path,60,60,mPaint);
        }
    }

    @Override
    public void onClick(View v) {
        if (!isstart){
            isstart=true;
            mRotateAnimation.setDuration(1000);
            mRotateAnimation.setInterpolator(new LinearInterpolator());
            startAnimation(mRotateAnimation);
        }else {
            isstart=false;
            clearAnimation();
        }
    }
}

然後寫好這個類之後只需在佈局檔案中設定就行,把寬高設定成自適應否則會出現轉盤控制元件圍繞佈局公轉

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolBar"
        android:background="#987"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v7.widget.Toolbar>
   <mrcao.dark.rotarydraw.ZhuanPan2
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />

</LinearLayout>
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="textColorSearchUrl">#f00</item>
    </style>

</resources>