[英]How to hide progress bar when animation has finished
I have an activity containing a WebView
and ProgressBar
. 我有一个包含
WebView
和ProgressBar
的活动。 During the loading of a web page, the progress bar appears and gradually horizontally fills. 在加载网页期间,进度条出现并逐渐水平填充。 How can the visibility of the progress bar be set to
View.GONE
after the animation has finished? 动画完成后,如何将进度条的可见性设置为
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);
}
}
}
Use a listener: 使用听众:
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);
}
});
Try setting an animation listener to the animation object (fadeOut): 尝试将动画侦听器设置为动画对象(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);
}
}
});
With the callbacks above, you can hook into AnimationStart
, AnimationEnd
and AnimationRepeat
. 通过上面的回调,您可以挂钩到
AnimationStart
, AnimationEnd
和AnimationRepeat
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.