繁体   English   中英

如何通过加速计传感器移动布局?

[英]How can i move my layout via accelerometer sensor?

我在一个基本XML中将两个AbsoluteLayout称为Background_layoutForeground_layout

我想通过加速计传感器向YXZ方向移动第一个布局( Background_layout ),我该怎么做?

在这里你可以看到我的XML:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background_layout" 
android:background="@drawable/background" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
>
<AbsoluteLayout 
android:id="@+id/foreground_layout" 
android:layout_height="wrap_content" 
android:background="@drawable/foreground" 
android:layout_width="wrap_content" 
android:layout_x="0dip" 
android:layout_y="0dip">
</AbsoluteLayout>
</AbsoluteLayout>

我有关于方向的所有值,但我不知道如何在布局视图中使用它们。

 public void onSensorChanged(int sensor, float[] values) {
    synchronized (this) {
    if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
    float ValuX = (float) values[0];
    float ValuY = (float) values[1];
    float ValuZ = (float) values[2];
    int ResValuX  = new Integer(Float.toString(ValuX));
    int ResValuY  = new Integer(Float.toString(ValuY));
    int ResValuZ  = new Integer(Float.toString(ValuZ));
    Background.addView(Background,ResValuX);
    Background.addView(Background,ResValuY);
    Background.addView(Background,ResValuZ);
    }
    }
    }

任何建议将不胜感激。

如果从加速度计获取值,则可以通过设置其布局参数来更新视图的位置。 要将视图移动到左侧,可以将值设置为左边距。

LinearLayout.LayoutParams lp = LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT
        ,LayoutParams.WRAP_CONTENT);
lp.leftMargin = 10;
view.setLayoutParameters(lp);

这应该工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM