http://www.ophonesdn.com/forum/viewthread.jsp?tid=2188
很多细心的网友发现Android浏览器的标题栏TitleBar的功能比较多,细心的网友在查看Browser时会发现,从左到右依次为网站图标 (favicon)、标题、最右边的动画进度条(圆圈)、背景进度条(和前面的不在一层),今天我们就一起来看看Android标题栏高级实现方法。 在Android Browser程序中标题栏是自绘的,TitleBar类继承于线性布局LinearLayout类,通过LayoutInflater调用layout 中的xml布局文件实现相关方法
01.
public
class
TitleBar
extends
LinearLayout {
02.
03.
private
TextView mTitle;
//标题文字
04.
05.
private
Drawable mCloseDrawable;
06.
07.
private
ImageView mRtButton;
08.
09.
private
Drawable mCircularProgress;
//圆圈进度指示
10.
11.
private
ProgressBar mHorizontalProgress;
//水平进度条
12.
13.
private
ImageView mFavicon;
//网站图标
14.
15.
private
ImageView mLockIcon;
16.
17.
private
Drawable mStopDrawable;
//停止状态的图标
18.
19.
private
Drawable mBookmarkDrawable;
//是一个书签的图标
20.
21.
private
boolean
mInLoad;
22.
23.
private
BrowserActivity mBrowserActivity;
24.
25.
private
Drawable mGenericFavicon;
//如果站点没有favicon.ico时显示的默认 图标
26.
27.
private
int
mIconDimension;
28.
29.
private
View mTitleBg;
//文字的背景
30.
31.
private
MyHandler mHandler;
32.
33.
private
static
int
LONG_PRESS =
1
;
34.
35.
public
TitleBar(BrowserActivity context) {
36.
37.
super
(context,
null
);
38.
39.
mHandler =
new
MyHandler();
40.
41.
LayoutInflater factory = LayoutInflater.from(context);
001.
mBrowserActivity = context;
002.
003.
mTitle = (TextView) findViewById(R.id.title);
004.
005.
mTitle.setCompoundDrawablePadding(
5
);
006.
007.
mTitleBg = findViewById(R.id.title_bg);
008.
009.
mLockIcon = (ImageView) findViewById(R.id.lock);
010.
011.
mFavicon = (ImageView) findViewById(R.id.favicon);
012.
013.
mRtButton = (ImageView) findViewById(R.id.rt_btn);
014.
015.
Resources resources = context.getResources();
016.
017.
mCircularProgress = (Drawable) resources.getDrawable(com.android.internal.R.drawable.search_spinner);
018.
019.
mIconDimension = (
int
) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20f,resources.getDisplayMetrics());
020.
021.
mCircularProgress.setBounds(
0
,
0
, mIconDimension, mIconDimension);
022.
023.
mHorizontalProgress = (ProgressBar) findViewById(R.id.progress_horizontal);
024.
025.
mGenericFavicon = context.getResources().getDrawable(R.drawable.app_web_browser_sm);
026.
027.
}
028.
029.
private
class
MyHandler
extends
Handler {
030.
031.
public
void
handleMessage(Message msg) {
032.
033.
if
(msg.what == LONG_PRESS) {
034.
035.
mTitleBg.setPressed(
false
);
036.
037.
mBrowserActivity.showTitleBarContextMenu();
038.
039.
}
040.
041.
}
042.
043.
};
044.
045.
@Override
046.
047.
protected
void
onCreateContextMenu(ContextMenu menu) {
//创建上下文菜单,相关的xml代码在本文最后
048.
049.
MenuInflater inflater = mBrowserActivity.getMenuInflater();
050.
051.
inflater.inflate(R.menu.title_context, menu);
052.
053.
}
054.
055.
@Override
056.
057.
public
boolean
onTouchEvent(MotionEvent event) {
058.
059.
switch
(event.getAction()) {
060.
061.
case
MotionEvent.ACTION_DOWN:
062.
063.
if
((
int
) event.getX() > mTitleBg.getRight()) {
064.
065.
mRtButton.setPressed(
true
);
066.
067.
}
else
{
068.
069.
mTitleBg.setPressed(
true
);
070.
071.
mHandler.sendMessageDelayed(mHandler.obtainMessage(
072.
073.
LONG_PRESS),
074.
075.
ViewConfiguration.getLongPressTimeout());
076.
077.
}
078.
079.
break
;
080.
081.
case
MotionEvent.ACTION_MOVE:
082.
083.
int
slop = ViewConfiguration.get(mBrowserActivity)
084.
085.
.getScaledTouchSlop();
086.
087.
if
((
int
) event.getY() > getHeight() + slop) {
088.
089.
mTitleBg.setPressed(
false
);
090.
091.
mRtButton.setPressed(
false
);
092.
093.
mHandler.removeMessages(LONG_PRESS);
094.
095.
break
;
096.
097.
}
098.
099.
int
x = (
int
) event.getX();
100.
101.
int
titleRight = mTitleBg.getRight();
102.
103.
if
(mTitleBg.isPressed() && x > titleRight + slop) {
104.
105.
mTitleBg.setPressed(
false
);
106.
107.
mHandler.removeMessages(LONG_PRESS);
108.
109.
}
else
if
(mRtButton.isPressed() && x < titleRight - slop) {
110.
111.
mRtButton.setPressed(
false
);
112.
113.
}
114.
115.
break
;
116.
117.
case
MotionEvent.ACTION_CANCEL:
118.
119.
mRtButton.setPressed(
false
);
120.
121.
mTitleBg.setPressed(
false
);
122.
123.
mHandler.removeMessages(LONG_PRESS);
124.
125.
break
;
126.
127.
case
MotionEvent.ACTION_UP:
128.
129.
if
(mRtButton.isPressed()) {
130.
131.
if
(mInLoad) {
132.
133.
mBrowserActivity.stopLoading();
134.
135.
}
else
{
136.
137.
mBrowserActivity.bookmarksOrHistoryPicker(
false
);
138.
139.
}
140.
141.
mRtButton.setPressed(
false
);
142.
143.
}
else
if
(mTitleBg.isPressed()) {
144.
145.
mHandler.removeMessages(LONG_PRESS);
146.
147.
mBrowserActivity.onSearchRequested();
148.
149.
mTitleBg.setPressed(
false
);
150.
151.
}
152.
153.
break
;
154.
155.
default
:
156.
157.
break
;
158.
159.
}
160.
161.
return
true
;
162.
163.
}
164.
165.
boolean
isInLoad() {
166.
167.
return
mInLoad;
168.
169.
}
170.
171.
void
setFavicon(Bitmap icon) {
172.
173.
Drawable[] array =
new
Drawable[
3
];
174.
175.
array[
0
] =
new
PaintDrawable(Color.BLACK);
176.
177.
PaintDrawable p =
new
PaintDrawable(Color.WHITE);
178.
179.
array[
1
] = p;
180.
181.
if
(icon ==
null
) {
182.
183.
array[
2
] = mGenericFavicon;
184.
185.
}
else
{
186.
187.
array[
2
] =
new
BitmapDrawable(icon);
188.
189.
}
190.
191.
LayerDrawable d =
new
LayerDrawable(array);
192.
193.
d.setLayerInset(
1
,
1
,
1
,
1
,
1
);
194.
195.
d.setLayerInset(
2
,
2
,
2
,
2
,
2
);
196.
197.
mFavicon.setImageDrawable(d);
198.
199.
}
200.
201.
void
setLock(Drawable d) {
202.
203.
if
(
null
== d) {
204.
205.
mLockIcon.setVisibility(View.GONE);
206.
207.
}
else
{
208.
209.
mLockIcon.setImageDrawable(d);
210.
211.
mLockIcon.setVisibility(View.VISIBLE);
212.
213.
}
214.
215.
}
216.
217.
void
setProgress(
int
newProgress) {
218.
219.
if
(newProgress >= mHorizontalProgress.getMax()) {
220.
221.
mTitle.setCompoundDrawables(
null
,
null
,
null
,
null
);
222.
223.
((Animatable) mCircularProgress).stop();
224.
225.
mHorizontalProgress.setVisibility(View.INVISIBLE);
226.
227.
if
(mBookmarkDrawable !=
null
) {
228.
229.
mRtButton.setImageDrawable(mBookmarkDrawable);
230.
231.
}
232.
233.
mInLoad =
false
;
234.
235.
}
else
{
236.
237.
mHorizontalProgress.setProgress(newProgress);
238.
239.
if
(!mInLoad && getWindowToken() !=
null
) {
240.
241.
mTitle.setCompoundDrawables(
null
,
null
, mCircularProgress,
242.
243.
null
);
244.
245.
((Animatable) mCircularProgress).start();
246.
247.
mHorizontalProgress.setVisibility(View.VISIBLE);
248.
249.
if
(mBookmarkDrawable ==
null
) {
250.
251.
mBookmarkDrawable = mRtButton.getDrawable();
252.
253.
}
254.
255.
if
(mStopDrawable ==
null
) {
256.
257.
mRtButton.setImageResource(R.drawable.ic_btn_stop_v2);
258.
259.
mStopDrawable = mRtButton.getDrawable();
260.
261.
}
else
{
262.
263.
mRtButton.setImageDrawable(mStopDrawable);
264.
265.
}
266.
267.
mInLoad =
true
;
268.
269.
}
270.
271.
}
272.
273.
}
274.
275.
void
setTitleAndUrl(CharSequence title, CharSequence url) {
276.
277.
if
(url ==
null
) {
278.
279.
mTitle.setText(R.string.title_bar_loading);
280.
281.
}
else
{
282.
283.
mTitle.setText(url.toString());
284.
285.
}
286.
287.
}
288.
289.
void
setToTabPicker() {
290.
291.
mTitle.setText(R.string.tab_picker_title);
292.
293.
setFavicon(
null
);
294.
295.
setLock(
null
);
296.
297.
mHorizontalProgress.setVisibility(View.GONE);
298.
299.
}
300.
301.
}