跳到主要内容

锁屏、桌面、桌面万象小组件联动<globalPersist>

功能概述

通过在锁屏、桌面和桌面万象小组件的变量声明脚本中,同时给变量的globalPersist属性设置为true,可以实现锁屏、锁屏和桌面万象小组件使用同一个变量的值。

globalPersist是锁屏、桌面和桌面万象小组件变量同步的桥梁,可以将锁屏上变量的变化传递给桌面和桌面万象小组件,实现更多的创意主题。

应用场景

  • 在锁屏中双击切换壁纸、桌面和桌面万象小组件的背景图片同步变化。

XML规范

<Var name="cartoon" expression="1" type="number" globalPersist="true"/>

参数说明

参数类型选项注释
globalPersistboolean选填变量全局持久化;true或者false,默认为false。设置为true即开启变量全局持久化,会优先读取本地同名变量是否有值,该变量会在锁屏、桌面、折叠屏不同状态(折叠、打开、翻转)中同步,如果没有则赋予expression或者默认值。注意不可以和persist同时使用。

应用示例

示例 一:锁屏上设置红、白、黑3张壁纸,双击切换壁纸颜色。在锁屏上切换壁纸颜色后,将变化同步给桌面和桌面万象小组件,桌面和桌面万象小组件的壁纸颜色同步变化。

  • 锁屏脚本
<Lockscreen displayDesktop="true" frameRate="60" id="201801194068" screenWidth="1080" version="1">
<Var name="bg_id" expression="0" globalPersist="true" />

<Image src="bg_0.png" visibility="eq(#bg_id,0)" x="0" y="0" scaleType="center_crop" isBackground="true"/>
<Image src="bg_1.png" visibility="eq(#bg_id,1)" x="0" y="0" scaleType="center_crop" isBackground="true"/>
<Image src="bg_2.png" visibility="eq(#bg_id,2)" x="0" y="0" scaleType="center_crop" isBackground="true"/>
<Video name="red" visibility="eq(#bg_id,0)" x="0" y="0" scaleType="center_crop" isBackground="true" src="red.mp4" sound="0" play="true" looping="true"/>
<Video name="white" visibility="eq(#bg_id,1)" x="0" y="0" scaleType="center_crop" isBackground="true" src="white.mp4" sound="0" play="true" looping="true"/>
<Video name="black" visibility="eq(#bg_id,2)" x="0" y="0" scaleType="center_crop" isBackground="true" src="black.mp4" sound="0" play="true" looping="true"/>

<Button h="#screen_height" w="#screen_width" x="0" y="0">
<Triggers>
<Trigger action="double">
<VariableCommand name="bg_id" expression="(#bg_id+1)%3" />
</Trigger>
</Triggers>
</Button>

</Lockscreen>
  • 桌面脚本
<CommonWallpaper displayDesktop="true" frameRate="60" id="201801194068" screenWidth="1080" version="1">
<Image src="bg_0.jpg" visibility="eq(#bg_id,0)" x="0" y="0" scaleType="center_crop" isBackground="true"/>
<Image src="bg_1.jpg" visibility="eq(#bg_id,1)" x="0" y="0" scaleType="center_crop" isBackground="true"/>
<Image src="bg_2.jpg" visibility="eq(#bg_id,2)" x="0" y="0" scaleType="center_crop" isBackground="true"/>
</CommonWallpaper>
  • 桌面万象小组件脚本
<Widget version="1" frameRate="30" displayDesktop="true" screenWidth="940" screenHeight="940">
<Group x="0" y="0" visibility="true">
<Image x="0" y="0" src="clock_1.png" visibility="eq(#bg_id,0)"/>
<Image x="0" y="0" src="clock_2.png" visibility="eq(#bg_id,1)"/>
<Image x="0" y="0" src="clock_3.png" visibility="eq(#bg_id,2)"/>
<Image x="435" y="90" pivotX="35" pivotY="380" src="minute.png" rotation="360*(#minute/60)" />
<Image x="435" y="90" pivotX="35" pivotY="380" src="hour.png" rotation="360*(#hour12/12)+30*(#minute/60)" />
<Image x="435" y="90" pivotX="35" pivotY="380" src="second.png" rotation="360*(#second/60)" />
</Group>

<Button x="0" y="0" w="940" h="940">
<Trigger action="click">
<IntentCommand action="android.intent.action.MAIN" package="com.android.deskclock" class="com.android.deskclock.AlarmsMainActivity"/>
</Trigger>
</Button>
</Widget>

示例 二:锁屏与桌面的背景图片同步变化

为变量cartoon声明globalPersist="true":即锁屏和桌面中cartoon的值将同步变化。cartoon作为image标签的资源id控制参数,可以控制图片资源变化。双击屏幕可以改变cartoon的值,以显示对应的图片资源,同时锁屏和桌面的图片会同步变化。注意:需要给锁屏和桌面设置相同的或相关联的图片资源。

  • 锁屏脚本
<Lockscreen frameRate="60" screenWidth="1080" version="1">
<Image h="#screen_height" src="bg.jpg" w="#screen_width" />
<Var name="cartoon" expression="1" type="number" globalPersist="true"/>
<Image h="#screen_height" w="#screen_width" src="tu/wallpaper.jpg" srcid="#cartoon" alpha="200"/>
<Button h="#screen_height" w="#screen_width">
<Trigger action="double">
<VariableCommand name="cartoon" expression="#cartoon+1" condition="lt(#cartoon,6)"/>
<VariableCommand name="cartoon" expression="1" condition="eq(#cartoon,6)" />
</Trigger>
</Button>
<Unlocker bounceAcceleration="3000" bounceInitSpeed="2000" name="unlocker">
<StartPoint h="#screen_height" w="#screen_width" x="0" y="0"/>
<EndPoint h="200" w="#screen_width" x="0" y="-350">
<Path h="#screen_height" w="#screen_width" x="0" y="0">
<Position x="0" y="0" />
<Position x="0" y="-200" />
</Path>
</EndPoint>
</Unlocker>
</Lockscreen>
  • 桌面脚本
<CommonWallpaper frameRate="60" screenWidth="1080" version="1">
<Image h="#screen_height" src="bg.jpg" w="#screen_width" />
<Var name="cartoon" expression="1" type="number" globalPersist="true" />
<Image h="#screen_height" w="#screen_width" src="tu/wallpaper.jpg" srcid="#cartoon" alpha="200"/>
<Button h="#screen_height" w="#screen_width">
<Trigger action="double">
<VariableCommand name="cartoon" expression="#cartoon+1" condition="lt(#cartoon,6)" />
<VariableCommand name="cartoon" expression="1" condition="eq(#cartoon,6)" />
</Trigger>
</Button>
</CommonWallpaper>