【lua教程】指南针
--Copyright© Ayaka_Ago. All Rights Reserved.require"import"
import"android.hardware.SensorEventListener"
import"android.hardware.SensorManager"
import"android.hardware.Sensor"
--setOrientation(0)
h=activity.getHeight()--
w=activity.getWidth()
import "android.graphics.ColorFilter"
import "android.hardware.camera2.CameraManager"
import "android.widget.PageView"
import "android.hardware.SensorEventListener"
import "android.hardware.Sensor"
import "android.widget.FrameLayout"
import "android.content.Context"
import "android.widget.ImageView"
import "android.hardware.SensorManager"
import "android.widget.TextView"
import "android.widget.LinearLayout"
import "android.widget.CircleImageView"
import "com.nirenr.Color"
import "android.graphics.Color"
import "android.text.Html"
import "android.graphics.drawable.GradientDrawable"
activity.setTheme(android.R.style.Theme_DeviceDefault_Light)--设置md主题
local CameraManager=this.getSystemService(Context.CAMERA_SERVICE)--相机
local sensorManager=this.getSystemService(Context.SENSOR_SERVICE)--传感器
local OrientationSensor=sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION)--方向
local curp=0
local function rim(radiu,da,db,c,inside)
return GradientDrawable().setShape(GradientDrawable.RECTANGLE).setStroke(10,c or 文字色,da,db).setColor(inside or 0).setCornerRadii({radiu,radiu,radiu,radiu,radiu,radiu,radiu,radiu}).setGradientType(1)
end
aa={
FrameLayout,
{
PageView,
OverScrollMode=2,
layout_width="fill",
layout_height="fill",
onPageChangeListener={
onPageSelected=function (p)
curp=p
if p==0 then
compass_title.setText("指南针")
compass_indicator.getChildAt(0).setColorFilter(Color.DKGRAY)
compass_indicator.getChildAt(1).setColorFilter(Color.LTGRAY)
else
compass_title.setText("水平仪")
compass_indicator.getChildAt(0).setColorFilter(Color.LTGRAY)
compass_indicator.getChildAt(1).setColorFilter(Color.DKGRAY)
end
end},
id="pgs",
pages={
{
LinearLayout,
orientation="vertical",
gravity="center",
layout_width="fill",
{
FrameLayout,
layout_width="250dp",
layout_height="250dp",
layout_gravity="center",
id="compass_circle",
{
FrameLayout,
backgroundDrawable=rim(w,w*0.008,w*0.016),
layout_width="fill",
layout_height="fill",
},
{
TextView,
layout_gravity="top|center",
id="compass_north",
textSize="20dp",
gravity="center",
layout_marginTop="16dp",
text="N",
},
{
TextView,
id="compass_west",
textSize="20dp",
layout_marginLeft="16dp",
layout_gravity="center|left",
text="W",
},
{
ImageView,
src="icon.png",
layout_height="42dp",
layout_gravity="center",
layout_width="42dp",
rotation=90,
ColorFilter=强调色,
},
{
TextView,
id="compass_east",
layout_marginRight="16dp",
textColor=文字色,
textSize="20dp",
layout_gravity="center|right",
text="E",
},
{
TextView,
layout_gravity="bottom|center",
textColor=文字色,
id="compass_south",
textSize="20dp",
layout_marginBottom="16dp",
text="S",
},
},
{
TextView,
id="current_dir",
layout_width="fill",
textSize="25dp",
text="北",
layout_marginTop="56dp",
gravity="center",
},
{
TextView,
id="compass_degree",
text="0",
textSize="25dp",
layout_marginTop="12dp",
gravity="center",
layout_width="fill",
},
},
{
FrameLayout,
layout_width="fill",
layout_height="fill",
{
FrameLayout,
backgroundDrawable=rim(w,h,0,文字色),
layout_width="148dp",
layout_gravity="center",
id="compass_balanceline",
layout_height="148dp",
},
{
FrameLayout,
-- foreground=rim(w,h,0,图标色,图标色),
layout_width="148dp",
layout_gravity="center",
id="compass_balance",
layout_height="148dp",
},
{
LinearLayout,
layout_width="fill",
layout_marginBottom="64dp",
layout_gravity="bottom",
{
LinearLayout,
layout_weight=1,
gravity="center",
orientation="vertical",
{
TextView,
id="balance_x",
text="0",
textSize="16dp",
textColor=文字色,
},
{
TextView,
text="X",
textSize="14dp",
textColor=次要文字色,
},
},
{
LinearLayout,
layout_weight=1,
gravity="center",
orientation="vertical",
{
TextView,
id="balance_y",
text="0",
textSize="16dp",
textColor=文字色,
},
{
TextView,
text="Y",
textSize="14dp",
textColor=次要文字色,
},
},
},
},
},--pages
},--pageview
{
LinearLayout,
layout_width="fill",
layout_gravity="bottom",
gravity="center",
layout_marginBottom="24dp",
id="compass_indicator",
{
CircleImageView,
src="drawable/dot.png",
layout_width="6dp",
--colorFilter=Color.DKGRAY,
layout_height="6dp",
layout_marginRight="4dp",
},
{
CircleImageView,
src="drawable/dot.png",
--colorFilter=Color.GRAY,
layout_width="6dp",
layout_height="6dp",
},
},
{
LinearLayout,
layout_height="52dp",
{
TextView,
textSize="18dp",
textColor=图标色,
text="指南针",
layout_height="fill",
paddingLeft="16dp",
paddingRight="8dp",
id="compass_title",
gravity="center|left",
},
{--标题旁边的问号
ImageView,
layout_width="52dp",
layout_height="fill",
padding="16dp",
onClick=function()
a.fun.dia("指南针与水平仪",Html.fromHtml("\t\t校准指南针:<br><br>\t\t· 绕 8 字转动手机<br>\t\t· 远离磁场干扰"),"我知道了")
end,
--foreground=波纹(),
src="drawable/help_circle.png",
id="compass_question",
colorFilter=图标色,
--visibility=8,
},
},
}
activity.setContentView(loadlayout(aa))
if OrientationSensor then--有这个传感器
function onResume()
if not listener then
listener=SensorEventListener({
onSensorChanged=function(e)
infoChnaged=true
local z=e.values
local y=e.values
local x=e.values
if z>330 or z<45 then
current_dir.setText("北")
elseif z>315 then
current_dir.setText("西北")
elseif z>255 then
current_dir.setText("西")
elseif z>220 then
current_dir.setText("西南")
elseif z>165 then
current_dir.setText("南")
elseif z>125 then
current_dir.setText("东南")
elseif z>75 then
current_dir.setText("东")
elseif z>=45 then
current_dir.setText("东北")
end
compass_circle.setRotation(360-z)
compass_south.setRotation(z)
compass_west.setRotation(z)
compass_east.setRotation(z)
compass_north.setRotation(z)
compass_degree.setText(" "..tointeger(z).."°")
compass_balance.setTranslationX(x*3.5)
compass_balance.setTranslationY(y*3.5)
balance_x.setText(""..tointeger(x))
balance_y.setText(""..tointeger(y))
end})
sensorManager.registerListener(listener,OrientationSensor,SensorManager.SENSOR_DELAY_FASTEST)
end
end
function onPause()
if listener then
sensorManager.unregisterListener(listener)
listener=nil
end
end
function onDestroy()
if listener then
sensorManager.unregisterListener(listener)
listener=nil
end
end
task(1000,function()
if not infoChnaged then
onResume()
end
end)
function onKeyDown(k)
if k==4 and curp~=0 then
pgs.showPage(0)
return true
end
end
else--没这个传感器
print("您的设备不支持此功能")
this.finish()
end
页:
[1]