|
- --程序启动时会执行的事件
5 m; c- v& Z: `) p) w8 Y - function 物理效果(id,灵敏度)3 M! {" l! ~6 n, N6 j
- if not (__IIll00oo==nil)then0 G- ]+ {- ?4 A
- error("此代码仅允许运行一次,若需对多个控件操作的,请在id处输入一个table")
) ]# D6 ]8 i( [% q4 Y$ ]0 g7 Q - end6 x( q$ Y/ `( w. R4 n* u0 t) H
- if not (type(id)=="table")then
* z/ _% }) M* a+ \% R' g* z - __IIll00oo={}
3 L: z8 o" n+ J5 J+ w5 Q, Z3 I" ]) n - table.insert(__IIll00oo,id)
. L0 u5 _9 E1 ]0 X, G9 J - else3 W2 \0 Y1 u' x$ Y$ M# E; R, Y
- __IIll00oo=id4 V& H7 i" _+ I) ~! z' {: Z% Z7 s( ]
- end
' L" D# G# }( ^7 ~2 T - local l = 灵敏度 or 5
$ d$ r$ _. q0 \4 K2 e - require "import"
0 G9 `, l% s( P. n) |. A% z0 S1 k - import "android.content.Context"' D! J: R* K4 d
- import "android.hardware.Sensor"
5 [/ w% n/ v0 u3 C4 ~- j* ?5 s$ t5 t - import "android.hardware.SensorEvent"8 i& n. T4 j- T. d, T( o
- import "android.hardware.SensorEventListener"
2 T1 D) c4 A; N8 q - import "android.hardware.SensorManager"9 E3 f+ w; @0 Q2 }1 E
- local x轴,y轴,z轴. H) d: h: G7 i# C) X [1 R
- 传感器 = activity.getSystemService(Context.SENSOR_SERVICE): i# l$ Y! D8 ]
- local 加速度传感器 = 传感器.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)% |0 t. Z' z! k8 V+ x
- 传感器.registerListener(SensorEventListener({
$ b$ E* e: ~4 @( n - onSensorChanged=function(event)+ \% c0 h+ ^ D) s, V5 ?9 t& m- d" ^
- local x轴 = event.values[0]
3 {; s. d. O b/ Z+ s - local y轴 = event.values[1]" X3 L, ~) p% F. M: p2 v L$ _% \
- local z轴 = event.values[2]
7 D! }5 ^! y9 ]8 p+ o - for v,k in pairs(__IIll00oo) do' ]9 s3 B# V' {
- k.setRotationX(y轴*l)- d9 B# b2 _" }; W
- k.setRotationY(x轴*l)
) s) R* C, [, y0 J7 c% c - end) y- }* X6 S$ R( M5 |5 V+ O$ _
- end,nil}), 加速度传感器, SensorManager.SENSOR_DELAY_NORMAL)
/ }+ v. W7 O( _9 p3 y - end( z" a7 F- ?: K0 X
-
1 H) \9 ?, @! d - 指南针=
/ g$ o/ l# T" D; [" J) Z - {
8 m- x$ A) G5 G; X7 k6 H - LinearLayout;
1 U) @; B+ s" K3 G R) a' e - orientation="vertical";
' S! C, f( \3 d' S8 r2 z - layout_width="fill";$ K$ p: V8 @4 e+ @
- layout_height="fill";
! q6 R6 v- j$ W% W' R - id="coe",' }9 x" W/ B" h4 L9 B# B% [5 e
- gravity="center";
1 t# @8 r% V! G- D' T - {$ h. |0 O" l3 ^- S0 ?' A
- TextView;) @3 s3 W2 Z6 O
- id="dir";
+ V; K' @* s- M2 T& e1 Z9 Z5 k _ - layout_width="fill";
0 Z$ ^& Q; H( ^6 Y1 g - textColor="#0090FF";
" j4 T9 i9 y ~$ T% x7 C! r - layout_height="7%h";8 G$ }4 d; N, a E! {; a; k5 h7 w
- textSize="25dp";
6 R4 {) ?: h; Y2 R - gravity="center";+ q! s6 N* i9 S4 j0 o q7 A: w
- text="南";8 H8 X: s4 ^# j) L; K% A# Y
- };
: o4 a; k, _8 I8 t) J - {; I E# s2 o. i% U i
- 5 x7 ?; ~( `1 X* x( H+ ^3 ^" A
- 6 N5 m6 v4 w) z. G$ U# F6 H. V' J2 |) ?
- LinearLayout;
8 Z( |) W9 Q- O6 S - id="cors";
! u# u. q# C" _8 ~$ T* @ - orientation="vertical";, u# X% G1 r/ }( T4 m6 E. t( b/ i" q' I
- layout_width="70%w";* Y$ q0 }0 F9 y, Y( Q' y
- gravity="center";6 k2 H8 u8 C" q! c! `2 Z. l; i# X
- layout_height="70%w";
& A1 R" |4 ~& U9 \ - {$ Y ~' _8 e' Z# }3 k
- LinearLayout;
7 m7 v" Z- E( U2 C - layout_gravity="center";) @9 ~3 A w. J! q
- orientation="vertical";
6 d, _% ^: R X - id="cor";% e$ U" k H0 y4 o+ [6 A
- layout_width="50%w";( p; F! k2 \, O; L! z7 v
- gravity="start";
% b4 w. a4 C, T. N8 Q2 X - layout_height="50%w";( @( y: W0 Y3 A$ {% B4 k) [
- {5 y* t4 q; V5 r h" M! T
- TextView;
0 s! O: ~, z) i& n# \" Z$ H5 Y - layout_gravity="center";
# W( l1 ~8 o1 {& g: [/ h7 I6 y - textColor="#0088ff";1 {% N5 f. A7 u* S4 {0 p+ v) ~
- id="n";6 V) S/ q; l6 B
- textSize="20dp";9 h( H: z9 w7 M; {3 J. S: C
- gravity="center";
1 F# x9 r; k% v$ ?6 b8 ? - text="N";+ Z) j' L2 l6 @; ^3 l. a
- };
3 e w0 y3 ~7 l3 ` - {1 }( S+ U1 }4 k. h w; H6 _! i7 ^
- LinearLayout;
9 h c, e, j8 L7 i3 I. q - layout_width="fill";# g) O6 T% ?' H2 O1 W Q2 K8 M( }
- layout_marginTop="6%h";& F/ o Q: g6 U0 r& z" v
- layout_height="8%h";
( O# o5 u$ z" y! l( r# S; ` - {
6 q* k% u, ]6 e - TextView;1 w$ d& u7 m8 t% {2 U
- id="w";- d% m$ B6 r7 a+ {. ]( H
- textColor="0xff0088ff";+ |) F% k! Q7 B
- paddingLeft="6dp";
4 i3 O* [% `, j5 o - textSize="20dp";
- q$ p# v6 i; J7 A }. o - gravity="left";+ K0 t6 _ b! A! x- w4 p- H& [
- text="W";
- R5 L, \* Q/ V% ^0 `6 T& E6 w4 X - };
* Q" w- P* ]8 Q) V+ E! q - {7 G& S( W1 K7 e) B( n0 I
- TextView;
* y/ k: Y. \# x/ U - layout_width="16.5%w";
3 K2 R7 Z# u6 l: N' @: M - layout_marginLeft="9%w";; p5 r! K" d0 ~
- textColor="0xff0088ff";
9 z% q1 r; o$ {% R: |- V - layout_height="8%h";' T" N, U' s9 k! u
- textSize="27dp";! D7 O- U: s& X9 A. J5 {( O
- gravity="center";4 o& D, I# X: d3 z- Q1 y
- text="";
! t" h4 M% M5 e6 l8 A3 V - };
$ a0 n3 A3 O8 `' T* w - {
" g/ C/ K6 p: c* a2 W7 x - TextView;
% V* Z: |+ Z! `' n6 v5 k0 t$ f - layout_marginRight="6dp";
3 P: ?3 h" D3 c9 r - id="e";% i+ ^0 Q2 i- c I/ p6 c# d
- layout_marginLeft="11%w";2 o# A! W# Q/ h" [+ Y" `
- textColor="0xff0088ff";; w) l. I/ ]. w* @ q, Q. E& C
- textSize="20dp";
4 n: y, M* b) @' D( d3 N - layout_width="6%w";
% B* {5 u/ o# q" T3 }3 F - gravity="right";8 w; ^4 P* |, l
- text="E";
$ Y. \6 k& k1 r/ I* c% l - };
2 |0 n. f x+ d" d1 a3 ?- z - };( L. @; Z& v# r: U6 u- J6 E |
- {
+ V% Y4 i1 y7 s8 z - TextView;. e1 k9 [. H1 A4 @3 Z$ O' K4 S. \$ h
- layout_gravity="center";
2 i% Y+ i/ g0 `. j2 [ - layout_marginTop="11%h";1 I) H- x) J" c9 U9 X
- textColor="#0088ff";
' o( p8 ~2 s3 T4 n* \+ K - id="s";
! N$ C: c1 O5 Q8 M8 b; P) k9 I - textSize="20dp";
& B+ h! Z) h9 v. ^ - gravity="center";
3 ^' U: c& z0 m4 ~9 J; ~/ ? - text="S";
m. P+ g8 m% E* n7 q; T. W
' b5 i) M/ |& s0 e- };- u8 n$ F7 h9 H# {% q+ K
- };( S. D, n$ `+ h- M7 r
- + L6 h7 O9 w% w# S0 Z' W
- };& |' ~' Z3 ?9 t. C7 N" n: k4 l7 _9 C
- {, ^5 C- O# f$ h% G% _* v" b
- TextView;
: ~( W, ?4 L1 k0 a - id="deg";1 q! ]% y7 l) z1 K& v* h* B! S8 R$ q
- textColor="0xff0088ff";
. y5 Y6 V. S5 X% A - layout_height="8%h";' l( a7 |4 D4 A
- textSize="27dp";
& K$ P, V6 }9 ?$ i( X {9 ~ - gravity="center";2 _ J" x& q* W J
- text="0°";: A' T# {. w* v
- };
% W) }- m! ]) j5 u( y - };* r8 Y' o) V3 t/ Z3 K9 Q
- : V* ?: I( l1 N4 o
- webView.addView(loadlayout(指南针))$ r; S4 Q+ m, L G; g- Q
: `! `2 `: P7 e- import "android.content.Context". h, n* J2 E% d9 a7 }( e3 s& {
- import "android.hardware.SensorManager"( |1 Q: y z& v1 H% R, l
- import "android.hardware.SensorEventListener"8 a1 l( r+ s. `( C' Y& c
- import "android.hardware.Sensor"
4 Z" ?, Q+ p/ P* U, ]7 t - dirs=""
; N: B, n2 Q( `: l' D - FPS=60+ U# y6 C( O0 ]; F* D! q7 f# T
- 磁场=0
8 g& L* y% B7 C - 传感器 = activity.getSystemService(Context.SENSOR_SERVICE)
) f2 B1 i% p7 L1 j O! \0 P, Y - local 磁场传感器 = 传感器.getDefaultSensor(Sensor.TYPE_ORIENTATION)
1 c. ?% e9 _! S7 u6 e2 \ - 传感器.registerListener(SensorEventListener({ , }! P$ X1 \4 t6 H' ^# {
- onSensorChanged=function(event)
9 r, G1 @9 A( u+ v# K - 磁场 = event.values[0]% t& ?1 R7 h: i. ]. z# r3 P3 \
- end,nil}), 磁场传感器, SensorManager.SENSOR_DELAY_NORMAL)
1 _6 l( z% `9 H1 _ - function CircleButton(view,InsideColor,radiu,da,db)
8 s$ X3 {8 p7 c8 v - import "android.graphics.drawable.GradientDrawable"
* {# c- y% r7 [5 a: _0 L$ H/ o - drawable = GradientDrawable()
# n0 C/ _; G+ _) y9 N% [ - drawable.setShape(GradientDrawable.RECTANGLE)
" z6 W& J5 d6 n$ @ - drawable.setStroke(10, 0xff0088ff,da,db)4 L) R; n* P0 b; F, S' v
- drawable.setColor(InsideColor)
+ H8 N" }: s5 c% A - drawable.setCornerRadii({radiu,radiu,radiu,radiu,radiu,radiu,radiu,radiu});+ [8 i+ x! h2 c% N. X
- drawable.setGradientType(1)
8 @$ Y$ R- [% S P - view.setBackgroundDrawable(drawable)
5 t9 s+ ~+ S' [3 l - end. m- l/ b/ `8 O, \$ Z* [1 L6 B: V
- 角度=activity.getWidth()
' @2 f( w& D! S) r+ m5 Q) { - 控件id=cor8 A. k5 Q& Z, g8 _: ~ q6 P" ]/ \
- 控件颜色=0xFFFFFFFF t, M. l0 ~; L9 }# |# g
- CircleButton(控件id,控件颜色,角度,2,2)
" W3 h* I. _$ z% u8 | - CircleButton(cors,控件颜色,角度,(activity.getWidth()*0.8)/2,10)
- p) B9 ~, m- u/ k. j, Q+ n - tick=Ticker()3 L2 j- O1 j6 \, s+ q
- tick.Period=1000/FPS8 d) x' a7 K, E, B
- tick.onTick=function()
?2 I, X7 D5 p* A# y. C% s - if 磁场>165 and 磁场<195 then
# K# h8 b: R( `! c. ^3 T% J - dirs="南"
$ z, V9 I* W, J# `' m. [ - elseif 磁场>195 and 磁场<255 then; J( b' _4 T2 z+ }. i
- dirs="西南"
1 U2 }1 f* I6 w7 h0 S - elseif 磁场>255 and 磁场<285 then
$ o0 k2 h" ]& P0 }+ Z# _ - dirs="西"9 P) g$ Q. \! t0 H: u8 w+ g( ~! @
- elseif 磁场>285 and 磁场<345 then
- \" G" w" C/ U9 ?) [ - dirs="西北"
: F& R6 Z; i# v& l - elseif 磁场>345 or 磁场<15 then
3 t2 O% M5 Z4 x) V1 ` - dirs="北"
* S& K6 u E0 Y - elseif 磁场>15 and 磁场<75 then/ ^8 C/ @3 O2 P9 L3 C' e' S
- dirs="东北"2 d( `( X' V5 J3 A/ B" @8 J- s1 o
- elseif 磁场>75 and 磁场<105 then
- y: {' j; N- U0 \7 `6 I0 Q - dirs="东"8 a& R. h5 z f/ A- }. T
- elseif 磁场>105 and 磁场<165 then
# G* G3 {. O% [ - dirs="东南"" X% @% X7 l, s1 N1 V
- end! k) u8 a( l: J" O- K
- dir.setText(dirs)
% ~ F, T- G6 { - cors.setRotation(360-磁场)
- ?; T0 K6 j" {. z' @ - w.setRotation(磁场)
1 p1 k$ z% j; e4 C- i5 a - s.setRotation(磁场)
0 o! w4 P3 K+ Q" S- w - n.setRotation(磁场)2 p! L5 P) ?% ?+ `' Y
- e.setRotation(磁场)
' c7 [1 o$ |5 F7 j" b- ? - deg.setText(tostring(tointeger(磁场).."°"))8 }/ o6 z! k2 g# D5 w3 w
- --Toast.makeText(activity,tostring(磁场.."°"),Toast.LENGTH_SHORT).show()
9 i. K b) S, d2 l: q* A - end- v& `3 m8 u. k" k
- tick.start()& y1 y P! V: W' T4 u- X
' o8 { K2 y ]- function onDestroy()8 G3 }! a( x3 w+ z4 b4 M7 O
- tick.stop()
% C* t) y3 ~. z4 S& T - 传感器.unregisterListener(Listener); v( |6 a! U7 P0 A
- end
, E' K( |! z$ b - 8 a( y# u& f/ q: o# t8 A8 i9 p
- function onTouchEvent(event)$ C& H% n1 E/ S( j+ W
- ac=event.action
: F: p5 l2 L" Q W0 O% U! _) o - if ac==2 or ac==0 then
) C, x. T' B" C/ p* H; t - x=event.X3 M) N8 F3 o, O3 ~% P) g6 V
- y=event.Y
; d" h- s$ R: q# a - coe.setRotationX((activity.getHeight()-y)/30)
8 n& k: q# C; J - coe.setRotationY(x/30)
4 p& }+ s( y9 r, {% v* v - elseif ac==1 then
- D( {! Y e; b7 G/ j3 x - coe.setRotationX(0)
N- Y, f! K; e' @& ] - coe.setRotationY(0)
- ]9 Z2 j. L- z5 ?* T5 t) S - end" l" [0 R. r9 r% s7 C
- end, k; w' ?& B7 x0 ^0 T% X/ h
- --coe.setRotationX(activity.getHeight()*0.33)
0 H" u* C1 S- j - dir.getPaint().setFakeBoldText(true)) O1 \/ I" [4 u- s4 H; P. r
- deg.getPaint().setFakeBoldText(true)
# D9 a; {, f2 L -
6 m1 O3 s! N# u/ D1 @ - import "android.content.Context"
/ P+ J9 Y) X6 l. [5 ~9 H$ I- M - import "android.hardware.SensorManager"
+ q: ]7 d" U& p - import "android.hardware.SensorEventListener"* Z: O P$ E. ~+ I
- import "android.hardware.Sensor"& m3 C2 _+ j( a% D- l1 `9 c f( N( a6 k
- dirs=""
7 ^ s+ W6 p6 J* `$ t - FPS=50
/ v% @# R) p1 J. W5 u) x - 磁场=0% g9 D8 ^" G1 }% i# c, ~3 M$ d
- 传感器 = activity.getSystemService(Context.SENSOR_SERVICE)
3 V. Z0 |# d M; v6 l - local 磁场传感器 = 传感器.getDefaultSensor(Sensor.TYPE_ORIENTATION)
: ~+ i( F5 h4 d/ s. W" A7 [ - 传感器.registerListener(SensorEventListener({
2 s& y- w2 ^) ]7 ^4 o: { d. u - onSensorChanged=function(event)
6 a, ~; [5 e, p" g2 O - 磁场 = event.values[0]
3 z) n: H2 ~) H - end,nil}), 磁场传感器, SensorManager.SENSOR_DELAY_NORMAL): N+ |& A8 @( B; V) I# w! O
- function CircleButton(view,InsideColor,radiu,da,db)2 j* P& _: ]7 Y7 K4 d, x
- import "android.graphics.drawable.GradientDrawable"
5 K3 @& ~4 ]. n. m% a C- E - drawable = GradientDrawable()
) p. W! s3 ^. e - drawable.setShape(GradientDrawable.RECTANGLE)
/ h A& c( L7 P4 Y+ k8 Q6 f j& l7 g - drawable.setStroke(10, 0xff0088ff,da,db)
~3 q; U+ a# S3 t! _$ ^3 i - drawable.setColor(InsideColor) 3 l5 ^, t# W. I8 E# } p, U# ^
- drawable.setCornerRadii({radiu,radiu,radiu,radiu,radiu,radiu,radiu,radiu});
) g, y+ f& x" J5 d6 @8 k( }1 K - drawable.setGradientType(1)+ y) K$ H2 K' _# S3 `) f
- view.setBackgroundDrawable(drawable). J' d! | E/ C' c1 v _
- end: \- X" r: i C$ r! m
- 角度=activity.getWidth()
2 I: u1 k. Y8 E+ y! f: S/ j2 N - 控件id=cor& e. o6 q; n. x7 f# s
- 控件颜色=0xFFFFFFFF9 j/ F) G$ Y/ @( z1 i3 p) j8 u
- CircleButton(控件id,控件颜色,角度,2,2)8 A+ l/ u( w$ i
- CircleButton(cors,控件颜色,角度,(activity.getWidth()*0.8)/2,10)% I/ Y6 L: t* F0 E. Y
- tick=Ticker()
% f; S- G- o/ l7 S - tick.Period=1000/FPS; e, l. b+ ]+ q x C8 O
- tick.onTick=function()& i$ s& @# y. s( c, J1 Q+ x
- if 磁场>165 and 磁场<195 then
2 L- y' J* L' r, ] - dirs="南": O. _7 Z; f c# r" n) v
- elseif 磁场>195 and 磁场<255 then
: G$ W: W. ~# [6 b3 }& c - dirs="西南"
; U3 c% x3 M& A) C& C" e S: @! q G5 } - elseif 磁场>255 and 磁场<285 then7 x$ T" d6 ?) E+ s8 H# I' a7 m0 `: g
- dirs="西"
3 w2 A( N+ i6 M/ A4 T& T - elseif 磁场>285 and 磁场<345 then4 e# r/ q2 V, `' P
- dirs="西北"$ x {+ A! ]! C
- elseif 磁场>345 or 磁场<15 then8 c- T8 C* ^8 X# k1 H* a; x" l
- dirs="北"
; f4 L. R7 H/ L8 b& L; a/ f! @ - elseif 磁场>15 and 磁场<75 then2 Q$ u. c/ U" s3 W m
- dirs="东北"
2 Y/ b# D, {# [1 M! h8 d - elseif 磁场>75 and 磁场<105 then
- Z1 r9 X" W, I$ \/ A$ d$ z! ` - dirs="东"
1 c& _# |$ F+ _" q4 ?7 ` - elseif 磁场>105 and 磁场<165 then/ Q" g+ [/ l6 V" o9 Z
- dirs="东南"" M, H& v- w' E! @4 B! u
- end
0 T h8 }3 v9 C$ L" o, n& { - dir.setText(dirs)
! @9 I4 O+ j* g' E - cors.setRotation(360-磁场) w( j F* y& a! k/ N
- w.setRotation(磁场): I9 N7 j% w' d# P8 m' g1 |
- s.setRotation(磁场)
$ W" M& s9 y8 a, W - n.setRotation(磁场)
( Z1 \% M& {* n5 Q' x: d+ T - e.setRotation(磁场)
+ g, U8 ^0 T' [" Z& D$ R - deg.setText(tostring(tointeger(磁场).."°"))+ y& ~' q' c" ]! a: I5 r
- --Toast.makeText(activity,tostring(磁场.."°"),Toast.LENGTH_SHORT).show()
/ Z2 G' r& A. F* M9 s+ u" m) ~$ M9 p - end9 V8 C7 u! D: B9 @
- tick.start()2 L' b* Y% v1 M( m! r! J! n7 o, X3 h
1 g( J7 O5 N5 Y" N4 C- function onDestroy()
8 o$ h+ i8 S5 V' I5 {. R - tick.stop()
' S! M0 p9 B; N* `9 }2 C0 F' d - 传感器.unregisterListener(Listener);
8 I3 J! M" l3 _* F& D( Q - end
6 m, S3 w' p4 v+ `% L4 Q - ( e1 U- ?/ V. i* I' h6 ?
- function onTouchEvent(event)- ~; Z# o1 H/ v D. _
- ac=event.action
& G2 S& {) K; U - if ac==2 or ac==0 then% z' r: L& \: F0 f$ r
- x=event.X) F2 F& G, s6 M t3 F0 l
- y=event.Y
/ q& q8 o, J# D/ ]7 D& m - coe.setRotationX((activity.getHeight()-y)/30)
: K+ M" S/ e+ l% f+ Y5 V( ~) L - coe.setRotationY(x/30)5 ?1 m& Z& q( c* I% [
- elseif ac==1 then
6 I, Z V: Q; u. K$ l - coe.setRotationX(0): Q$ ^3 ?$ u0 X6 s
- coe.setRotationY(0)0 p& m/ T4 E8 k1 P, C% R" f$ L2 ^. N8 |
- end
6 u1 @% Q8 C0 o: x A0 W" ?) h - end
( u; x+ {0 E8 ?9 `. f - --coe.setRotationX(activity.getHeight()*0.33)
: W$ m; Y6 V+ v% ^* O1 G1 { - dir.getPaint().setFakeBoldText(true)
" k1 X! ?/ S' p5 g% x# w: r- a, _ - deg.getPaint().setFakeBoldText(true)+ O2 |/ S' d0 D8 ]6 Q2 I2 v
- 物理效果(coe,4)
复制代码 |
|