• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.webview_shell;
6 
7 import android.app.Activity;
8 import android.os.Bundle;
9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.view.WindowManager;
12 import android.webkit.WebView;
13 import android.widget.CheckBox;
14 import android.widget.CompoundButton;
15 import android.widget.SeekBar;
16 import android.widget.SeekBar.OnSeekBarChangeListener;
17 
18 /**
19  * Activity to exercise transform animations on WebView.
20  */
21 public class WebViewAnimationTestActivity extends Activity {
22     private static final String HTML = "<html>"
23             + "  <head>"
24             + "    <style type =\"text/css\">"
25             + "      .container {"
26             + "            display: grid;"
27             + "            grid-template-columns: 100px 100px 100px 100px 100px;"
28             + "            grid-template-rows: 100px 100px 100px 100px 100px;"
29             + "      }"
30             + "     .alt1 {"
31             + "       background-color: #aaffaa;"
32             + "     }"
33             + "     .alt2 {"
34             + "       background-color: #ff4545;"
35             + "     }"
36             + "    </style>"
37             + "  </head>"
38             + "  <body>"
39             + "   <div class=\"container\">"
40             + "     <div class=\"alt1\">00</div>"
41             + "     <div class=\"alt2\">01</div>"
42             + "     <div class=\"alt1\">02</div>"
43             + "     <div class=\"alt2\">03</div>"
44             + "     <div class=\"alt1\">04</div>"
45             + "     <div class=\"alt2\">05</div>"
46             + "     <div class=\"alt1\">06</div>"
47             + "     <div class=\"alt2\">07</div>"
48             + "     <div class=\"alt1\">08</div>"
49             + "     <div class=\"alt2\">09</div>"
50             + "     <div class=\"alt1\">10</div>"
51             + "     <div class=\"alt2\">11</div>"
52             + "     <div class=\"alt1\">12</div>"
53             + "     <div class=\"alt2\">13</div>"
54             + "     <div class=\"alt1\">14</div>"
55             + "     <div class=\"alt2\">15</div>"
56             + "     <div class=\"alt1\">16</div>"
57             + "     <div class=\"alt2\">17</div>"
58             + "     <div class=\"alt1\">18</div>"
59             + "     <div class=\"alt2\">19</div>"
60             + "     <div class=\"alt1\">20</div>"
61             + "     <div class=\"alt2\">21</div>"
62             + "     <div class=\"alt1\">22</div>"
63             + "     <div class=\"alt2\">23</div>"
64             + "     <div class=\"alt1\">24</div>"
65             + "   </div>"
66             + "  </body>"
67             + "</html>";
68 
69     private WebView mWebView;
70     private boolean mIsWindowHardwareAccelerated;
71 
72     /** Called when the activity is first created. */
73     @Override
onCreate(Bundle savedInstanceState)74     public void onCreate(Bundle savedInstanceState) {
75         super.onCreate(savedInstanceState);
76         setContentView(R.layout.activity_webview_animation_test);
77         mWebView = (WebView) findViewById(R.id.webview);
78 
79         mIsWindowHardwareAccelerated =
80                 (getWindow().getAttributes().flags
81                         | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED)
82                 != 0;
83         mWebView.setBackgroundColor(0);
84         mWebView.loadDataWithBaseURL("http://foo.bar", HTML, "text/html", null, "http://foo.bar");
85         OnClickListener onClickListner = (View v) -> {
86             switch (v.getId()) {
87                 case R.id.translate:
88                     runTranslate();
89                     break;
90                 case R.id.scale:
91                     runScale();
92                     break;
93                 case R.id.rotate:
94                     runRotate();
95                     break;
96             }
97         };
98         findViewById(R.id.scale).setOnClickListener(onClickListner);
99         findViewById(R.id.translate).setOnClickListener(onClickListner);
100         findViewById(R.id.rotate).setOnClickListener(onClickListner);
101         ((SeekBar) findViewById(R.id.view_alpha))
102                 .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
103                     @Override
104                     public void onProgressChanged(SeekBar view, int progress, boolean fromUser) {
105                         switch (view.getId()) {
106                             case R.id.view_alpha:
107                                 mWebView.setAlpha(progress / 100f);
108                                 break;
109                         }
110                     }
111 
112                     @Override
113                     public void onStartTrackingTouch(SeekBar seekBar) {}
114 
115                     @Override
116                     public void onStopTrackingTouch(SeekBar seekBar) {}
117                 });
118         CheckBox checkBox = ((CheckBox) findViewById(R.id.use_layer));
119         checkBox.setOnCheckedChangeListener(
120                 (CompoundButton arg0, boolean checked) -> { setWebViewLayer(checked); });
121         setWebViewLayer(checkBox.isChecked());
122     }
123 
runTranslate()124     private void runTranslate() {
125         if (mWebView.getTranslationX() == 0f) {
126             mWebView.animate().translationX(100f).translationY(100f);
127         } else {
128             mWebView.animate().translationX(0f).translationY(0f);
129         }
130     }
131 
runScale()132     private void runScale() {
133         if (mWebView.getScaleX() == 1f) {
134             mWebView.animate().scaleX(.5f).scaleY(.5f);
135         } else {
136             mWebView.animate().scaleX(1f).scaleY(1f);
137         }
138     }
139 
runRotate()140     private void runRotate() {
141         if (mWebView.getRotationX() == 0f) {
142             mWebView.animate().rotationX(45f).rotationY(45f).rotation(90f);
143         } else {
144             mWebView.animate().rotationX(0f).rotationY(0f).rotation(0f);
145         }
146     }
147 
setWebViewLayer(boolean isOnLayer)148     private void setWebViewLayer(boolean isOnLayer) {
149         if (isOnLayer) {
150             mWebView.setLayerType(mIsWindowHardwareAccelerated ? View.LAYER_TYPE_HARDWARE
151                                                                : View.LAYER_TYPE_SOFTWARE,
152                     null);
153         } else {
154             mWebView.setLayerType(View.LAYER_TYPE_NONE, null);
155         }
156     }
157 }
158