wangy 发表于 2021-6-30 23:15:56

【lua教程】指南针代码

--程序启动时会执行的事件
function 物理效果(id,灵敏度)
if not (__IIll00oo==nil)then
    error("此代码仅允许运行一次,若需对多个控件操作的,请在id处输入一个table")
end
if not (type(id)=="table")then
    __IIll00oo={}
    table.insert(__IIll00oo,id)
   else
    __IIll00oo=id
end
local l = 灵敏度 or 5
require "import"
import "android.content.Context"
import "android.hardware.Sensor"
import "android.hardware.SensorEvent"
import "android.hardware.SensorEventListener"
import "android.hardware.SensorManager"
local x轴,y轴,z轴
传感器 = activity.getSystemService(Context.SENSOR_SERVICE)
local 加速度传感器 = 传感器.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
传感器.registerListener(SensorEventListener({
    onSensorChanged=function(event)
      local x轴 = event.values
      local y轴 = event.values
      local z轴 = event.values
      for v,k in pairs(__IIll00oo) do
      k.setRotationX(y轴*l)
      k.setRotationY(x轴*l)
      end
    end,nil}), 加速度传感器, SensorManager.SENSOR_DELAY_NORMAL)
end
   
   指南针=
{
LinearLayout;
orientation="vertical";
layout_width="fill";
layout_height="fill";
id="coe",
gravity="center";
{
    TextView;
    id="dir";
    layout_width="fill";
    textColor="#0090FF";
    layout_height="7%h";
    textSize="25dp";
    gravity="center";
    text="南";
};
{


    LinearLayout;
    id="cors";
    orientation="vertical";
    layout_width="70%w";
    gravity="center";
    layout_height="70%w";
    {
      LinearLayout;
      layout_gravity="center";
      orientation="vertical";
      id="cor";
      layout_width="50%w";
      gravity="start";
      layout_height="50%w";
      {
      TextView;
      layout_gravity="center";
      textColor="#0088ff";
      id="n";
      textSize="20dp";
      gravity="center";
      text="N";
      };
      {
      LinearLayout;
      layout_width="fill";
      layout_marginTop="6%h";
      layout_height="8%h";
      {
          TextView;
          id="w";
          textColor="0xff0088ff";
          paddingLeft="6dp";
          textSize="20dp";
          gravity="left";
          text="W";
      };
      {
          TextView;
          layout_width="16.5%w";
          layout_marginLeft="9%w";
          textColor="0xff0088ff";
          layout_height="8%h";
          textSize="27dp";
          gravity="center";
          text="";
      };
      {
          TextView;
          layout_marginRight="6dp";
          id="e";
          layout_marginLeft="11%w";
          textColor="0xff0088ff";
          textSize="20dp";
          layout_width="6%w";
          gravity="right";
          text="E";
      };
      };
      {
      TextView;
      layout_gravity="center";
      layout_marginTop="11%h";
      textColor="#0088ff";
      id="s";
      textSize="20dp";
      gravity="center";
      text="S";

      };
    };

};
{
    TextView;
    id="deg";
    textColor="0xff0088ff";
    layout_height="8%h";
    textSize="27dp";
    gravity="center";
    text="0°";
};
};

webView.addView(loadlayout(指南针))

import "android.content.Context"
import "android.hardware.SensorManager"
import "android.hardware.SensorEventListener"
import "android.hardware.Sensor"
dirs=""
FPS=60
磁场=0
传感器 = activity.getSystemService(Context.SENSOR_SERVICE)
local 磁场传感器 = 传感器.getDefaultSensor(Sensor.TYPE_ORIENTATION)
传感器.registerListener(SensorEventListener({
onSensorChanged=function(event)
    磁场 = event.values
end,nil}), 磁场传感器, SensorManager.SENSOR_DELAY_NORMAL)
function CircleButton(view,InsideColor,radiu,da,db)
import "android.graphics.drawable.GradientDrawable"
drawable = GradientDrawable()
drawable.setShape(GradientDrawable.RECTANGLE)
drawable.setStroke(10, 0xff0088ff,da,db)
drawable.setColor(InsideColor)
drawable.setCornerRadii({radiu,radiu,radiu,radiu,radiu,radiu,radiu,radiu});
drawable.setGradientType(1)
view.setBackgroundDrawable(drawable)
end
角度=activity.getWidth()
控件id=cor
控件颜色=0xFFFFFFFF
CircleButton(控件id,控件颜色,角度,2,2)
CircleButton(cors,控件颜色,角度,(activity.getWidth()*0.8)/2,10)
tick=Ticker()
tick.Period=1000/FPS
tick.onTick=function()
if 磁场>165 and 磁场<195 then
    dirs="南"
