life02
C++博客
::
首页
::
新随笔
::
联系
::
聚合
::
管理
::
197 随笔 :: 3 文章 :: 37 评论 :: 0 Trackbacks
<
2011年12月
>
日
一
二
三
四
五
六
27
28
29
30
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
1
2
3
4
5
6
7
常用链接
我的随笔
我的评论
我参与的随笔
留言簿
(4)
给我留言
查看公开留言
查看私人留言
随笔分类
(179)
android ndk开发(6)
(rss)
Android开发(71)
(rss)
android面试题(4)
(rss)
android模拟器学习(7)
(rss)
android组件学习(15)
(rss)
c++学习(9)
(rss)
laucher开发(转载)(7)
(rss)
OGRE(1)
(rss)
笔试(24)
(rss)
简历(6)
(rss)
算法(7)
(rss)
游戏开发(3)
(rss)
源码学习(19)
(rss)
随笔档案
(197)
2012年7月 (1)
2012年4月 (13)
2012年3月 (40)
2012年2月 (20)
2012年1月 (10)
2011年12月 (47)
2011年11月 (4)
2011年10月 (1)
2011年8月 (3)
2011年7月 (1)
2011年6月 (4)
2011年3月 (1)
2011年2月 (1)
2010年12月 (4)
2010年4月 (2)
2010年2月 (1)
2009年11月 (3)
2009年10月 (16)
2009年9月 (22)
2009年8月 (3)
文章分类
(1)
转载--OGRE(1)
(rss)
文章档案
(3)
2010年12月 (1)
2009年9月 (1)
2009年8月 (1)
收藏夹
(16)
c++游戏开发(2)
(rss)
笔\面试经验(5)
(rss)
笔试题(9)
(rss)
Android开发
Android 源码
(rss)
feng88724(android)
get android source
Himi-android game
java-admin
Sunboy_2050的专栏
widebright的个人空间
八度空间 一点点技术,一点点文艺
点点
愷风(Wei)的专栏
思想实践地
移动平台碎碎念
c++blog——游戏
3d Game Walkman
AI_blog
AI-CODE.ORG
AI博客集合
A东亮——blog
赖勇浩的编程私伙局
牛蛙社团队
牵着老婆满街逛
清源游民的网络笔记本
王者风范 浩荡天下
业内网面试笔试综合版
游戏程序员养成计划
重剑无锋,大巧不工
c++学习
A老牛
c++虚函数
编程之美
QQblog
我想我是鸟
阿里巴巴
杨争的专栏
笔试
技术网站
AIGameDev
android 学习视频
(rss)
http://www.mars-droid.com/
Android开发指南中文版
C/C++ Reference
CSDN
Game Programming Wiki
GameDev.NET
linux学习
sourceforge.net
STL学习
STL中文站
软件测试
数据结构
游戏学习
游戏学习网站
搜索
最新评论
1. re: Android开发多线程断点续传下载器 (转载)
这个handler是不是只能放在外面?
还有哪些地方 。
--25Age
2. re: An internal error occurred during: "Launching New_configuration"
这个问题,这样解决不了啊!!!还有其他办法木啊
--李大明
3. re: Ubuntu 中学习 C/C++ 编程基础入门教程
fugkgfyuk
--46546416
4. re: IconUtilities类的createIconBitmap方法分析
分析在哪里?
--bs
5. re: android SD卡文件的读写(z转载)
不错,有帮助
--brief
阅读排行榜
1. 类string的构造函数、拷贝构造函数和析构函数(6481)
2. Android中Context详解 ---- 你所不知道的Context (转载)(6287)
3. RJ48线序及RJ48的自环线缆做法(6001)
4. android控件详解----TextView(转载)(5438)
5. canvas 里drawbitmap方法(5218)
评论排行榜
1. 如何判断一点在三角形内(转)(8)
2. 类string的构造函数、拷贝构造函数和析构函数(4)
3. 深信服笔试(转)(4)
4. Android开发多线程断点续传下载器 (转载)(2)
5. 笔试题(2)
带进度和时间的播放器(转载)
http://hao3100590.iteye.com/blog/1317151
*
博客分类: Android开发
androidProgressBar播放器进度
最近由于需要,做了一个音乐播放控制view,在上面需要能
*
控制播放
*
显示剩余时间
*
显示进度(整个view就是一个进度条)
*
实现播放暂停,以及ProgressBar的第一二进度功能
开始想到用组合的方式实现,然后重写ProgressBar的方式实现,但是发现很困难而且文字显示也不行
最后只有自己动手写一个新的控制条
主要的原理就是绘制视图的时候控制onDraw,然后在上面画图片和文字
然后在进度变化的时候不断重绘View就可以了
先上图:
其主要的View部分:
Java代码 收藏代码
1
.
package
com.hao;
2
.
3
.
import
android.content.Context;
4
.
import
android.graphics.Bitmap;
5
.
import
android.graphics.BitmapFactory;
6
.
import
android.graphics.Canvas;
7
.
import
android.graphics.Color;
8
.
import
android.graphics.Paint;
9
.
import
android.graphics.PixelFormat;
10
.
import
android.graphics.Rect;
11
.
import
android.graphics.drawable.BitmapDrawable;
12
.
import
android.graphics.drawable.Drawable;
13
.
import
android.util.AttributeSet;
14
.
import
android.util.Log;
15
.
import
android.view.MotionEvent;
16
.
import
android.view.View;
17
.
import
android.view.View.MeasureSpec;
18
.
import
android.view.View.OnClickListener;
19
.
import
android.widget.ImageButton;
20
.
import
android.widget.ImageView;
21
.
import
android.widget.TextView;
22
.
23
.
public
class
ProgressButton
extends
View{
24
.
private
Bitmap begin , bm_gray, bm_yellow, bm_second, end_gray, end_yellow, line,begin_gray;
25
.
private
Bitmap pausePressedImg;
26
.
private
Bitmap playPressedImg;
27
.
private
int
bitmapWidth
=
0
, bitmapHeight
=
0
, btWidth
=
0
, btHeight
=
0
;
28
.
private
int
progress
=
0
, secondProgress
=
0
;
29
.
private
double
perLen
=
0
, max
=
0
, maxSize
=
0
;
30
.
private
OnProgressChanged mOnProgressChanged;
31
.
private
boolean
isPlaying
=
false
;
32
.
private
Paint mTextPaint;
33
.
private
String time
=
"
00:00
"
;
34
.
private
int
color
=
Color.BLUE;
35
.
36
.
public
ProgressButton(Context context) {
37
.
super
(context);
38
.
//
TODO Auto-generated constructor stub
39
. init();
40
. }
41
.
42
.
public
ProgressButton(Context context, AttributeSet attrs,
int
defStyle){
43
.
super
(context, attrs, defStyle);
44
. init();
45
. }
46
.
47
.
public
ProgressButton(Context context, AttributeSet attrs){
48
.
super
(context, attrs);
49
. init();
50
. }
51
.
52
.
private
void
init(){
53
. begin
=
drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_left_yellow));
54
. begin_gray
=
drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_left_gray));
55
. bm_gray
=
drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_gray));
56
. bm_yellow
=
drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_yellow));
57
. bm_second
=
drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_second_yellow));
58
. end_gray
=
drawableToBitmap(getResources().getDrawable( R.drawable.rectangle_right_gray));
59
. end_yellow
=
drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_right_yellow));
60
. line
=
drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_line));
61
. pausePressedImg
=
BitmapFactory.decodeResource(getResources(), R.drawable.pause_button_pressed);
62
. playPressedImg
=
BitmapFactory.decodeResource(getResources(), R.drawable.play_button_pressed);
63
. bitmapHeight
=
begin.getHeight();
64
. bitmapWidth
=
begin.getWidth();
65
. btWidth
=
pausePressedImg.getWidth();
66
. btHeight
=
pausePressedImg.getHeight();
67
. mTextPaint
=
new
Paint();
68
. mTextPaint.setAntiAlias(
true
);
69
. mTextPaint.setTextSize(
14
);
70
. mTextPaint.setColor(color);
71
. setPadding(
3
,
3
,
3
,
3
);
72
. }
73
.
74
.
public
static
Bitmap drawableToBitmap(Drawable drawable) {
75
.
int
width
=
drawable.getIntrinsicWidth();
76
.
int
height
=
drawable.getIntrinsicHeight();
77
. Bitmap bitmap
=
Bitmap.createBitmap(width, height, drawable
78
. .getOpacity()
!=
PixelFormat.OPAQUE
?
Bitmap.Config.ARGB_8888
79
. : Bitmap.Config.RGB_565);
80
. Canvas canvas
=
new
Canvas(bitmap);
81
. drawable.setBounds(
0
,
0
, width, height);
82
. drawable.draw(canvas);
83
.
return
bitmap;
84
. }
85
.
86
.
87
.
88
. @Override
89
.
protected
void
onMeasure(
int
widthMeasureSpec,
int
heightMeasureSpec) {
90
.
//
TODO Auto-generated method stub
91
. Log.e(
"
*******
"
,
"
onMeasure
"
);
92
. setMeasuredDimension(measureWidth(widthMeasureSpec),
93
. measureHeight(heightMeasureSpec));
94
. perLen
=
maxSize
/
max;
95
. }
96
.
97
. @Override
98
.
protected
void
onDraw(Canvas canvas) {
99
.
//
TODO Auto-generated method stub
100
. Log.e(
"
*******
"
,
"
onDraw
"
);
101
.
int
middle1
=
(
int
) (progress
*
perLen), middle2
=
(
int
) (secondProgress
*
perLen) ,end
=
(
int
) maxSize
-
4
;
102
.
if
(progress
==
0
&&
secondProgress
==
0
){
103
.
//
draw background
104
. canvas.drawBitmap(begin_gray,
new
Rect(
0
,
0
,bitmapWidth,bitmapHeight),
105
.
new
Rect(
0
,
0
, bitmapWidth, bitmapHeight),
null
);
106
. canvas.drawBitmap(bm_gray,
new
Rect(
0
,
0
,end
-
middle1,bitmapHeight),
107
.
new
Rect(bitmapWidth,
0
, end, bitmapHeight),
null
);
108
. canvas.drawBitmap(end_gray,
new
Rect(
0
,
0
,
4
,bitmapHeight),
109
.
new
Rect(end,
0
, end
+
4
, bitmapHeight),
null
);
110
.
//
draw button and line
111
. canvas.drawBitmap(playPressedImg,
new
Rect(
0
,
0
, btWidth, btHeight),
112
.
new
Rect(
0
,
0
, btWidth, bitmapHeight),
null
);
113
. canvas.drawBitmap(line,
new
Rect(
0
,
0
,
2
, bitmapHeight),
114
.
new
Rect(btWidth,
0
, btWidth
+
2
, bitmapHeight),
null
);
115
.
//
draw time and line
116
.
if
(time.length()
==
5
){
117
. canvas.drawBitmap(line,
new
Rect(
0
,
0
,
2
, bitmapHeight),
118
.
new
Rect(end
-
50
,
0
, end
-
48
, bitmapHeight),
null
);
119
. canvas.drawText(
"
-
"
+
time, end
-
45
, bitmapHeight
/
2
+
5
, mTextPaint);
120
. }
else
{
121
. canvas.drawBitmap(line,
new
Rect(
0
,
0
,
2
, bitmapHeight),
122
.
new
Rect(end
-
60
,
0
, end
-
58
, bitmapHeight),
null
);
123
. canvas.drawText(
"
-
"
+
time, end
-
55
, bitmapHeight
/
2
+
5
, mTextPaint);
124
. }
125
. }
else
{
126
.
//
begin
127
. canvas.drawBitmap(begin,
new
Rect(
0
,
0
,bitmapWidth,bitmapHeight),
128
.
new
Rect(
0
,
0
, bitmapWidth, bitmapHeight),
null
);
129
. canvas.drawBitmap(bm_yellow,
new
Rect(
0
,
0
,middle1
-
bitmapWidth,bitmapHeight),
130
.
new
Rect(bitmapWidth,
0
, middle1, bitmapHeight),
null
);
131
.
//
middle
132
.
if
(secondProgress
!=
0
&&
secondProgress
>
progress){
133
. canvas.drawBitmap(bm_second,
new
Rect(
0
,
0
,bitmapWidth,bitmapHeight),
134
.
new
Rect(middle1,
0
, middle2, bitmapHeight),
null
);
135
. canvas.drawBitmap(bm_gray,
new
Rect(
0
,
0
,bitmapWidth,bitmapHeight),
136
.
new
Rect(middle2,
0
, end, bitmapHeight),
null
);
137
. }
else
{
138
. canvas.drawBitmap(bm_gray,
new
Rect(
0
,
0
,end
-
middle1,bitmapHeight),
139
.
new
Rect(middle1,
0
, end, bitmapHeight),
null
);
140
. }
141
.
//
end
142
. canvas.drawBitmap(end_gray,
new
Rect(
0
,
0
,
4
,bitmapHeight),
143
.
new
Rect(end,
0
, end
+
4
, bitmapHeight),
null
);
144
.
if
(middle2
>=
end
||
middle1
>=
end){
145
. canvas.drawBitmap(end_yellow,
new
Rect(
0
,
0
,
4
,bitmapHeight),
146
.
new
Rect(end,
0
, end
+
4
, bitmapHeight),
null
);
147
. }
148
.
//
draw button
149
.
if
(
!
isPlaying) {
150
. canvas.drawBitmap(playPressedImg,
new
Rect(
0
,
0
, btWidth, btHeight),
151
.
new
Rect(
0
,
0
, btWidth, bitmapHeight),
null
);
152
. }
else
{
153
. canvas.drawBitmap(pausePressedImg,
new
Rect(
0
,
0
, btWidth, btHeight),
154
.
new
Rect(
0
,
0
, btWidth, bitmapHeight),
null
);
155
. }
156
.
//
draw line and time
157
. canvas.drawBitmap(line,
new
Rect(
0
,
0
,
2
, bitmapHeight),
158
.
new
Rect(btWidth,
0
, btWidth
+
2
, bitmapHeight),
null
);
159
.
if
(time.length()
==
5
){
160
. canvas.drawBitmap(line,
new
Rect(
0
,
0
,
2
, bitmapHeight),
161
.
new
Rect(end
-
50
,
0
, end
-
48
, bitmapHeight),
null
);
162
. canvas.drawText(
"
-
"
+
time, end
-
45
, bitmapHeight
/
2
+
5
, mTextPaint);
163
. }
else
{
164
. canvas.drawBitmap(line,
new
Rect(
0
,
0
,
2
, bitmapHeight),
165
.
new
Rect(end
-
60
,
0
, end
-
58
, bitmapHeight),
null
);
166
. canvas.drawText(
"
-
"
+
time, end
-
55
, bitmapHeight
/
2
+
5
, mTextPaint);
167
. }
168
. }
169
.
super
.onDraw(canvas);
170
. }
171
.
172
.
173
. @Override
174
.
public
boolean
onTouchEvent(MotionEvent event) {
175
.
//
TODO Auto-generated method stub
176
.
//
在这里因为要换按钮,故而需要更新整个视图
177
.
if
(event.getAction()
==
MotionEvent.ACTION_DOWN){
178
. onClickListener.onClick(
this
);
179
. invalidate();
180
. }
181
.
return
true
;
182
. }
183
.
184
.
/**
185. * 这个方法必须设置,当播放的时候
186. *
@param
isPlaying
187.
*/
188
.
public
void
setStateChanged(
boolean
isPlaying){
189
.
this
.isPlaying
=
isPlaying;
190
. }
191
.
192
.
public
void
setTextColor(
int
color){
193
.
this
.color
=
color;
194
. invalidate();
195
. }
196
.
197
.
198
.
/**
199. * Determines the width of this view
200. *
@param
measureSpec A measureSpec packed into an int
201. *
@return
The width of the view, honoring constraints from measureSpec
202.
*/
203
.
private
int
measureWidth(
int
measureSpec) {
204
.
int
result
=
0
;
205
.
int
specMode
=
MeasureSpec.getMode(measureSpec);
206
.
int
specSize
=
MeasureSpec.getSize(measureSpec);
207
.
if
(specMode
==
MeasureSpec.EXACTLY) {
208
.
//
We were told how big to be
209
. result
=
specSize;
210
. }
else
{
211
. result
=
(
int
) ((
int
)max
*
perLen
+
getPaddingLeft()
+
getPaddingRight());
212
.
if
(specMode
==
MeasureSpec.AT_MOST) {
213
.
//
Respect AT_MOST value if that was what is called for by measureSpec
214
. result
=
Math.min(result, specSize);
215
. }
216
. }
217
. System.out.println(
"
width:
"
+
result);
218
. maxSize
=
result;
219
.
return
result;
220
. }
221
.
222
.
/**
223. * Determines the height of this view
224. *
@param
measureSpec A measureSpec packed into an int
225. *
@return
The height of the view, honoring constraints from measureSpec
226.
*/
227
.
private
int
measureHeight(
int
measureSpec) {
228
.
int
result
=
0
;
229
.
int
specMode
=
MeasureSpec.getMode(measureSpec);
230
.
int
specSize
=
MeasureSpec.getSize(measureSpec);
231
.
232
.
if
(specMode
==
MeasureSpec.EXACTLY) {
233
.
//
We were told how big to be
234
. result
=
specSize;
235
. }
else
{
236
.
//
Measure the text (beware: ascent is a negative number)
237
. result
=
(
int
) getPaddingTop()
+
getPaddingBottom()
+
bitmapHeight;
238
.
if
(specMode
==
MeasureSpec.AT_MOST) {
239
.
//
Respect AT_MOST value if that was what is called for by measureSpec
240
. result
=
Math.min(result, specSize);
241
. }
242
. }
243
. System.out.println(
"
Height:
"
+
result);
244
.
return
result;
245
. }
246
.
247
.
/**
248. * set the time
249. *
@param
currentTime 当前播放时间
250. *
@param
totalTime 总播放时间
251.
*/
252
.
public
void
setTime(
int
currentTime,
int
totalTime){
253
.
int
time
=
totalTime
-
currentTime;
254
.
if
(time
<=
1000
){
255
.
this
.time
=
"
00:00
"
;
256
.
return
;
257
. }
258
. time
/=
1000
;
259
.
int
minute
=
time
/
60
;
260
.
int
hour
=
minute
/
60
;
261
.
int
second
=
time
%
60
;
262
. minute
%=
60
;
263
.
if
(hour
==
0
){
264
.
this
.time
=
String.format(
"
%02d:%02d
"
, minute,second);
265
. }
else
{
266
.
this
.time
=
String.format(
"
%02d:%02d:%02d
"
, hour, minute,second);
267
. }
268
. }
269
.
270
.
/**
271. *
272. *
@param
viewWidth 组件的宽度
273.
*/
274
.
public
void
setMax(
int
max){
275
.
this
.max
=
max;
276
. }
277
.
278
.
public
int
getMax(){
279
.
return
(
int
)max;
280
. }
281
.
282
.
/**
283. * 设置第一进度
284. *
@param
progress
285.
*/
286
.
public
void
setProgress(
int
progress){
287
.
if
(progress
>
max){
288
. progress
=
(
int
) max;
289
. }
290
.
else
if
(progress
<
0
){
291
. progress
=
0
;
292
. }
293
.
if
(mOnProgressChanged
!=
null
){
294
. mOnProgressChanged.onProgressUpdated();
295
. }
296
.
this
.progress
=
progress;
297
. invalidate();
298
. }
299
.
300
.
/**
301. * 设置第二进度
302. *
@param
secondProgress
303.
*/
304
.
public
void
setSecondProgress(
int
secondProgress){
305
.
if
(secondProgress
>
max){
306
. secondProgress
=
(
int
) max;
307
. }
308
.
else
if
(secondProgress
<
0
){
309
. secondProgress
=
0
;
310
. }
311
.
if
(mOnProgressChanged
!=
null
){
312
. mOnProgressChanged.onSecondProgressUpdated();
313
. }
314
.
this
.secondProgress
=
secondProgress;
315
. invalidate();
316
. }
317
.
318
.
/**
319. * 设置进度监听器
320. *
@param
mOnProgressChanged
321.
*/
322
.
public
void
setmOnProgressChanged(OnProgressChanged mOnProgressChanged) {
323
.
this
.mOnProgressChanged
=
mOnProgressChanged;
324
. }
325
.
326
.
327
.
public
interface
OnProgressChanged{
328
.
void
onProgressUpdated();
329
.
void
onSecondProgressUpdated();
330
. }
331
.
332
. @Override
333
.
public
void
setOnClickListener(OnClickListener l) {
334
.
//
TODO Auto-generated method stub
335
.
if
(l
!=
null
) onClickListener
=
l;
336
.
super
.setOnClickListener(l);
337
. }
338
.
339
.
private
View.OnClickListener onClickListener;
340
.
341
. }
package
com.hao;
import
android.content.Context;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.graphics.Canvas;
import
android.graphics.Color;
import
android.graphics.Paint;
import
android.graphics.PixelFormat;
import
android.graphics.Rect;
import
android.graphics.drawable.BitmapDrawable;
import
android.graphics.drawable.Drawable;
import
android.util.AttributeSet;
import
android.util.Log;
import
android.view.MotionEvent;
import
android.view.View;
import
android.view.View.MeasureSpec;
import
android.view.View.OnClickListener;
import
android.widget.ImageButton;
import
android.widget.ImageView;
import
android.widget.TextView;
public
class
ProgressButton
extends
View{
private
Bitmap begin , bm_gray, bm_yellow, bm_second, end_gray, end_yellow, line,begin_gray;
private
Bitmap pausePressedImg;
private
Bitmap playPressedImg;
private
int
bitmapWidth
=
0
, bitmapHeight
=
0
, btWidth
=
0
, btHeight
=
0
;
private
int
progress
=
0
, secondProgress
=
0
;
private
double
perLen
=
0
, max
=
0
, maxSize
=
0
;
private
OnProgressChanged mOnProgressChanged;
private
boolean
isPlaying
=
false
;
private
Paint mTextPaint;
private
String time
=
"
00:00
"
;
private
int
color
=
Color.BLUE;
public
ProgressButton(Context context) {
super
(context);
//
TODO Auto-generated constructor stub
init();
}
public
ProgressButton(Context context, AttributeSet attrs,
int
defStyle){
super
(context, attrs, defStyle);
init();
}
public
ProgressButton(Context context, AttributeSet attrs){
super
(context, attrs);
init();
}
private
void
init(){
begin
=
drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_left_yellow));
begin_gray
=
drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_left_gray));
bm_gray
=
drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_gray));
bm_yellow
=
drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_yellow));
bm_second
=
drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_second_yellow));
end_gray
=
drawableToBitmap(getResources().getDrawable( R.drawable.rectangle_right_gray));
end_yellow
=
drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_right_yellow));
line
=
drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_line));
pausePressedImg
=
BitmapFactory.decodeResource(getResources(), R.drawable.pause_button_pressed);
playPressedImg
=
BitmapFactory.decodeResource(getResources(), R.drawable.play_button_pressed);
bitmapHeight
=
begin.getHeight();
bitmapWidth
=
begin.getWidth();
btWidth
=
pausePressedImg.getWidth();
btHeight
=
pausePressedImg.getHeight();
mTextPaint
=
new
Paint();
mTextPaint.setAntiAlias(
true
);
mTextPaint.setTextSize(
14
);
mTextPaint.setColor(color);
setPadding(
3
,
3
,
3
,
3
);
}
public
static
Bitmap drawableToBitmap(Drawable drawable) {
int
width
=
drawable.getIntrinsicWidth();
int
height
=
drawable.getIntrinsicHeight();
Bitmap bitmap
=
Bitmap.createBitmap(width, height, drawable
.getOpacity()
!=
PixelFormat.OPAQUE
?
Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas
=
new
Canvas(bitmap);
drawable.setBounds(
0
,
0
, width, height);
drawable.draw(canvas);
return
bitmap;
}
@Override
protected
void
onMeasure(
int
widthMeasureSpec,
int
heightMeasureSpec) {
//
TODO Auto-generated method stub
Log.e(
"
*******
"
,
"
onMeasure
"
);
setMeasuredDimension(measureWidth(widthMeasureSpec),
measureHeight(heightMeasureSpec));
perLen
=
maxSize
/
max;
}
@Override
protected
void
onDraw(Canvas canvas) {
//
TODO Auto-generated method stub
Log.e(
"
*******
"
,
"
onDraw
"
);
int
middle1
=
(
int
) (progress
*
perLen), middle2
=
(
int
) (secondProgress
*
perLen) ,end
=
(
int
) maxSize
-
4
;
if
(progress
==
0
&&
secondProgress
==
0
){
//
draw background
canvas.drawBitmap(begin_gray,
new
Rect(
0
,
0
,bitmapWidth,bitmapHeight),
new
Rect(
0
,
0
, bitmapWidth, bitmapHeight),
null
);
canvas.drawBitmap(bm_gray,
new
Rect(
0
,
0
,end
-
middle1,bitmapHeight),
new
Rect(bitmapWidth,
0
, end, bitmapHeight),
null
);
canvas.drawBitmap(end_gray,
new
Rect(
0
,
0
,
4
,bitmapHeight),
new
Rect(end,
0
, end
+
4
, bitmapHeight),
null
);
//
draw button and line
canvas.drawBitmap(playPressedImg,
new
Rect(
0
,
0
, btWidth, btHeight),
new
Rect(
0
,
0
, btWidth, bitmapHeight),
null
);
canvas.drawBitmap(line,
new
Rect(
0
,
0
,
2
, bitmapHeight),
new
Rect(btWidth,
0
, btWidth
+
2
, bitmapHeight),
null
);
//
draw time and line
if
(time.length()
==
5
){
canvas.drawBitmap(line,
new
Rect(
0
,
0
,
2
, bitmapHeight),
new
Rect(end
-
50
,
0
, end
-
48
, bitmapHeight),
null
);
canvas.drawText(
"
-
"
+
time, end
-
45
, bitmapHeight
/
2
+
5
, mTextPaint);
}
else
{
canvas.drawBitmap(line,
new
Rect(
0
,
0
,
2
, bitmapHeight),
new
Rect(end
-
60
,
0
, end
-
58
, bitmapHeight),
null
);
canvas.drawText(
"
-
"
+
time, end
-
55
, bitmapHeight
/
2
+
5
, mTextPaint);
}
}
else
{
//
begin
canvas.drawBitmap(begin,
new
Rect(
0
,
0
,bitmapWidth,bitmapHeight),
new
Rect(
0
,
0
, bitmapWidth, bitmapHeight),
null
);
canvas.drawBitmap(bm_yellow,
new
Rect(
0
,
0
,middle1
-
bitmapWidth,bitmapHeight),
new
Rect(bitmapWidth,
0
, middle1, bitmapHeight),
null
);
//
middle
if
(secondProgress
!=
0
&&
secondProgress
>
progress){
canvas.drawBitmap(bm_second,
new
Rect(
0
,
0
,bitmapWidth,bitmapHeight),
new
Rect(middle1,
0
, middle2, bitmapHeight),
null
);
canvas.drawBitmap(bm_gray,
new
Rect(
0
,
0
,bitmapWidth,bitmapHeight),
new
Rect(middle2,
0
, end, bitmapHeight),
null
);
}
else
{
canvas.drawBitmap(bm_gray,
new
Rect(
0
,
0
,end
-
middle1,bitmapHeight),
new
Rect(middle1,
0
, end, bitmapHeight),
null
);
}
//
end
canvas.drawBitmap(end_gray,
new
Rect(
0
,
0
,
4
,bitmapHeight),
new
Rect(end,
0
, end
+
4
, bitmapHeight),
null
);
if
(middle2
>=
end
||
middle1
>=
end){
canvas.drawBitmap(end_yellow,
new
Rect(
0
,
0
,
4
,bitmapHeight),
new
Rect(end,
0
, end
+
4
, bitmapHeight),
null
);
}
//
draw button
if
(
!
isPlaying) {
canvas.drawBitmap(playPressedImg,
new
Rect(
0
,
0
, btWidth, btHeight),
new
Rect(
0
,
0
, btWidth, bitmapHeight),
null
);
}
else
{
canvas.drawBitmap(pausePressedImg,
new
Rect(
0
,
0
, btWidth, btHeight),
new
Rect(
0
,
0
, btWidth, bitmapHeight),
null
);
}
//
draw line and time
canvas.drawBitmap(line,
new
Rect(
0
,
0
,
2
, bitmapHeight),
new
Rect(btWidth,
0
, btWidth
+
2
, bitmapHeight),
null
);
if
(time.length()
==
5
){
canvas.drawBitmap(line,
new
Rect(
0
,
0
,
2
, bitmapHeight),
new
Rect(end
-
50
,
0
, end
-
48
, bitmapHeight),
null
);
canvas.drawText(
"
-
"
+
time, end
-
45
, bitmapHeight
/
2
+
5
, mTextPaint);
}
else
{
canvas.drawBitmap(line,
new
Rect(
0
,
0
,
2
, bitmapHeight),
new
Rect(end
-
60
,
0
, end
-
58
, bitmapHeight),
null
);
canvas.drawText(
"
-
"
+
time, end
-
55
, bitmapHeight
/
2
+
5
, mTextPaint);
}
}
super
.onDraw(canvas);
}
@Override
public
boolean
onTouchEvent(MotionEvent event) {
//
TODO Auto-generated method stub
//
在这里因为要换按钮,故而需要更新整个视图
if
(event.getAction()
==
MotionEvent.ACTION_DOWN){
onClickListener.onClick(
this
);
invalidate();
}
return
true
;
}
/**
* 这个方法必须设置,当播放的时候
*
@param
isPlaying
*/
public
void
setStateChanged(
boolean
isPlaying){
this
.isPlaying
=
isPlaying;
}
public
void
setTextColor(
int
color){
this
.color
=
color;
invalidate();
}
/**
* Determines the width of this view
*
@param
measureSpec A measureSpec packed into an int
*
@return
The width of the view, honoring constraints from measureSpec
*/
private
int
measureWidth(
int
measureSpec) {
int
result
=
0
;
int
specMode
=
MeasureSpec.getMode(measureSpec);
int
specSize
=
MeasureSpec.getSize(measureSpec);
if
(specMode
==
MeasureSpec.EXACTLY) {
//
We were told how big to be
result
=
specSize;
}
else
{
result
=
(
int
) ((
int
)max
*
perLen
+
getPaddingLeft()
+
getPaddingRight());
if
(specMode
==
MeasureSpec.AT_MOST) {
//
Respect AT_MOST value if that was what is called for by measureSpec
result
=
Math.min(result, specSize);
}
}
System.out.println(
"
width:
"
+
result);
maxSize
=
result;
return
result;
}
/**
* Determines the height of this view
*
@param
measureSpec A measureSpec packed into an int
*
@return
The height of the view, honoring constraints from measureSpec
*/
private
int
measureHeight(
int
measureSpec) {
int
result
=
0
;
int
specMode
=
MeasureSpec.getMode(measureSpec);
int
specSize
=
MeasureSpec.getSize(measureSpec);
if
(specMode
==
MeasureSpec.EXACTLY) {
//
We were told how big to be
result
=
specSize;
}
else
{
//
Measure the text (beware: ascent is a negative number)
result
=
(
int
) getPaddingTop()
+
getPaddingBottom()
+
bitmapHeight;
if
(specMode
==
MeasureSpec.AT_MOST) {
//
Respect AT_MOST value if that was what is called for by measureSpec
result
=
Math.min(result, specSize);
}
}
System.out.println(
"
Height:
"
+
result);
return
result;
}
/**
* set the time
*
@param
currentTime 当前播放时间
*
@param
totalTime 总播放时间
*/
public
void
setTime(
int
currentTime,
int
totalTime){
int
time
=
totalTime
-
currentTime;
if
(time
<=
1000
){
this
.time
=
"
00:00
"
;
return
;
}
time
/=
1000
;
int
minute
=
time
/
60
;
int
hour
=
minute
/
60
;
int
second
=
time
%
60
;
minute
%=
60
;
if
(hour
==
0
){
this
.time
=
String.format(
"
%02d:%02d
"
, minute,second);
}
else
{
this
.time
=
String.format(
"
%02d:%02d:%02d
"
, hour, minute,second);
}
}
/**
*
*
@param
viewWidth 组件的宽度
*/
public
void
setMax(
int
max){
this
.max
=
max;
}
public
int
getMax(){
return
(
int
)max;
}
/**
* 设置第一进度
*
@param
progress
*/
public
void
setProgress(
int
progress){
if
(progress
>
max){
progress
=
(
int
) max;
}
else
if
(progress
<
0
){
progress
=
0
;
}
if
(mOnProgressChanged
!=
null
){
mOnProgressChanged.onProgressUpdated();
}
this
.progress
=
progress;
invalidate();
}
/**
* 设置第二进度
*
@param
secondProgress
*/
public
void
setSecondProgress(
int
secondProgress){
if
(secondProgress
>
max){
secondProgress
=
(
int
) max;
}
else
if
(secondProgress
<
0
){
secondProgress
=
0
;
}
if
(mOnProgressChanged
!=
null
){
mOnProgressChanged.onSecondProgressUpdated();
}
this
.secondProgress
=
secondProgress;
invalidate();
}
/**
* 设置进度监听器
*
@param
mOnProgressChanged
*/
public
void
setmOnProgressChanged(OnProgressChanged mOnProgressChanged) {
this
.mOnProgressChanged
=
mOnProgressChanged;
}
public
interface
OnProgressChanged{
void
onProgressUpdated();
void
onSecondProgressUpdated();
}
@Override
public
void
setOnClickListener(OnClickListener l) {
//
TODO Auto-generated method stub
if
(l
!=
null
) onClickListener
=
l;
super
.setOnClickListener(l);
}
private
View.OnClickListener onClickListener;
}
控制非常简单,在这里设置了第一和二进度:
Java代码 收藏代码
1
.
package
com.hao;
2
.
3
.
import
java.util.Timer;
4
.
import
java.util.TimerTask;
5
.
6
.
import
com.hao.ProgressView.OnProgressChanged;
7
.
8
.
import
android.app.Activity;
9
.
import
android.graphics.drawable.Drawable;
10
.
import
android.os.Bundle;
11
.
import
android.os.Handler;
12
.
import
android.os.Message;
13
.
import
android.view.Gravity;
14
.
import
android.view.View;
15
.
import
android.view.View.OnClickListener;
16
.
import
android.view.ViewGroup.LayoutParams;
17
.
import
android.widget.ImageButton;
18
.
import
android.widget.LinearLayout;
19
.
import
android.widget.SeekBar;
20
.
import
android.widget.SeekBar.OnSeekBarChangeListener;
21
.
22
.
public
class
SeekBarTestActivity
extends
Activity {
23
. ProgressButton bp;
24
.
int
time
=
60000
, currentTime
=
0
;
25
.
boolean
flag
=
true
;
26
.
/**
Called when the activity is first created.
*/
27
. @Override
28
.
public
void
onCreate(Bundle savedInstanceState) {
29
.
super
.onCreate(savedInstanceState);
30
. setContentView(R.layout.my_progress_bar);
31
. bp
=
(ProgressButton) findViewById(R.id.pbt);
32
. bp.setOnClickListener(
new
OnClickListener() {
33
.
34
. @Override
35
.
public
void
onClick(View v) {
36
.
//
TODO Auto-generated method stub
37
. System.out.println(
"
main onck
"
);
38
. bp.setStateChanged(flag);
39
. flag
=
!
flag;
40
. }
41
. });
42
. bp.setMax(
60000
);
43
. Timer timer
=
new
Timer();
44
. timer.schedule(
new
TimerTask() {
45
.
46
. @Override
47
.
public
void
run() {
48
.
//
TODO Auto-generated method stub
49
. handler.sendEmptyMessage(
0
);
50
. }
51
. },
0
,
2000
);
52
.
53
.
54
. }
55
.
56
. Handler handler
=
new
Handler(){
57
.
58
. @Override
59
.
public
void
handleMessage(Message msg) {
60
.
//
TODO Auto-generated method stub
61
. currentTime
+=
1000
;
62
. bp.setProgress(currentTime);
63
. bp.setSecondProgress(currentTime
+
1000
);
64
. bp.setTime(currentTime, time);
65
. }
66
.
67
. };
68
. }
package
com.hao;
import
java.util.Timer;
import
java.util.TimerTask;
import
com.hao.ProgressView.OnProgressChanged;
import
android.app.Activity;
import
android.graphics.drawable.Drawable;
import
android.os.Bundle;
import
android.os.Handler;
import
android.os.Message;
import
android.view.Gravity;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.view.ViewGroup.LayoutParams;
import
android.widget.ImageButton;
import
android.widget.LinearLayout;
import
android.widget.SeekBar;
import
android.widget.SeekBar.OnSeekBarChangeListener;
public
class
SeekBarTestActivity
extends
Activity {
ProgressButton bp;
int
time
=
60000
, currentTime
=
0
;
boolean
flag
=
true
;
/**
Called when the activity is first created.
*/
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.my_progress_bar);
bp
=
(ProgressButton) findViewById(R.id.pbt);
bp.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
//
TODO Auto-generated method stub
System.out.println(
"
main onck
"
);
bp.setStateChanged(flag);
flag
=
!
flag;
}
});
bp.setMax(
60000
);
Timer timer
=
new
Timer();
timer.schedule(
new
TimerTask() {
@Override
public
void
run() {
//
TODO Auto-generated method stub
handler.sendEmptyMessage(
0
);
}
},
0
,
2000
);
}
Handler handler
=
new
Handler(){
@Override
public
void
handleMessage(Message msg) {
//
TODO Auto-generated method stub
currentTime
+=
1000
;
bp.setProgress(currentTime);
bp.setSecondProgress(currentTime
+
1000
);
bp.setTime(currentTime, time);
}
};
}
布局文件:
Java代码 收藏代码
1
.
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
2
.
<
LinearLayout
3
. xmlns:android
=
"
http://schemas.android.com/apk/res/android
"
4
. android:layout_height
=
"
fill_parent
"
5
. android:layout_width
=
"
fill_parent
"
6
. android:orientation
=
"
vertical
"
>
7
.
<
TextView android:layout_width
=
"
fill_parent
"
8
. android:layout_height
=
"
wrap_content
"
9
. android:text
=
"
音乐播放
"
/>
10
.
<
LinearLayout
11
. android:layout_width
=
"
fill_parent
"
12
. android:layout_height
=
"
wrap_content
"
13
. android:paddingTop
=
"
5dip
"
14
. android:gravity
=
"
center_horizontal
"
>
15
.
<
com.hao.ProgressButton
16
. android:id
=
"
@+id/pb1
"
17
. android:layout_width
=
"
300dp
"
18
. android:layout_height
=
"
45dp
"
19
. android:background
=
"
@drawable/button_left_gray_background
"
/>
20
.
</
LinearLayout
>
21
.
</
LinearLayout
>
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
<
LinearLayout
xmlns:android
=
"
http://schemas.android.com/apk/res/android
"
android:layout_height
=
"
fill_parent
"
android:layout_width
=
"
fill_parent
"
android:orientation
=
"
vertical
"
>
<
TextView android:layout_width
=
"
fill_parent
"
android:layout_height
=
"
wrap_content
"
android:text
=
"
音乐播放
"
/>
<
LinearLayout
android:layout_width
=
"
fill_parent
"
android:layout_height
=
"
wrap_content
"
android:paddingTop
=
"
5dip
"
android:gravity
=
"
center_horizontal
"
>
<
com.hao.ProgressButton
android:id
=
"
@+id/pb1
"
android:layout_width
=
"
300dp
"
android:layout_height
=
"
45dp
"
android:background
=
"
@drawable/button_left_gray_background
"
/>
</
LinearLayout
>
</
LinearLayout
>
posted on 2012-03-31 16:38
life02
阅读(458)
评论(0)
编辑
收藏
引用
所属分类:
android ndk开发
只有注册用户
登录
后才能发表评论。
【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
相关文章:
第二个功能首先想到的是socketserver,但是不知道在wifi环境下一台android手机当服务器,另外一台当客户端访问(转载)
android 私有文件夹 文件的写入与读取(转载)
android开发音乐播放器之进度条
带进度和时间的播放器(转载)
getDefaultSharedPreferences()
使用eclipse开发ndk之:导入现有的ndk工程(NDK例子-Hello-jni导入到eclipse)(转载)
网站导航:
博客园
IT新闻
BlogJava
知识库
博问
管理
Powered by:
C++博客
Copyright © life02