/**
- @author LuoXuLiang
- @date: 2017/8/8 9:17
*/
public class RateActivity extends BaseActivity {
@BindView(R.id.wv_rate)
WebView webView;
@BindView(R.id.pb_web)
ProgressBar mProgressBar;
private int from; //页面类型
private String URL = "https://www.baidu.com"; //链接地址
private boolean isAnimStart = false;
private int currentProgress=0;
@Override
protected void initViews() {
from = getIntent().getIntExtra("FROM",0);
webViewInit(URL);
}
//webview初始化
private void webViewInit(String URL) {
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.setAlpha(1.0f);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//view.loadUrl(url);
return true;
}
});
// 获取网页加载进度
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (null==mProgressBar) return;
currentProgress = mProgressBar.getProgress();
if (newProgress >= 100 && !isAnimStart) {
// 防止调用多次动画
isAnimStart = true;
mProgressBar.setProgress(newProgress);
// 开启属性动画让进度条平滑消失
startDismissAnimation(mProgressBar.getProgress());
} else {
// 开启属性动画让进度条平滑递增
startProgressAnimation(newProgress);
}
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(URL);
}
@Override
protected boolean shouldBindEvent() {
return false;
}
/**
* progressBar递增动画
*/
private void startProgressAnimation(int newProgress) {
ObjectAnimator animator = ObjectAnimator.ofInt(mProgressBar, "progress", currentProgress, newProgress);
animator.setDuration(300);
animator.setInterpolator(new DecelerateInterpolator());
animator.start();
}
/**
* progressBar消失动画
*/
private void startDismissAnimation(final int progress) {
ObjectAnimator anim = ObjectAnimator.ofFloat(mProgressBar, "alpha", 1.0f, 0.0f);
anim.setDuration(1500); // 动画时长
anim.setInterpolator(new DecelerateInterpolator()); // 减速
// 关键, 添加动画进度监听器
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float fraction = valueAnimator.getAnimatedFraction(); // 0.0f ~ 1.0f
int offset = 100 - progress;
if (null!=mProgressBar) {
mProgressBar.setProgress((int) (progress + offset * fraction));
}
}
});
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
// 动画结束
mProgressBar.setProgress(0);
mProgressBar.setVisibility(View.GONE);
isAnimStart = false;
}
});
anim.start();
}
/**
* 监听up键
* id = android.R.id.home
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* 监听back键
* 在WebView中回退导航
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) { // 返回键的KEYCODE
if (webView.canGoBack()) {
webView.goBack();
return true; // 拦截
} else {
return super.onKeyDown(keyCode, event); // 放行
}
}
return super.onKeyDown(keyCode, event);
}
}
网友评论