轮盘抽奖制作

图上图片自己选择别的,仅参考
在这里插入图片描述

布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:background="@android:color/white"
    tools:context=".MainActivity">

    <com.example.demo.LotteryView
        android:id="@+id/lottery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:paddingLeft="30dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <android.support.v7.widget.AppCompatImageView
        android:id="@+id/node"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:src="@drawable/node"
        app:layout_constraintBottom_toBottomOf="@+id/lottery"
        app:layout_constraintEnd_toEndOf="@+id/lottery"
        app:layout_constraintStart_toStartOf="@+id/lottery"
        app:layout_constraintTop_toTopOf="@+id/lottery"/>


</android.support.constraint.ConstraintLayout>

color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
    <color name="red">#e1306c</color>
    <color name="gray">#b4aeb0</color>
    <color name="blue">#d0ea9a</color>
</resources>

CatfaceTextView

package com.example.text_1129.view;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import com.example.text_1129.R;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

@SuppressLint("AppCompatCustomView")
public class CatfaceTextView extends TextView {

    /**
     * 文本
     */
    private String mTitleText;
    /**
     * 文本的颜色
     */
    private int mTitleTextColor;
    /**
     * 文本的大小
     */
    private int mTitleTextSize;

    /**
     * 绘制时控制文本绘制的范围
     * @param context
     */
    private Rect mBound;
    private Paint mPaint;

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

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



    public CatfaceTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a=context.getTheme().obtainStyledAttributes(attrs,R.styleable.CatfaceTextView,defStyleAttr,0);
        int n=a.getIndexCount();
        for (int i=0;i<n;i++){

            int attr=a.getIndex(i);

            switch (attr){
                case R.styleable.CatfaceTextView_titleText:
                    mTitleText=a.getString(attr);
                    break;
                case R.styleable.CatfaceTextView_titleCoclor:
                    //默认颜色设置为黑色
                    mTitleTextColor=a.getColor(attr,Color.BLACK);
                    break;
                case R.styleable.CatfaceTextView_titleSize:
                    mTitleTextSize= a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
                    break;
            }
            this.setOnClickListener(new OnClickListener(){
                @Override public void onClick(View v)  {
                    mTitleText = randomText();
                    postInvalidate();
                }
            });

        }
        a.recycle();
        /**
         * 获得绘制文本的宽和高
         */
        mPaint=new Paint();
        mPaint.setTextSize(mTitleTextSize);
        mBound=new Rect();
        mPaint.getTextBounds(mTitleText,0,mTitleText.length(),mBound);
    }

    private String randomText() {
        Random random = new Random();
        Set<Integer> set = new HashSet<Integer>();
        while (set.size() < 4){
            int randomInt = random.nextInt(10);
            set.add(randomInt);
        }
        StringBuffer sb = new StringBuffer();
        for (Integer i : set)     {
            sb.append("" + i);
        }
        return sb.toString();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int width;
        int height;
        if (widthMode==MeasureSpec.EXACTLY){
            width=widthSize;
        }else {
            mPaint.setTextSize(mTitleTextSize);
            mPaint.getTextBounds(mTitleText,0,mTitleText.length(),mBound);
            float textWidth=mBound.width();

            int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());
            width=desired;
        }
        if (heightMode==MeasureSpec.EXACTLY){
            height=heightSize;
        }else {
            mPaint.setTextSize(mTitleTextSize);
            mPaint.getTextBounds(mTitleText,0,mTitleText.length(),mBound);
            float textHeight=mBound.height();
            int desired = (int) (getPaddingTop() + getPaddingBottom() + textHeight);
            height=desired;
        }
        super.onMeasure(width, height);

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(Color.YELLOW);
        canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint);

        mPaint.setColor(mTitleTextColor);
        canvas.drawText(mTitleText,getWidth() / 2 -mBound.width() / 2,getHeight()/2+mBound.height(),mPaint);

    }
}

猜你喜欢

转载自blog.csdn.net/qq_43580899/article/details/84635563