闲来无事,做了一个简单的抽奖转盘的ui实现,供大家参考
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
|
package com.microchange.lucky; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.rectf; import android.util.attributeset; import android.util.log; import android.view.motionevent; import android.view.view; public class halfcircle extends view { private paint paint; private rectf oval; private float startangle; private float sweepspeed; private float sweepangle; boolean usecenter; int count; // 等份 @override protected void ondraw(canvas canvas) { setsweepangle(count); while (startangle <= 360 ) { if (startangle % (count* 3 ) == 0 ) { paint.setcolor(color.blue); } else if (startangle % (count* 2 ) == 0 ){ paint.setcolor(color.green); } else { paint.setcolor(color.red); } log.e( "" +startangle, paint.getcolor()+ "" ); canvas.drawarc(oval, startangle, sweepangle, usecenter, paint); startangle += count; } float centerx = oval.centerx(); float centery = oval.centery(); paint.setcolor(color.white); // paint.setstrokewidth(5); // paint.setstyle(paint.style.stroke); //设置空心 paint.setantialias( true ); //消除锯齿 canvas.drawcircle(centerx, centery, 50 , paint); string text = "奖" ; paint.settextsize( 20 * getcontext().getresources().getdisplaymetrics().density); float measuretext = paint.measuretext(text); float texty = paint.descent() - paint.ascent(); paint.setcolor(color.red); // canvas.drawline(0, centery, 480, centery, paint); // canvas.drawtext(text, centerx-(measuretext/2), centery, paint); canvas.drawtext(text, centerx-(measuretext/ 2 ), centery+(texty/ 4 ), paint); } private void init() { paint = new paint(); paint.setcolor(color.blue); paint.setantialias( true ); paint.setstrokewidth( 5 ); } @override public boolean ontouchevent(motionevent event) { return super .ontouchevent(event); } /** * @return the count */ public int getcount() { return count; } /** * @param count the count to set */ public void setcount( int count) { this .count = 360 / count; } public paint getpaint() { return paint; } public void setpaint(paint paint) { this .paint = paint; } public rectf getoval() { return oval; } public void setoval(rectf oval) { this .oval = oval; } public float getstartangle() { return startangle; } public void setstartangle( float startangle) { this .startangle = startangle; } public float getsweepspeed() { return sweepspeed; } public void setsweepspeed( float sweepspeed) { this .sweepspeed = sweepspeed; } public float getsweepangle() { return sweepangle; } public void setsweepangle( float sweepangle) { this .sweepangle = sweepangle; } public boolean isusecenter() { return usecenter; } public void setusecenter( boolean usecenter) { this .usecenter = usecenter; } public halfcircle(context context, attributeset attrs, int defstyle) { super (context, attrs, defstyle); init(); } public halfcircle(context context, attributeset attrs) { this (context, attrs, 0 ); } public halfcircle(context context) { this (context, null , 0 ); } } |
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
|
package com.microchange.lucky; import android.app.activity; import android.graphics.rectf; import android.os.bundle; import android.view.animation.accelerateinterpolator; import android.view.animation.animation; import android.view.animation.decelerateinterpolator; import android.view.animation.interpolator; import android.view.animation.rotateanimation; public class mainactivity extends activity { rectf rect; int radius = 300 ; @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); halfcircle circle = new halfcircle(getapplicationcontext()); circle.setoval(getrectf()); // circle.setstartangle(90); circle.setusecenter( true ); circle.setcount( 9 ); animation animation = new rotateanimation( 0 , 135 * 10 , getrectf().centerx(), getrectf().centery()); animation.setduration( 5000 ); animation.setinterpolator( new decelerateinterpolator()); // animation.setrepeatcount(-1); circle.setanimation(animation ); // animation.start(); setcontentview(circle); } public rectf getrectf(){ if (rect== null ){ // getwindow().getdecorview().getwidth() int width = getresources().getdisplaymetrics().widthpixels; int height = getresources().getdisplaymetrics().heightpixels; int top = (height - radius)/ 2 ; int left = (width - radius)/ 2 ; rect = new rectf(left, top, left+radius, top+radius); } return rect; } } |
希望本文所述对大家学习android程序设计有所帮助。