在Android中更改ImageView位置时出现问题

wis*_*ndy 4 java android position imageview android-layout

我似乎无法改变ImageView我的Android应用程序中的位置.当我将代码放在特定方法中时会发生这种情况,如下所述.

我在我的android项目中使用IndoorAtlas库.我下载了示例应用程序并在手机上运行它.效果很好.

该库有一个onServiceUpdate用于处理位置更新的方法.我的目标是ImageView每当位置更新时移动.很简单.

这是示例提供的原始方法(对我来说很好):

/* IndoorAtlasListener interface */

/**
* This is where you will handle location updates.
*/
public void onServiceUpdate(ServiceState state) {
    mSharedBuilder.setLength(0);
    mSharedBuilder.append("Location: ")
            .append("\n\troundtrip : ").append(state.getRoundtrip()).append("ms")
            .append("\n\tlat : ").append(state.getGeoPoint().getLatitude())
            .append("\n\tlon : ").append(state.getGeoPoint().getLongitude())
            .append("\n\tX [meter] : ").append(state.getMetricPoint().getX())
            .append("\n\tY [meter] : ").append(state.getMetricPoint().getY())
            .append("\n\tI [pixel] : ").append(state.getImagePoint().getI())
            .append("\n\tJ [pixel] : ").append(state.getImagePoint().getJ())
            .append("\n\theading : ").append(state.getHeadingDegrees())
            .append("\n\tuncertainty: ").append(state.getUncertainty());
    log(mSharedBuilder.toString());
}
Run Code Online (Sandbox Code Playgroud)

以下是我用来更新我ImageView位置的代码:

ImageView imgPoint = (ImageView) findViewById(R.id.imagePoint);
imgPoint.setX(250);
imgPoint.setY(200);
Run Code Online (Sandbox Code Playgroud)

如果我将这些行添加到onServiceUpdate上面的方法,该方法不起作用.onServiceUpdate方法中没有任何内容可以运行.

但是,如果我将这三行放入onCreate或任何其他方法,它的效果很好.在ImageView能够成功转会.

我也注意到,如果我添加一个ToastAlertonServiceUpdate,同样的事情发生.该onServiceUpdate方法根本不会发射.

Toast.makeText(getApplicationContext(), "Hello!",
      Toast.LENGTH_LONG).show();
Run Code Online (Sandbox Code Playgroud)

这是我的 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="300dp"
        android:layout_weight="1">

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/imageView"
            android:layout_weight="1" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1">

            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_marginTop="1dp"
                android:layout_marginLeft="1dp"
                android:id="@+id/imagePoint"
                android:layout_weight="1" />

        </RelativeLayout>

        </RelativeLayout>

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Roy*_*sef 6

好吃的有点意思.我下载了SDK并查看了示例应用程序.请注意你在你的代码片段中提到的log()函数.在示例应用程序中是这样的:

    private void log(final String msg) {
    Log.d(TAG, msg);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mLogAdapter.add(msg);
            mLogAdapter.notifyDataSetChanged();
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

这意味着onServiceUpdate未在UI线程上运行,这意味着如果它崩溃,您可能无法获得任何日志或访问断点.

我建议将3行添加到log方法中

        private void log(final String msg) {
    Log.d(TAG, msg);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mLogAdapter.add(msg);
            mLogAdapter.notifyDataSetChanged();
            ImageView imgPoint = (ImageView)findViewById(R.id.imagePoint);
            imgPoint.setX(250);
            imgPoint.setY(200);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

并查看它是否运行,如果它只是使图像视图与X和Y参数一起成为私有成员; 更新onServiceUpdate上的X和Y,并将其值设置为UI线程上的日志中的ImageView.

(如果它显然有效,那么必须在之后进行一些重构,但它会为你提供一个好的和快速的测试).