Android progress bar
1. Implementation results
2. layout code
Write a my_browser.xml file to store the WebView.
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<WebView
Android: id = "@ + id/webView"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"/>
</LinearLayout>
Progress bar Layout
Then write a broser. xml file to store the progress bar
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<TextView
Android: id = "@ + id/tvtitle"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: focusableInTouchMode = "true"
Android: singleLine = "true"
Android: ellipsize = "marquee"
Android: focusable = "false"
Android: marqueeRepeatLimit = "marquee_forever"
Android: textSize = "20sp" android: layout_centerVertical = "true"/>
<ProgressBar
Android: id = "@ + id/pb"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Style = "? Android: attr/progressBarStyleHorizontal"
Android: visibility = "gone"
Android: layout_alignParentBottom = "true">
</ProgressBar>
</RelativeLayout>
WebChromeClient
Override onProgressChanged and onReceivedTitle events (use the animation to fade away after the progress bar is loaded)
[Java]
/**
* MyWebChromeClient. java
* Copyright (C) 2012
[Java] view plaincopy
* Creation: cuiran 3:05:34
*/
Package com. cayden. citygirl. activity;
Import android. app. Activity;
Import android. view. View;
Import android. view. animation. Animation;
Import android. view. animation. AnimationUtils;
Import android. webkit. WebChromeClient;
Import android. webkit. WebView;
Import android. widget. ProgressBar;
Import android. widget. TextView;
/**
* TODO
* @ Author cuiran
* @ Version TODO
*/
Public class MyWebChromeClient extends WebChromeClient {
Private Activity;
Private ProgressBar pb;
Private Animation animation;
Private TextView tvtitle;
Public MyWebChromeClient (Activity activity ){
This. activity = activity;
}
@ Override
Public void onProgressChanged (WebView view, int newProgress ){
Pb = (ProgressBar) activity. findViewById (R. id. pb );
Pb. setMax (100 );
If (newProgress <100 ){
If (pb. getVisibility () = View. GONE) pb. setVisibility (View. VISIBLE );
Pb. setProgress (newProgress );
} Else {
Pb. setProgress (100 );
Animation = AnimationUtils. loadAnimation (activity, R. anim. animation );
// Run the animation
Pb. startAnimation (animation );
// Sets the visibility of the spinner to invisible.
Pb. setVisibility (View. INVISIBLE );
}
Super. onProgressChanged (view, newProgress );
}
@ Override
Public void onReceivedTitle (WebView view, String title ){
Tvtitle = (TextView) activity. findViewById (R. id. tvtitle );
Tvtitle. setText (title );
Super. onReceivedTitle (view, title );
}
}
Animated style res/anim/animation. xml of the progress bar
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Set xmlns: android = "http://schemas.android.com/apk/res/android">
& Lt; alpha android: fromAlpha = "1.0"
Android: toAlpha = "0.0"
Android: duration = "700"/>
</Set>
5. Program startup
[Java]
/**
* ProgressActivity. java
* Copyright (C) 2012
* Creation: cuiran 3:13:49
*/
Package com. cayden. citygirl. activity;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. Window;
Import android. webkit. WebView;
/**
* TODO
* @ Author cuiran
* @ Version TODO
*/
Public class ProgressActivity extends Activity {
Private WebView browser;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
GetWindow (). requestFeature (Window. FEATURE_CUSTOM_TITLE );
SetContentView (R. layout. my_browser );
GetWindow (). setFeatureInt (Window. FEATURE_CUSTOM_TITLE, R. layout. broser );
Browser = (WebView) findViewById (R. id. webView );
// CurrentWebView = browser;
Browser. setWebChromeClient (new MyWebChromeClient (ProgressActivity. this ));
Browser. loadUrl ("http://shop.paipai.com/731681975 ");
}
}