服务器之家

服务器之家 > 正文

Android实现抽奖转盘实例代码

时间:2021-03-05 15:45     来源/作者:Android开发网

本文详述了android抽奖程序的实现方法,程序为一个抽奖大转盘代码,里面定义了很多图形方法和动画。

实现主要功能的SlyderView.java源代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import android.app.Activity;
import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.EmbossMaskFilter;
import android.graphics.MaskFilter;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Paint.Style;
import android.graphics.PorterDuff.Mode;
import android.graphics.Path;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
public class SlyderView extends View{
  public SlyderView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context);
  }
  public SlyderView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
  }
  public SlyderView(Context context) {
    super(context);
    init(context);
  }
  /**
   * 屏幕宽度
   */
  private int screenW;
  /**
   * 屏幕的高度
   */
  private int screenH;
  /**
   * 分割的度数
   */
  private int [] drgrees = {20,50,40,90,70,40,50};
  /***
   * 分割的文字
   */
  private String [] strs = {"level1","level2","level3","level4","level5","level6","level7"};
  /**
   * 分割的颜色
   */
  private int [] colos = new int[] { 0xfed9c960, 0xfe57c8c8, 0xfe9fe558, 0xfef6b000, 0xfef46212, 0xfecf2911, 0xfe9d3011 };
  /**
   * 画笔
   */
  private Paint paint;
  /**
   * 文字的大小
   */
  private float textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 15, getResources().getDisplayMetrics());
  /**
   * 文字的颜色
   */
  private int textcolor = Color.WHITE;
  /**
   * 园的半径
   */
  private float radius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 100, getResources().getDisplayMetrics());
  /**
   * 画文字的距离
   */
  private float textdis = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 80, getResources().getDisplayMetrics());
  /**
   * 画箭头的大小
   */
  private float roketSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 10, getResources().getDisplayMetrics());
 
  private float initDegress = 0;
  /**
   * 圆心
   */
  private float centerX;
  /**
   * 圆心
   */
  private float centerY;
  /**
   * 立体边缘
   */
  private MaskFilter filter = new EmbossMaskFilter(new float[] { 1, 1, 1 },0.4f, 6, 3.5f);
  private MaskFilter outerFilter = new BlurMaskFilter(10, BlurMaskFilter.Blur.OUTER);
  private MaskFilter innerFilter = new BlurMaskFilter(10, BlurMaskFilter.Blur.INNER);
  @SuppressWarnings("deprecation")
  private void init(Context context){
    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Style.FILL);
    paint.setColor(Color.WHITE);
    screenW = ((Activity)context).getWindowManager().getDefaultDisplay().getWidth();
    screenH = ((Activity)context).getWindowManager().getDefaultDisplay().getHeight();
    int[] colores = new int[3];
    colores[0] = Color.rgb(0xfF, 0x99, 0x00);
    colores[1] = Color.rgb(0xff, 0xff, 0x00);
    colores[2] = Color.rgb(0xff, 0x99, 0x00);
    float[] positions = new float[3];
    positions[0] = 0.0f;
    positions[1] = 0.5f;
    positions[2] = 1.0f;
    gradient = new RadialGradient(centerX, centerY, radius/5, colores, positions, TileMode.CLAMP);
  }
  /**
   * 绘制三角箭头
   */
  private Path path = new Path();
  /**
   * 绘制矩形框
   */
  private RectF oval;
  /**
   * 外圆内阴影矩阵
   */
  private ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(new float[]{
      1,0,0,0,0,
      0,1,0,0,0,
      0,0,1,0,0,
      0,0,0,-1,255
  });
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    centerX = screenW/2;
    centerY = screenH/2;
    oval = new RectF(centerX-radius,centerY-radius,centerX+radius,centerY+radius);
    float start = 0;
    paint.setColor(Color.rgb(0xdd, 0xdd, 0xdd));
    paint.setAlpha(127);
    canvas.drawCircle(centerX, centerY, radius+10, paint);
    paint.setAlpha(255);
    //画扇形
    paint.setAntiAlias(true);
    for(int i=0;i<drgrees.length;i++){
      float sweepAngle = drgrees[i];
      float startAngle = start;
      paint.setColor(colos[i%colos.length]);
      canvas.drawArc(oval, startAngle, sweepAngle, true, paint);
      start += drgrees[i];
    }
    //画文字
    paint.setColor(textcolor);
    paint.setAntiAlias(true);
    paint.setTextSize(textSize);
    paint.setTextAlign(Paint.Align.RIGHT);
    start = 0;
    for(int i=0;i<drgrees.length;i++){
      canvas.save();
      canvas.rotate(start+drgrees[i]/2, centerX, centerY);
      canvas.drawText(strs[i], centerX+textdis, centerY, paint);
      canvas.restore();
      start += drgrees[i];
    }
    int saveCount = canvas.save();
    //画外层立体效果
    paint.setColorFilter(colorFilter);
    canvas.saveLayer(oval,paint,Canvas.ALL_SAVE_FLAG);
    paint.setColorFilter(null);
    canvas.drawARGB(255, 0, 0, 0);
    paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
    canvas.drawCircle(centerX, centerY, radius, paint);
    paint.setXfermode(null);
    paint.setMaskFilter(innerFilter);
    paint.setColor(Color.argb(0xff, 0, 0, 0));
    canvas.drawCircle(centerX, centerY, radius, paint);
    paint.setMaskFilter(null);
    canvas.restoreToCount(saveCount);
    //画内圆和内园效果
    canvas.save();
    paint.setColor(Color.argb(0xff, 0, 0, 0));
    paint.setAntiAlias(true);
    paint.setMaskFilter(outerFilter);
    canvas.rotate(initDegress, centerX, centerY);
    canvas.drawCircle(centerX, centerY, radius/3, paint);
    paint.setMaskFilter(null);
    paint.setColor(Color.WHITE);
    canvas.drawCircle(centerX, centerY, radius/3, paint);
    //画三角型叠加当箭头
    path.moveTo(centerX-radius/3, centerY);
    path.lineTo(centerX, centerY-radius/3-roketSize);
    path.lineTo(centerX+radius/3, centerY);
    path.close();
    canvas.drawPath(path, paint);
    canvas.restore();
    paint.setMaskFilter(filter);
    paint.setColor(Color.GREEN);
    paint.setShader(gradient);
    canvas.drawCircle(centerX, centerY, radius/5, paint);
    paint.setMaskFilter(null);
    paint.setShader(null);
    //重绘调整三角的指向达到滚动的效果,现实项目中可不能这样用的,效率太低下了,拆分View用动画完成滚动才是王道
    if(isRunning){
      if(initDegress>=360){
        initDegress = 0;
      }
      initDegress +=4;
      invalidate();
    }
    if(isStoping){
      if(initDegress<=360){
        initDegress+=4;
        invalidate();
      }else{
        if(initDegress-360<stop_degress){
          initDegress+=2;
          invalidate();
        }
      }
    }
  }
  private boolean isRunning = false;
  private boolean isStoping = false;
  private int stop_degress =90;
  /**
   * 渐变
   */
  private RadialGradient gradient;
  public void play(){
    isRunning = true;
    invalidate();
  }
  public void stop(int count){
    for(int i =0;i<=count;i++){
      if(i == count){
        stop_degress +=drgrees[i]/2;
      }else{
        stop_degress +=drgrees[i];
      }
    }
    isStoping = true;
    isRunning = false;
    invalidate();
  }
}
标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部