XML
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
<Button
android:id="@+id/button"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“点击调用”
<WebView
android:id="@+id/web"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
public class JsActivity extends AppCompatActivity {
private WebView webview;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_js);
webview = findViewById(R.id.web);
button = findViewById(R.id.button);
findViewById(R.id.dian).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(JsActivity.this,DongHuaActivity.class);
JsActivity.this.startActivity(intent);
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
webview.loadUrl("javascript:callJS()");
}
});
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
webview.loadUrl("file:///android_asset/test.html");
//支持js弹框
webview.setWebChromeClient(new WebChromeClient(){
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
return super.onJsAlert(view, url, message, result);
}
});
}
}
//动画
s DongHuaActivity extends AppCompatActivity {
private ImageView image;
private Button but;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dong_hua);
image = findViewById(R.id.image_donghua);
but = findViewById(R.id.but_donghua);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 垂直移动
float curTranslationY = image.getTranslationY();
ObjectAnimator translationY = ObjectAnimator.ofFloat(image, "translationY",
curTranslationY, curTranslationY + 100f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(image, "scaleY", 1f, 5f, 1f);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(image, "scaleX", 1f, 5f, 1f);
AnimatorSet animSet = new AnimatorSet();
animSet.play(scaleY).with(scaleX).after(translationY);
animSet.setDuration(2000);
animSet.start();
// //360旋转
// ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(image,“rotation”,0,360);
// ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(image,“translationY”,0,100);
// AnimatorSet animatorSet = new AnimatorSet();
// animatorSet.play(objectAnimator).after(objectAnimator1);
// animatorSet.setDuration(3000);
// animatorSet.start();
}
});
}
}