简体   繁体   English

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

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

I've two AbsoluteLayout are called Background_layout and Foreground_layout in one base XML. 我在一个基本XML中将两个AbsoluteLayout称为Background_layoutForeground_layout

I wanna move first layout ( Background_layout ) through the accelerometer sensor for direction YXZ , How can i do that? 我想通过加速计传感器向YXZ方向移动第一个布局( Background_layout ),我该怎么做?

Here you can see my XML : 在这里你可以看到我的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>

I have all the values about direction , But I don't know How can I use them in layout view. 我有关于方向的所有值,但我不知道如何在布局视图中使用它们。

 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);
    }
    }
    }

Any suggestion would be appreciated. 任何建议将不胜感激。

If you get values from the accelerometer, then you can update the postition of views by setting their layout parameters. 如果从加速度计获取值,则可以通过设置其布局参数来更新视图的位置。 To move the views to left, you could set the value as the left margin. 要将视图移动到左侧,可以将值设置为左边距。

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

This should work. 这应该工作。

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

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