// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.webview_shell;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
/**
* Activity to exercise transform animations on WebView.
*/
public class WebViewAnimationTestActivity extends Activity {
private static final String HTML = ""
+ "
"
+ " "
+ " "
+ " "
+ " "
+ "
00
"
+ "
01
"
+ "
02
"
+ "
03
"
+ "
04
"
+ "
05
"
+ "
06
"
+ "
07
"
+ "
08
"
+ "
09
"
+ "
10
"
+ "
11
"
+ "
12
"
+ "
13
"
+ "
14
"
+ "
15
"
+ "
16
"
+ "
17
"
+ "
18
"
+ "
19
"
+ "
20
"
+ "
21
"
+ "
22
"
+ "
23
"
+ "
24
"
+ "
"
+ " "
+ "";
private WebView mWebView;
private boolean mIsWindowHardwareAccelerated;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview_animation_test);
mWebView = (WebView) findViewById(R.id.webview);
mIsWindowHardwareAccelerated =
(getWindow().getAttributes().flags
| WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED)
!= 0;
mWebView.setBackgroundColor(0);
mWebView.loadDataWithBaseURL("http://foo.bar", HTML, "text/html", null, "http://foo.bar");
OnClickListener onClickListner = (View v) -> {
switch (v.getId()) {
case R.id.translate:
runTranslate();
break;
case R.id.scale:
runScale();
break;
case R.id.rotate:
runRotate();
break;
}
};
findViewById(R.id.scale).setOnClickListener(onClickListner);
findViewById(R.id.translate).setOnClickListener(onClickListner);
findViewById(R.id.rotate).setOnClickListener(onClickListner);
((SeekBar) findViewById(R.id.view_alpha))
.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar view, int progress, boolean fromUser) {
switch (view.getId()) {
case R.id.view_alpha:
mWebView.setAlpha(progress / 100f);
break;
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
CheckBox checkBox = ((CheckBox) findViewById(R.id.use_layer));
checkBox.setOnCheckedChangeListener(
(CompoundButton arg0, boolean checked) -> { setWebViewLayer(checked); });
setWebViewLayer(checkBox.isChecked());
}
private void runTranslate() {
if (mWebView.getTranslationX() == 0f) {
mWebView.animate().translationX(100f).translationY(100f);
} else {
mWebView.animate().translationX(0f).translationY(0f);
}
}
private void runScale() {
if (mWebView.getScaleX() == 1f) {
mWebView.animate().scaleX(.5f).scaleY(.5f);
} else {
mWebView.animate().scaleX(1f).scaleY(1f);
}
}
private void runRotate() {
if (mWebView.getRotationX() == 0f) {
mWebView.animate().rotationX(45f).rotationY(45f).rotation(90f);
} else {
mWebView.animate().rotationX(0f).rotationY(0f).rotation(0f);
}
}
private void setWebViewLayer(boolean isOnLayer) {
if (isOnLayer) {
mWebView.setLayerType(mIsWindowHardwareAccelerated ? View.LAYER_TYPE_HARDWARE
: View.LAYER_TYPE_SOFTWARE,
null);
} else {
mWebView.setLayerType(View.LAYER_TYPE_NONE, null);
}
}
}