我有一个活动,其中包含WebView
和ProgressBar
。在加载网页期间,进度条出现并逐渐水平填充。动画结束后,如何将进度条的可见性设置为View.GONE
?
public class WebviewActivity extends AppCompatActivity {
WebView myWebView;
ProgressBar pBar;
ProgressBar myProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_webview);
}
@Override
protected void onStart() {
super.onStart();
setContentView(R.layout.fragment_webview);
myProgressBar = findViewById(R.id.mProgressBar);
myProgressBar.setMax(100);
String url = getIntent().getStringExtra("url");
myWebView = findViewById(R.id.webview01);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl(url);
myWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
myProgressBar.setProgress(newProgress);
if (newProgress == 100) {
AlphaAnimation fadeOut;
fadeOut = new AlphaAnimation(1,0);
fadeOut.setDuration(500);
fadeOut.setFillAfter(true);
myProgressBar.startAnimation(fadeOut);
// myProgressBar.setVisibility(View.GONE);
} else {
myProgressBar.setVisibility(View.VISIBLE);
}
}
});
}
private class WebViewClient extends android.webkit.WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){
String url=request.getUrl().toString();
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// myProgressBar.setVisibility(View.GONE);
}
}
}
答案 0 :(得分:2)
使用侦听器:
fadeOut.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimantionEnd(Animation animation) {
myProgressBar.setVisibility(View.GONE);
}
});
答案 1 :(得分:1)
尝试将动画侦听器设置为动画对象(fadeOut):
myWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
myProgressBar.setProgress(newProgress);
if (newProgress == 100) {
AlphaAnimation fadeOut;
fadeOut = new AlphaAnimation(1, 0);
fadeOut.setDuration(500);
fadeOut.setFillAfter(true);
fadeOut.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// whatever you want to happen when the fadeOut animation starts
}
@Override
public void onAnimationEnd(Animation animation) {
// whatever you want to happen when the fadeOut animation ends
myProgressBar.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
// whatever you want to happen when the fadeOut animation repeats itself
}
});
myProgressBar.startAnimation(fadeOut);
} else {
myProgressBar.setVisibility(View.VISIBLE);
}
}
});
使用上述回调,您可以插入AnimationStart
,AnimationEnd
和AnimationRepeat
。