Chrome CustomTabs的使用

Chrome CustomTabs是什么
Chrome CustomTabs为应用程序提供了一种方式,可以自定义并与之互动的Chrome Activity。 这使得Web内容感觉像是应用程序的一部分,同时保留了完整Web浏览器的完整功能和性能。
这句话的理解是:如果您想在应用中打开一个网页,你可以通过 Chrome Custom Tabs 来打开 Chrome 浏览器的一个自定义 Tab 来显示该网页,你可以自定义这个 Tab 的一些属性来保持良好的用户体验,并且让用户感觉这个自定义 Tab 就是您应用的一部分
Chrome CustomTabs 介绍链接(原文)
Chrome CustomTabs 原文的翻译
Google 官方示例: CustomTabs Sample
Note:
Chrome CustomTabs支持Chrome 浏览器45版本以上,而国内 Android 手机大部分用的不是 Chrome 浏览器,要用到该项功能需要一个支持Chrome CustomTabs的浏览器

Chrome CustomTabs的应用场景

1.显示的网页内容是由自己控制的,并且网页内容需要和 Android 组件交互(比如通过 JavaScript 接口来调用 Android 系统的一些功能),这种情况下你还需要用 WebView 来实现;
2.显示的网页内容不用自己操作等其他情况都可以用 Chrome Custom Tabs 来实现

Chrome Custom Tabs 与 Chrome,WebView的对比

这里写图片描述
(网上找的图>–<)
Chrome CustomTabs的使用
在app目录下build.gradle中

compile 'com.android.support:customtabs:27.1.1'

接着自定义CustomTabsIntent,查看该类,用 Builder模式来构建
示例如下:

CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
 //Sets the toolbar color.
 builder.setToolbarColor(getResources().getColor(R.color.color_title));
 Bitmap closeBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.back);
 builder.setCloseButtonIcon(closeBitmap);// 关闭按钮
 builder.setShowTitle(true); //显示网页标题
 Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.icon_link_middle);
 PendingIntent pendingIntent =  createPendingIntent(1);
 builder.setActionButton(icon,"Collection",pendingIntent); //增加操作按钮
 //自定义 Activity 转场 动画
 builder.setStartAnimations(this, R.anim.push_left_in, R.anim.push_left_out);
 builder.setExitAnimations(this, R.anim.push_right_in,R.anim.push_right_out);
 CustomTabsIntent customTabsIntent = builder.build();
 customTabsIntent.launchUrl(SimpleNewDetailsActivity.this, Uri.parse(url));

这里写图片描述
上面的是基本用法,如果想深入的话可以参考下面的文章,就不一 一赘述了

参考文章
提升体验-支持Chrome Custom Tabs
Android 开发小工具之:Custom Tabs
Chrome custom tabs:在浏览器与本地应用之间流畅的切换
Chrome Custom Tabs最佳实践
Demo: https://github.com/hitherejoe/Tabby

猜你喜欢

转载自blog.csdn.net/K_Hello/article/details/81709040
今日推荐