简单指南针

199 阅读1分钟


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:padding="20dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/compass_direction"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="center_horizontal"
        android:layout_weight="1"
        android:text="@string/jp_car_compass_south"
        android:textColor="@android:color/white"
        android:textSize="30sp" />

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/jp_background_compass" />

        <ImageView
            android:id="@+id/jp_compass_bg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/jp_compass" />

        <TextView
            android:id="@+id/compass_offset"
            android:layout_width="45dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/sync_round_line_selector"
            android:gravity="center"
            android:text="10"
            android:textColor="@android:color/white"
            android:textSize="30sp" />

    </FrameLayout>



    <Button
        android:id="@+id/compass_calibration"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="校准"
        android:layout_weight="1"
        android:textColor="@android:color/white"
        android:textSize="30sp" />
</LinearLayout>

private TextView mCompassArea;
private TextView mCompassAngle;
private ImageView mCompassBg;
private Button mCompassCalibration;
private int mCompassStart = 0;
private int mCompassEnd = 0;

private void initView() {
    mCompassAngle = (TextView) findViewById(R.id.compass_direction);
    mCompassArea = (TextView) findViewById(R.id.compass_offset);
    mCompassCalibration = (Button) findViewById(R.id.compass_calibration);
    mCompassBg = (ImageView) findViewById(R.id.jp_compass_bg);
    mCompassAngle.setOnClickListener(this);
    mCompassCalibration.setOnClickListener(this);
} private void onCompassChanged() {
        boolean compassState = mCanbusState.b(CompassCalibration);
        mCompassEnd = mCanbusState.i(CompassAngle);
        setViewText(mCompassArea, String.valueOf(mCanbusState.i(CompassArea)));
        setViewText(mCompassAngle, String.valueOf(mCompassEnd));

        RotateAnimation compassAnim = new RotateAnimation(
                mCompassStart, mCompassEnd, Animation.RELATIVE_TO_SELF,
                0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        compassAnim.setDuration(1000);
        compassAnim.setFillAfter(true);
        mCompassBg.startAnimation(compassAnim);
        mCompassStart = mCompassEnd;
    }