User Tools

Site Tools


script_parallax_wallpaper

====== About the script ====== * Purpose : Background wallpaper that moves with accelerometer (parallax effect) The script takes the accelerometer values and place the item accordingly. It is size-dependent so the bigger the item is the more it will move. (Items smaller that the screen size will move a bit odd) * Author : [[https://plus.google.com/u/1/105066926163073195690|TrianguloY]] with the help of F43nd1r. All the new bind class, and accelerometer events code was made by Lukas (also some suggestions), the position computation, tags and relative is mine. * Link: https://plus.google.com/+TrianguloY/posts/XqiqsFmcnDS ====== How to use the script ====== The settings are a parameter to control the lerp (smooth change from the previous value to the new one) otherwise it will move a lot. The other is a threshold to avoid small movements if your accelerometer sends changes even when the device rest, for low end devices. Instructions: - Choose an item. Remember to set a picture as background and hide the icon and text (or not, that's up to you) - Set this script in the resumed event of the item - Important: detach the item - Optional but recommended: set the script in the paused event of the item - Optional: you may want to pin the item too. The movement is stopped automatically when the launcher lost focus, but I recommend also to set the script in the paused event (specially if you use different desktops). It is made so if you run it from other events (a swipe or even the scripts menu) it stops. Useful if you want to edit the item. To reload the script just turn off/on the screen. You may want to do it when setting the event and when changing the size of the item. It works in folders and in panels too. And also you can set it in as much items as you want (with caution) The second video has two items, the galaxy background and the stars, both items has different sizes. Note: I needed to add a small code to avoid the '0-size' bug. If you get an error when the container is loaded just turn off/on the screen. Also I couldn't test it myself long enough. If you get an error or it don't work as expected write a comment. Version 2: now it works on portrait and landscape ====== Script code ====== **New version** <sxh javascript> //Version 2 var delta=0.04;//between (0,1], to avoid shaking due to accelerometer fast changes ( try low values like 0.1) var threshold=0;//minimum pixels required to move it, if it shakes when the device rest (try 1,2...) var accuracy=1; //accuracy of the Sensor. 0 is the fastest but can consume a bit more battery. 3 is battery-friendly, but can be slower. 1 and 2 are a medium value. Default 1 var tablet = false; //set this to true if your device is a tablet or if the item is moving vertical instead of horizontal and the opposite var data=LL.getEvent().getData(); if(data=="")data=null; var gyro=LL.getEvent().getItem()||LL.getItemById(data||-1); if(gyro==null){ alert("run this script from an item"); return; } var source=LL.getEvent().getSource(); //disable when desired if(source!="I_RESUMED" && data==null) { gyro.setTag("parallax",null); return; } var cont=gyro.getParent(); //small tweak to avoid strange bugs that happen when the script is run directly //this only runs itself if(gyro.getTag("parallax")!="repeat"&&cont.getHeight()==0){ gyro.setTag("parallax","repeat"); var name=LL.getCurrentScript().getName(); setTimeout(function(){LL.runScript(name,gyro.getId())},0); return; } var token=Math.random(); while(token==gyro.getTag("parallax"))token=Math.random(); gyro.setTag("parallax",token); var g=[gyro.getWidth()*gyro.getScaleX()-cont.getWidth(),gyro.getHeight()*gyro.getScaleY()-cont.getHeight()]; var portrait=cont.getWidth()<cont.getHeight(); var xy=[,]; LL.bindClass("android.hardware.Sensor"); LL.bindClass("android.hardware.SensorEventListener"); LL.bindClass("android.hardware.SensorManager"); var sm = LL.getContext().getSystemService(Context.SENSOR_SERVICE); var orientation = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); var i = { onAccuracyChanged: function(sensor,accuracy){}, onSensorChanged: function(event) { if(LL.isPaused()){ gyro.setTag("parallax",null); sm.unregisterListener(l); return; } if(gyro.getTag("parallax")!=token){ sm.unregisterListener(l); return; } if(portrait!=(cont.getWidth()<cont.getHeight())){ sm.unregisterListener(l); cont.setTag("parallax",null); var name=LL.getCurrentScript().getName(); setTimeout(function(){LL.runScript(name,gyro.getId())},0); return; } if(portrait!=tablet){ var v=[-event.values[0],event.values[1]]; }else{ var v=[event.values[1],event.values[0]]; } var x=(v[0]+10)/20; var y=(v[1]+10)/20; if(xy[0]==null)xy=[x,y]; x=(x>1)?1:(x<0)?0:x; y=(y>1)?1:(y<0)?0:y //var flags=[true,true]; if(Math.abs(x-xy[0])*delta*g[0]<threshold)x=xy[0]; if(Math.abs(y-xy[1])*delta*g[1]<threshold)y=xy[1]; xy=[xy[0]+(x-xy[0])*delta,xy[1]+(y-xy[1])*delta]; gyro.setPosition(-g[0]*xy[0],-g[1]*xy[1]); } }; var l = new SensorEventListener(i); sm.registerListener(l, orientation,accuracy); </sxh> **Old version** <sxh javascript> //Version 1 var delta=0.04;//between (0,1], to avoid shaking due to accelerometer fast changes ( try low values like 0.1) var threshold=0;//minimum pixels required to move it, if it shakes when the device rest (try 1,2...) var accuracy=1; //accuracy of the Sensor. 0 is the fastest but can consume a bit more battery. 3 is battery-friendly, but can be slower. 1 and 2 are a medium value. Default 1 var gyro=LL.getEvent().getItem()||LL.getItemById(LL.getEvent().getData()||-1); if(gyro==null){ alert("run this script from an item"); return; } //disable when desired if(LL.getEvent().getSource()!="I_RESUMED"&&LL.getEvent().getData()==null) { gyro.setTag("parallax",null); return; } var cont=gyro.getParent(); //allowed only one if(gyro.getTag("parallax")=="active")return; if(gyro.getTag("parallax")!="repeat"){ gyro.setTag("parallax","repeat"); var name=LL.getCurrentScript().getName(); setTimeout(function(){LL.runScript(name,gyro.getId())},0); return; } gyro.setTag("parallax","active"); var g=[gyro.getWidth()*gyro.getScaleX()-cont.getWidth(),gyro.getHeight()*gyro.getScaleY()-cont.getHeight()]; xy=[,]; LL.bindClass("android.hardware.Sensor"); LL.bindClass("android.hardware.SensorEventListener"); LL.bindClass("android.hardware.SensorManager"); var sm = LL.getContext().getSystemService(Context.SENSOR_SERVICE); var orientation = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); var i = { onAccuracyChanged: function(sensor,accuracy){}, onSensorChanged: function(event) { if(LL.isPaused()||gyro.getTag("parallax")!="active"){ sm.unregisterListener(l); gyro.setTag("parallax",null); return; } var x=(-event.values[0]+10)/20; var y=(event.values[1]+10)/20; if(xy[0]==null)xy=[x,y]; x=(x>1)?1:(x<0)?0:x; y=(y>1)?1:(y<0)?0:y //var flags=[true,true]; if(Math.abs(x-xy[0])*delta*g[0]<threshold)x=xy[0]; if(Math.abs(y-xy[1])*delta*g[1]<threshold)y=xy[1]; xy=[xy[0]+(x-xy[0])*delta,xy[1]+(y-xy[1])*delta]; gyro.setPosition(-g[0]*xy[0],-g[1]*xy[1]); } }; var l = new SensorEventListener(i); sm.registerListener(l, orientation,accuracy); </sxh>

script_parallax_wallpaper.txt · Last modified: 2018/08/14 16:39 by f43nd1r