elseif 磁场>195 and 磁场<255 then
    dirs="西南"
elseif 磁场>255 and 磁场<285 then
    dirs="西"
elseif 磁场>285 and 磁场<345 then
    dirs="西北"
elseif 磁场>345 or 磁场<15 then
    dirs="北"
elseif 磁场>15 and 磁场<75 then
    dirs="东北"
elseif 磁场>75 and 磁场<105 then
    dirs="东"
elseif 磁场>105 and 磁场<165 then
    dirs="东南"
end
dir.setText(dirs)
cors.setRotation(360-磁场)
w.setRotation(磁场)
s.setRotation(磁场)
n.setRotation(磁场)
e.setRotation(磁场)
deg.setText(tostring(tointeger(磁场).."°"))
--Toast.makeText(activity,tostring(磁场.."°"),Toast.LENGTH_SHORT).show()
end
tick.start()

function onDestroy()
tick.stop()
传感器.unregisterListener(Listener);
end

function onTouchEvent(event)
ac=event.action
if ac==2 or ac==0 then
    x=event.X
    y=event.Y
    coe.setRotationX((activity.getHeight()-y)/30)
    coe.setRotationY(x/30)
elseif ac==1 then
    coe.setRotationX(0)
    coe.setRotationY(0)
end
end
--coe.setRotationX(activity.getHeight()*0.33)
dir.getPaint().setFakeBoldText(true)
deg.getPaint().setFakeBoldText(true)
   
import "android.content.Context"
import "android.hardware.SensorManager"
import "android.hardware.SensorEventListener"
import "android.hardware.Sensor"
dirs=""
FPS=50
磁场=0
传感器 = activity.getSystemService(Context.SENSOR_SERVICE)
local 磁场传感器 = 传感器.getDefaultSensor(Sensor.TYPE_ORIENTATION)
传感器.registerListener(SensorEventListener({
onSensorChanged=function(event)
    磁场 = event.values
end,nil}), 磁场传感器, SensorManager.SENSOR_DELAY_NORMAL)
function CircleButton(view,InsideColor,radiu,da,db)
import "android.graphics.drawable.GradientDrawable"
drawable = GradientDrawable()
drawable.setShape(GradientDrawable.RECTANGLE)
drawable.setStroke(10, 0xff0088ff,da,db)
drawable.setColor(InsideColor)
drawable.setCornerRadii({radiu,radiu,radiu,radiu,radiu,radiu,radiu,radiu});
drawable.setGradientType(1)
view.setBackgroundDrawable(drawable)
end
角度=activity.getWidth()
控件id=cor
控件颜色=0xFFFFFFFF
CircleButton(控件id,控件颜色,角度,2,2)
CircleButton(cors,控件颜色,角度,(activity.getWidth()*0.8)/2,10)
tick=Ticker()
tick.Period=1000/FPS
tick.onTick=function()
if 磁场>165 and 磁场<195 then
    dirs="南"
elseif 磁场>195 and 磁场<255 then
    dirs="西南"
elseif 磁场>255 and 磁场<285 then
    dirs="西"
elseif 磁场>285 and 磁场<345 then
    dirs="西北"
elseif 磁场>345 or 磁场<15 then
    dirs="北"
elseif 磁场>15 and 磁场<75 then
    dirs="东北"
elseif 磁场>75 and 磁场<105 then
    dirs="东"
elseif 磁场>105 and 磁场<165 then
    dirs="东南"
end
dir.setText(dirs)
cors.setRotation(360-磁场)
w.setRotation(磁场)
s.setRotation(磁场)
n.setRotation(磁场)
e.setRotation(磁场)
deg.setText(tostring(tointeger(磁场).."°"))
--Toast.makeText(activity,tostring(磁场.."°"),Toast.LENGTH_SHORT).show()
end
tick.start()

function onDestroy()
tick.stop()
传感器.unregisterListener(Listener);
end

function onTouchEvent(event)
ac=event.action
if ac==2 or ac==0 then
    x=event.X
    y=event.Y
    coe.setRotationX((activity.getHeight()-y)/30)
    coe.setRotationY(x/30)
elseif ac==1 then
    coe.setRotationX(0)
    coe.setRotationY(0)
end
end
--coe.setRotationX(activity.getHeight()*0.33)
dir.getPaint().setFakeBoldText(true)
deg.getPaint().setFakeBoldText(true)
物理效果(coe,4)
页: [1]
查看完整版本: 【lua教程】指南针代码