Java程序  |  158行  |  5.99 KB

// 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 = "<html>"
            + "  <head>"
            + "    <style type =\"text/css\">"
            + "      .container {"
            + "            display: grid;"
            + "            grid-template-columns: 100px 100px 100px 100px 100px;"
            + "            grid-template-rows: 100px 100px 100px 100px 100px;"
            + "      }"
            + "     .alt1 {"
            + "       background-color: #aaffaa;"
            + "     }"
            + "     .alt2 {"
            + "       background-color: #ff4545;"
            + "     }"
            + "    </style>"
            + "  </head>"
            + "  <body>"
            + "   <div class=\"container\">"
            + "     <div class=\"alt1\">00</div>"
            + "     <div class=\"alt2\">01</div>"
            + "     <div class=\"alt1\">02</div>"
            + "     <div class=\"alt2\">03</div>"
            + "     <div class=\"alt1\">04</div>"
            + "     <div class=\"alt2\">05</div>"
            + "     <div class=\"alt1\">06</div>"
            + "     <div class=\"alt2\">07</div>"
            + "     <div class=\"alt1\">08</div>"
            + "     <div class=\"alt2\">09</div>"
            + "     <div class=\"alt1\">10</div>"
            + "     <div class=\"alt2\">11</div>"
            + "     <div class=\"alt1\">12</div>"
            + "     <div class=\"alt2\">13</div>"
            + "     <div class=\"alt1\">14</div>"
            + "     <div class=\"alt2\">15</div>"
            + "     <div class=\"alt1\">16</div>"
            + "     <div class=\"alt2\">17</div>"
            + "     <div class=\"alt1\">18</div>"
            + "     <div class=\"alt2\">19</div>"
            + "     <div class=\"alt1\">20</div>"
            + "     <div class=\"alt2\">21</div>"
            + "     <div class=\"alt1\">22</div>"
            + "     <div class=\"alt2\">23</div>"
            + "     <div class=\"alt1\">24</div>"
            + "   </div>"
            + "  </body>"
            + "</html>";

    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);
        }
    }
}