开发者

How can i move my layout via accelerometer sensor?

I've two AbsoluteLayout are called Background_layout and Foreground_layout in one base XML.

I wanna move first layout (Background_layout) through the accelerometer sensor for direction Y X Z , How can i do that?

Here you can see my XML :

<开发者_如何学Go?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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