Java 在Android中更改ImageView位置时出现问题
我似乎无法更改android应用程序中Java 在Android中更改ImageView位置时出现问题,java,android,android-layout,imageview,position,Java,Android,Android Layout,Imageview,Position,我似乎无法更改android应用程序中ImageView的位置。当我将代码放在一个特定的方法中时,就会发生这种情况,如下所述 我正在android项目中使用这个库。我下载了示例应用程序并在手机上运行。效果很好 此库有一个onServiceUpdate方法,用于处理位置更新。我的目标是在位置更新时移动ImageView。很简单 下面是示例提供的原始方法(对我来说很好): 下面是我用来更新我的ImageView位置的代码: ImageView imgPoint = (ImageView) findV
ImageView
的位置。当我将代码放在一个特定的方法中时,就会发生这种情况,如下所述
我正在android项目中使用这个库。我下载了示例应用程序并在手机上运行。效果很好
此库有一个onServiceUpdate
方法,用于处理位置更新。我的目标是在位置更新时移动ImageView
。很简单
下面是示例提供的原始方法(对我来说很好):
下面是我用来更新我的ImageView
位置的代码:
ImageView imgPoint = (ImageView) findViewById(R.id.imagePoint);
imgPoint.setX(250);
imgPoint.setY(200);
如果我将这些行添加到上面的onServiceUpdate
方法中,则该方法不起作用。onServiceUpdate
方法中没有任何内容运行
但是,如果我将这3行放在onCreate
或任何其他方法中,效果会很好。ImageView
能够成功移动
我还注意到,如果我在onServiceUpdate
中添加Toast
或Alert
,同样的情况也会发生。onServiceUpdate
方法根本不会启动
Toast.makeText(getApplicationContext(), "Hello!",
Toast.LENGTH_LONG).show();
这是我的活动\u 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>
是否在非UI线程上调用了onServiceUpdate方法,但try-catch忽略了错误?然后试试把手
private Handler mUpdateHandler = new Handler() {
public void handleMessage(Message msg){
if (msg.obj instanceof ServiceState) {
ServiceState state = (ServiceState) msg.obj;
ImageView imgPoint = (ImageView) findViewById(R.id.imagePoint);
imgPoint.setX(state.getMetricPoint().getX());
imgPoint.setY(state.getMetricPoint().getY());
}
}
}
public void onServiceUpdate(ServiceState state) {
mUpdateHandler.obtainMessage(0, state).sendToTarget();
//....
}
美味有点道理。
我下载了SDK并查看了示例应用程序。
请注意您在代码段中引用的log()函数,它的impl。在示例应用程序中,如下所示:
private void log(final String msg) {
Log.d(TAG, msg);
runOnUiThread(new Runnable() {
@Override
public void run() {
mLogAdapter.add(msg);
mLogAdapter.notifyDataSetChanged();
}
});
}
这意味着onServiceUpdate没有在UI线程上运行,这意味着如果它崩溃,您可能无法获取任何日志或访问断点
我建议在log方法中添加3行代码
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);
}
});
}
并查看它是否运行,是否只是将图像视图与X和Y参数一起作为私有成员;在onServiceUpdate上更新X和Y,并在UI线程的日志中将其值设置为ImageView
(如果显然有效,则必须在之后进行一些重构,但这将给您一个良好且快速的测试)。来自onServiceUpdate(…)
是后台线程中来自IndoorAtlas
的回调方法,因此如果您想与主UI线程通信,则需要RunnuithRead(…)
:
希望这有帮助 我认为这是因为你把它放在一个相对的布局中,而这个布局在另一个布局中。您可以更改图像的位置,但布局会将其放回其边框内。此外,您还没有在相对论中定义imageview应该位于的任何属性。左/右/上/下是什么组件。@wiseindy已经更新了我的答案,它应该适合您。我用我的应用程序测试了一下,一切正常
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);
}
});
}
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());
setImagePoint(state.getImagePoint());
}
private void setImagePoint(final ImagePoint imgPt) {
runOnUiThread(new Runnable() {
@Override
public void run() {
ImageView imgPoint = (ImageView) findViewById(R.id.imagePoint);
imageMan.setX(imgPt.getI());
imageMan.setY(imgPt.getJ());
}
});
}