314_抽签器项目完整代码







抽签器项目完整代码




public class MainActivity extends AppCompatActivity {


    private LotterView lotterView;
    private PointerView pointerView;
    private EditText et1;
    private EditText et2;
    private EditText et3;
    private EditText et4;
    private EditText et5;
    private EditText et6;
    private EditText et7;
    private EditText et8;
    private ArrayList<String> list = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        lotterView = (LotterView) findViewById(R.id.lotterView);
        pointerView = (PointerView) findViewById(R.id.pointerView);


        for (int i = 1; i <= 5; i++) {
            String string = "选项" + i;
            list.add(string);
        }


        lotterView.setData(list);
        lotterView.invalidate();
    }


    public void set(View view) {
        AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.show();
        dialog.getWindow().setContentView(R.layout.dialog_setting);
        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);


        et1 = (EditText) dialog.findViewById(R.id.et1);
        et2 = (EditText) dialog.findViewById(R.id.et2);
        et3 = (EditText) dialog.findViewById(R.id.et3);
        et4 = (EditText) dialog.findViewById(R.id.et4);
        et5 = (EditText) dialog.findViewById(R.id.et5);
        et6 = (EditText) dialog.findViewById(R.id.et6);
        et7 = (EditText) dialog.findViewById(R.id.et7);
        et8 = (EditText) dialog.findViewById(R.id.et8);


        Button btn = (Button) dialog.findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.clear();
                handleEdittext(et1);
                handleEdittext(et2);
                handleEdittext(et3);
                handleEdittext(et4);
                handleEdittext(et5);
                handleEdittext(et6);
                handleEdittext(et7);
                handleEdittext(et8);
                lotterView.setData(list);
                lotterView.invalidate();
            }
        });


        setEdittextString();
    }


    private void setEdittextString() {
        for (int i = 1; i <= list.size(); i++) {
            getEdittext(i).setText(list.get(i - 1));
        }
    }


    private void handleEdittext(EditText editText) {
        String string = editText.getText().toString().trim();
        if (string != null && !string.isEmpty()) {
            list.add(string);
        }
    }


    private EditText getEdittext(int i) {
        switch (i) {
            case 1:
                return et1;
            case 2:
                return et2;
            case 3:
                return et3;
            case 4:
                return et4;
            case 5:
                return et5;
            case 6:
                return et6;
            case 7:
                return et7;
            case 8:
                return et8;
        }
        return null;
    }


    public void start(View view) {


        int size = list.size();


        if (size == 0) {
            return;
        }


        //计算每个角度
        float angle = 360 / size;


        //5个angle是一圈,最起码来个5圈
        double random = Math.random();
        double v = random * size + 1;
        float rotateAngle = 360f * size + angle * (float) v;




        //创建旋转动画
        RotateAnimation animation = new RotateAnimation(0, rotateAngle, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);




        //持续5秒,结束后保持状态
        animation.setDuration(5000);
        animation.setFillAfter(true);




        pointerView.startAnimation(animation);
    }
}






public class LotterView extends View {


    private ArrayList<String> list = new ArrayList<>();


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


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


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


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }


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


        int size = list.size();


        //获取宽高
        int width = getWidth();
        int height = getHeight();




        //初始化数据,半径,颜色,圆环宽度
        int radius = width / 2;
        int ringWidth = 15;
        int tempWidth = ringWidth / 2;




        //初始化数据
        float amount = size;
        float angle = 360f / amount;




        //创建画笔
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(ringWidth);
        paint.setColor(Color.BLACK);




        //创建画圆弧要用的RectF
        RectF rectF = new RectF(tempWidth, tempWidth, width - tempWidth, width - tempWidth);




        float angleSum = 0;
        for (int i = 1; i <= amount; i++) {
            canvas.drawArc(rectF, angleSum, angle, true, paint);




            double x = radius + getRoundX(radius / 3 * 2, i - 1, (int) amount, 0 + angle / 2);
            double y = radius + getRoundY(radius / 3 * 2, i - 1, (int) amount, 0 + angle / 2);




            Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            textPaint.setColor(Color.BLACK);
            int textSize = 50;
            textPaint.setTextSize(textSize);




            String str = list.get(i - 1);
            Rect rect = new Rect();
            textPaint.getTextBounds(str, 0, str.length(), rect);
            int strW = rect.width();
            int strH = rect.height();




            canvas.drawText(str, (float) x - (strW + ringWidth) / 2, (float) ((float) y - strW / 2 + textSize + ringWidth), textPaint);




            angleSum += angle;
        }
    }




    private double getRoundY(float r, int i, int n, float offset_angle) {
        return r * Math.sin(i * 2 * Math.PI / n + Math.PI / 180
                * offset_angle);
    }




    private double getRoundX(float r, int i, int n, float offset_angle) {
        return r * Math.cos(i * 2 * Math.PI / n + Math.PI / 180
                * offset_angle);
    }


    public void setData(ArrayList<String> list) {
        this.list = list;
    }
}










public class PointerView extends View {


    private ArrayList<String> list = new ArrayList<>();


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


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


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


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }


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




        //获取宽高
        int width = getWidth();
        int height = getHeight();
        int radius = 50;




        //画圆
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.RED);
        canvas.drawCircle(width / 2, width / 2, radius, paint);




        //画三角形
        Path path = new Path();
        path.moveTo(width / 2 - radius, width / 2);
        path.lineTo(width / 2 + radius, width / 2);
        path.lineTo(width / 2, width / 2 - radius * 2);
        path.close();




        canvas.drawPath(path, paint);
    }


    public void setData(ArrayList<String> list) {
        this.list = list;
    }
}








<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">


        <com.lich.lichlotter.LotterView
            android:id="@+id/lotterView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />


        <com.lich.lichlotter.PointerView
            android:id="@+id/pointerView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">


        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:onClick="set"
            android:text="设置"
            android:textSize="20sp" />


        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:onClick="start"
            android:text="开始"
            android:textSize="20sp" />


    </LinearLayout>
</LinearLayout>






<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <EditText
            android:id="@+id/et1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


        <EditText
            android:id="@+id/et2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


        <EditText
            android:id="@+id/et3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


        <EditText
            android:id="@+id/et4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


        <EditText
            android:id="@+id/et5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


        <EditText
            android:id="@+id/et6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


        <EditText
            android:id="@+id/et7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


        <EditText
            android:id="@+id/et8"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="确定" />


    </LinearLayout>
</ScrollView>









  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值