Android 7.1.1 之实现 3D Touch
转载请注明出处:http://blog.csdn.net/yyh352091626/article/details/68962736
Shortcut概念
Shortcut 是Android-25(Android 7.1)新增的一项相似iOS的 3D Touch 功能的快捷方式组件。可是有着不同的表现形式。由于Android在硬件上不支持触摸压力感应,所以表现形式为长按,而iOS须用力长按。
首先。来个效果图
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveXloMzUyMDkxNjI2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" width="280" height="500">
在 Launcher 或 应用程序列表 里面。长按应用图标,弹出一个快捷方式列表。 而且,能够把单个快捷方式拖动出来作为一个桌面图标,拖出来的图标会随着清除应用数据或卸载应用而消失,须又一次创建。
详细实现
BuildConfig 配置
在主module下,改动 build.grade,使其使用 android-25 的 API 编译,当然,未下载的,就须要打开Android SDK Manager下载一下。
android {
compileSdkVersion 25
buildToolsVersion "25.0.0" // 或以上
defaultConfig {
targetSdkVersion 25
}
}
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveXloMzUyMDkxNjI2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" width="600">
静态配置
相似BroadCastReceiver,Shortcut注冊也分为静态注冊和动态注冊。首先介绍静态注冊,动态注冊后面继续~~
在
res/xml目录底下创建一个xml。举个栗子:shortcut.xml<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:enabled="true"
android:icon="@mipmap/ic_bar_detail_write"
android:shortcutDisabledMessage="@string/shortcut_publish"
android:shortcutId="publish"
android:shortcutLongLabel="@string/shortcut_publish"
android:shortcutShortLabel="@string/shortcut_publish">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.yanshi.writing.ui.bar.PublishPostActivity"
android:targetPackage="com.yanshi.writing" />
<categories android:name="android.shortcut.conversation" />
</shortcut> <shortcut
android:enabled="true"
android:icon="@mipmap/logo"
android:shortcutDisabledMessage="@string/shortcut_write"
android:shortcutId="write"
android:shortcutLongLabel="@string/shortcut_write"
android:shortcutShortLabel="@string/shortcut_write">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.yanshi.writing.ui.write.WriteActivity"
android:targetPackage="com.yanshi.writing" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
</shortcuts>1、enabled:表示当前快捷方式是否可使用
2、 icon: 快捷方式图标
3、 shortcutDisabledMessage: 快捷方式不可使用时显示的名字
4、 shortcutId:快捷方式标识
5、 shortcutLongLabel:长按下图标弹出来列表框中每一个快捷名
6、 shortcutShortLabel:快捷是能够单独显示在桌面上的,显示名为shortcutShortLabel
7、 targetClass:点击快捷方式进入的Activity
8、categories 默认写死就可以清单文件注冊
在 AndroidMainfest.xml 的默认启动页里加入meta-data标签配置<activity
android:name=".ui.MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoneTranslucent">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> <meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcut" />
</activity>- 完成! 能够到桌面查看效果了~~
动态配置
动态创建添加了菜单配置的灵活性,比方能够从服务端拉取快捷方式列表,再进行展示。详细配置方法例如以下:
创建
在须要注冊的地方加入例如以下代码:
/**
* 动态创建
*/
public void register() {
ShortcutManager mShortcutManager = getSystemService(ShortcutManager.class);
List<ShortcutInfo> infos = new ArrayList<>();
// 按下返回button跳转的activity
Intent intent1 = new Intent(this, MainActivity.class);
intent1.setAction(Intent.ACTION_VIEW);
// 目标activity
Intent intent2 = new Intent(this, PublishPostActivity.class);
intent2.setAction("com.yuyh.xxx.BACK");
Intent[] intents = new Intent[2];
intents[0] = intent1;
intents[1] = intent2;
ShortcutInfo info = new ShortcutInfo.Builder(this, "publish-2")
.setShortLabel("动态创建-公布帖子")
.setLongLabel("动态创建-公布帖子")
.setIcon(Icon.createWithResource(this, R.mipmap.ic_bar_detail_write))
.setIntents(intents)
.build();
infos.add(info);
mShortcutManager.setDynamicShortcuts(infos);
}
又一次执行app,再次长按,效果例如以下:
删除或禁用
动态删除能够删除动态配置的快捷方式。
/**
* 动态删除
*/
public void delete() {
ShortcutManager mShortcutManager = getSystemService(ShortcutManager.class);
/********* 移除弹出列表图标 **********/
// 全部动态创建图标
List<ShortcutInfo> infos1 = mShortcutManager.getDynamicShortcuts();
List<String> ids1 = new ArrayList<>();
for (ShortcutInfo info : infos1 ) {
ids1.add(info.getId());
}
// 禁用全部的快捷方式
mShortcutManager.disableShortcuts(ids1, "已禁用");
mShortcutManager.removeDynamicShortcuts(ids1);
/********* 移除拖出来的桌面快捷图标 **********/
// 放在桌面的图标
List<ShortcutInfo> infos2 = mShortcutManager.getPinnedShortcuts();
List<String> ids2 = new ArrayList<>();
for (ShortcutInfo info : infos2 ) {
ids2.add(info.getId());
}
mShortcutManager.disableShortcuts(ids2, "已禁用");
mShortcutManager.removeAllDynamicShortcuts();
}
代码比較简单。就不多做叙述了。 须注意一下 getPinnedShortcuts 方法与 getDynamicShortcuts 方法的差别!
禁用后的效果如图所看到的,图标变成灰色:
更新
快捷方式的唯一性。由前面提到的 shortcutId 这个标识符决定,所以更新快捷方式与创建快捷方式一样。 shortcutId 假设同样, 则会覆盖之前创建的快捷方式!
返回栈问题
当通过快捷方式打开时。现有的Activity都会被销毁,然后又一次创建一个Activity栈。
由于清单方式设置的快捷键的Intent不能自己定义Intent的Flag,其默认的Flag是 FLAG_ACTIVITY_NEW_TASK 和 FLAG_ACTIVITY_CLEAR_TASK。
通过动态注冊的方式,可发现。我们能够配置返回目标activity。当然,静态配置也能够实现。改动shortcut标签:
<shortcut
android:enabled="true"
android:icon="@mipmap/ic_bar_detail_write"
android:shortcutDisabledMessage="@string/shortcut_publish"
android:shortcutId="publish"
android:shortcutLongLabel="@string/shortcut_publish"
android:shortcutShortLabel="@string/shortcut_publish">
<!-- 返回目标activity -->
<intent
android:action="com.yuyh.xxx.BACK"
android:targetClass="com.yanshi.writing.ui.MainActivity"
android:targetPackage="com.yanshi.writing" />
<!-- 目标activity -->
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.yanshi.writing.ui.bar.PublishPostActivity"
android:targetPackage="com.yanshi.writing" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
感谢阅读。
Android 7.1.1 之实现 3D Touch的更多相关文章
- 从3D Touch 看 原生快速开发
全新的按压方式苹果继续为我们带来革命性的交互:Peek和Pop,Peek 和 Pop 让你能够预览所有类型的内容,甚至可对内容进行操作,却不必真的打开它们.例如,轻按屏幕,可用 Peek 预览收件箱中 ...
- iOS 3D Touch实践
本文主要讲解3DTouch各种场景下的开发方法,开发主屏幕应用icon上的快捷选项标签(Home Screen Quick Actions),静态设置 UIApplicationShortcutIte ...
- 初学3D Touch
引言 With iOS 9, new iPhone models add a third dimension to the user interface. A user can now press y ...
- iOS 3D Touch 适配开发
3D Touch的主要应用 文档给出的应用介绍主要有两块: 1.A user can now press your Home screen icon to immediately access fun ...
- 3D touch在Unity3D中的使用
0.开篇: 3D touch随着iOS9发布,它并不是一个单独的技术,而是可以分为pressure sensitivity.quick action以及peek&pop.在官方的介绍中提到可以 ...
- 3D Touch介绍:电子秤App与快捷操作
随着iPhone6s与6s plus的到来,苹果给我们展现了一种全新的交互方式:重按手势.你可能知道,这个特性已经在Apple Watch和MacBook上推出了,不过那时叫Force Touch,就 ...
- iOS 3D touch 使用技巧
第一个 在桌面中3d Touch 打开菜单 由于本人纯属代码党,本次实现方法也只使用代码实现 到达到这个效果并不难,只需要在appdelegate中实现以下代码即可 ,当然也有缺点,就是这个app没运 ...
- 3D Touch
一.认识3D Touch 1.硬件和操作系统要求 iPhone 6s或者iPhone 6s Plus 操作系统要求 ios9+ 2.3D Touch的交互效果 QuickAct ...
- 3D touch的 使用心得
一.设置图标touch 快捷进入 1.静态标签 静态标签是我们在项目的配置plist文件中配置的标签,在用户安装程序后就可以使用,并且排序会在动态标签的前面. 我们先来看静态标签的配置: 首先,在in ...
随机推荐
- [Node.js]操作redis
摘要 在实际开发中,免不了要操作mysql,mongodb,redis等数据存储服务器.这里先简单介绍如何操作redis. 一个例子 关于redis服务端的安装这里不再介绍,重点不在这里.感兴趣的可以 ...
- div与span区别及用法
DIV与SPAN区别及div与san用法篇 接下来了解在div+css开发的时候在html网页制作,特别是标签运用中div和span的区别及用法.新手在使用web标准(div css)开发网页的时候, ...
- poj Kaka's Matrix Travels
Kaka's Matrix Travels 题目: 给出一个矩阵.求仅仅能向下或者向右的情况下能得到的最大和.一般的是指遍历一次,而这个是能够反复走K次.每经过一次后就把该点设为0.求最大和. 算法: ...
- java.security.InvalidKeyException: Illegal key size aes解密失败
使用微信时定期提示:java.security.InvalidKeyException: Illegal key size和 com.qq.weixin.mp.aes.AesException: ae ...
- 转: Linux --- Supervisor的作用与配置
supervisor管理进程,是通过fork/exec的方式将这些被管理的进程当作supervisor的子进程来启动,所以我们只需要将要管理进程的可执行文件的路径添加到supervisor的配置文件中 ...
- Mac下使用XLD转换无损音乐Ape
最近想要给音乐库增加一些音乐,下载了一些Ape格式的无损音乐,但是无法直接导入到iTunes中,必须经过其他工具转换成苹果的无损格式,XLD就是这样一款工具.XLD的下载和安装非常方便,直接将APP拖 ...
- Orchard模块开发全接触6:自定义用户注册
我们都知道 Orchard 的用户注册相当简单,现在,我们需要一个自定义的用户注册,现在,开始吧. 一:定义实体 Models/CustomerPartRecord.cs: public class ...
- HTML5 本地文件操作之FileSystemAPI实例(四)
目录操作Demo二 1.删除目录 window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSyst ...
- jpa命名规则 jpa使用sql语句 @Query
关键字方法命名sql where字句 AndfindByNameAndPwdwhere name= ? and pwd =? OrfindByNameOrSexwhere name= ? or sex ...
- 看看Spring的源码(一)——Bean加载过程
首先Web项目使用Spring是通过在web.xml里面配置org.springframework.web.context.ContextLoaderListener初始化IOC容器的. <li ...