android 自定义一个抽奖轮盘代码
时间: 2024-01-21 12:03:31 浏览: 104
android 自定义抽奖转盘
5星 · 资源好评率100%
以下是一个简单的抽奖轮盘的示例代码,仅供参考:
1. 自定义 View 类:
```
public class LuckyWheelView extends View {
private static final int DEFAULT_SIZE = 400;
private static final int DEFAULT_SECTOR_NUM = 6;
private static final int DEFAULT_START_ANGLE = 0;
private static final int DEFAULT_ROTATION_ANGLE = 360 * 5;
private int mSize;
private int mRadius;
private int mInnerRadius;
private int mSectorNum;
private int mStartAngle;
private int mRotationAngle;
private Paint mPaint;
private RectF mRectF;
private String[] mSectorColors;
private String mTextColor;
private String mCenterText;
private int mCenterTextSize;
private int mTextColorResId;
private int mCenterTextSizeResId;
public LuckyWheelView(Context context) {
this(context, null);
}
public LuckyWheelView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LuckyWheelView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.LuckyWheelView, 0, 0);
try {
mSectorNum = ta.getInteger(R.styleable.LuckyWheelView_sectorNum, DEFAULT_SECTOR_NUM);
mStartAngle = ta.getInteger(R.styleable.LuckyWheelView_startAngle, DEFAULT_START_ANGLE);
mRotationAngle = ta.getInteger(R.styleable.LuckyWheelView_rotationAngle, DEFAULT_ROTATION_ANGLE);
mSectorColors = new String[mSectorNum];
mSectorColors[0] = ta.getString(R.styleable.LuckyWheelView_sectorColor1);
mSectorColors[1] = ta.getString(R.styleable.LuckyWheelView_sectorColor2);
mSectorColors[2] = ta.getString(R.styleable.LuckyWheelView_sectorColor3);
mSectorColors[3] = ta.getString(R.styleable.LuckyWheelView_sectorColor4);
mSectorColors[4] = ta.getString(R.styleable.LuckyWheelView_sectorColor5);
mSectorColors[5] = ta.getString(R.styleable.LuckyWheelView_sectorColor6);
mTextColor = ta.getString(R.styleable.LuckyWheelView_textColor);
mCenterText = ta.getString(R.styleable.LuckyWheelView_centerText);
mCenterTextSize = ta.getDimensionPixelSize(R.styleable.LuckyWheelView_centerTextSize, getResources().getDimensionPixelSize(R.dimen.default_center_text_size));
mTextColorResId = ta.getResourceId(R.styleable.LuckyWheelView_textColor, R.color.black);
mCenterTextSizeResId = ta.getResourceId(R.styleable.LuckyWheelView_centerTextSize, R.dimen.default_center_text_size);
} finally {
ta.recycle();
}
init();
}
private void init() {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mRectF = new RectF();
mPaint.setTextSize(mCenterTextSize);
mPaint.setTextAlign(Paint.Align.CENTER);
mTextColor = getResources().getString(mTextColorResId);
mCenterTextSize = getResources().getDimensionPixelSize(mCenterTextSizeResId);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int sizeSpec = MeasureSpec.getSize(widthMeasureSpec);
int mode = MeasureSpec.getMode(widthMeasureSpec);
if (mode == MeasureSpec.EXACTLY) {
mSize = sizeSpec;
} else {
mSize = DEFAULT_SIZE;
if (mode == MeasureSpec.AT_MOST && sizeSpec < mSize) {
mSize = sizeSpec;
}
}
mRadius = mSize / 2;
mInnerRadius = mRadius / 2;
setMeasuredDimension(mSize, mSize);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawSectors(canvas);
drawCenterText(canvas);
}
private void drawSectors(Canvas canvas) {
float sweepAngle = 360f / mSectorNum;
float startAngle = mStartAngle;
for (int i = 0; i < mSectorNum; i++) {
mPaint.setColor(Color.parseColor(mSectorColors[i]));
mRectF.set(0, 0, mSize, mSize);
canvas.drawArc(mRectF, startAngle, sweepAngle, true, mPaint);
startAngle += sweepAngle;
}
}
private void drawCenterText(Canvas canvas) {
mPaint.setColor(Color.parseColor(mTextColor));
mPaint.setTextSize(mCenterTextSize);
canvas.drawText(mCenterText, mRadius, mRadius + mCenterTextSize / 2, mPaint);
}
public void startRotation() {
ValueAnimator animator = ValueAnimator.ofInt(0, mRotationAngle);
animator.setDuration(5000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int value = (int) animation.getAnimatedValue();
mStartAngle = value % 360;
invalidate();
}
});
animator.start();
}
public void setCenterText(String centerText) {
mCenterText = centerText;
invalidate();
}
}
```
2. 在 values 文件夹下创建 attrs.xml 文件:
```
<resources>
<declare-styleable name="LuckyWheelView">
<attr name="sectorNum" format="integer" />
<attr name="startAngle" format="integer" />
<attr name="rotationAngle" format="integer" />
<attr name="sectorColor1" format="string" />
<attr name="sectorColor2" format="string" />
<attr name="sectorColor3" format="string" />
<attr name="sectorColor4" format="string" />
<attr name="sectorColor5" format="string" />
<attr name="sectorColor6" format="string" />
<attr name="textColor" format="string" />
<attr name="centerText" format="string" />
<attr name="centerTextSize" format="dimension" />
</declare-styleable>
</resources>
```
3. 在 layout 文件中引用自定义 View:
```
<com.example.luckywheel.LuckyWheelView
android:id="@+id/luckyWheelView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:centerText="Start"
app:centerTextSize="@dimen/default_center_text_size"
app:rotationAngle="1800"
app:sectorColor1="#FFC107"
app:sectorColor2="#9C27B0"
app:sectorColor3="#2196F3"
app:sectorColor4="#009688"
app:sectorColor5="#FF5722"
app:sectorColor6="#607D8B"
app:sectorNum="6"
app:startAngle="0"
app:textColor="@color/black" />
```
4. 在 Activity 或 Fragment 中启动轮盘转动:
```
LuckyWheelView luckyWheelView = findViewById(R.id.luckyWheelView);
luckyWheelView.startRotation();
```
阅读全文