问题改变Android的一个ImageView的位置 [英] Problems changing the position of an ImageView in Android

查看:160
本文介绍了问题改变Android的一个ImageView的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法改变我的Andr​​oid应用程序的的ImageView 的位置。它发生时,我把我的code的具体方法如下所述。

我使用的是 IndoorAtlas在我的Andr​​oid项目库。我下载示例应用程序并得到它我的手机上运行。伟大的作品。

该图书馆有一个 onServiceUpdate 方法,它是用来处理位置更新。我的目的是将一个的ImageView 每当位置更新。 pretty简单。

下面是由例如提供的原始方法(对我来说工作正常):

  / * IndoorAtlasListener接口* // **
*在这里,您将处理位置更新。
* /
公共无效onServiceUpdate(ServiceState状态){
    mSharedBuilder.setLength(0);
    mSharedBuilder.append(位置)
            .append(\\ n \\ troundtrip:).append(state.getRoundtrip())追加(MS)。
            .append(\\ n \\ TLAT:).append(state.getGeoPoint()getLatitude())
            .append(\\ n \\ tlon:).append(state.getGeoPoint()getLongitude())
            .append(\\ n \\ TX [表]:).append(state.getMetricPoint()的getX())
            .append(\\ n \\ TY [表]:).append(state.getMetricPoint()的getY())
            .append(\\ n \\ TI [像素]:).append(state.getImagePoint()格提())
            .append(\\ n \\ TJ [像素]:).append(state.getImagePoint()getJ())
            .append(\\ n \\ theading:).append(state.getHeadingDegrees())
            .append(\\ n \\ tuncertainty:).append(state.getUncertainty());
    日志(mSharedBuilder.toString());
}

下面是code,我用它来更新我的的ImageView 的位置:

  ImageView的imgPoint =(ImageView的)findViewById(R.id.imagePoint);
imgPoint.setX(250);
imgPoint.setY(200);

如果我添加这些行上述 onServiceUpdate 方法,该方法是行不通的。在没有任何的 onServiceUpdate 方法运行。

不过,如果我放在的onCreate 这3线或任何其他方法,它的伟大工程。在的ImageView 是能够成功地移动。

我也注意到,如果我添加了一个吐司提示 onServiceUpdate ,同样的事情发生。在 onServiceUpdate 方法不火的。

  Toast.makeText(getApplicationContext(),你好!,
      Toast.LENGTH_LONG).show();

这是我的 activity_main.xml中

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>    < RelativeLayout的
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =300dp
        机器人:layout_weight =1>        < ImageView的
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            机器人:ID =@ + ID / ImageView的
            机器人:layout_weight =1/>        < RelativeLayout的
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            机器人:layout_weight =1>            < ImageView的
                机器人:layout_width =20dp
                机器人:layout_height =20dp
                机器人:layout_marginTop =1DP
                机器人:layout_marginLeft =1DP
                机器人:ID =@ + ID / imagePoint
                机器人:layout_weight =1/>        < / RelativeLayout的>        < / RelativeLayout的>    < ListView控件
        机器人:ID =@ + ID /列表
        机器人:layout_width =match_parent
        机器人:layout_height =0dp
        机器人:layout_weight =1/>< / LinearLayout中>


解决方案

美味的那种有一个点。
我下载了SDK,看着示例应用程序。
请注意你指的是在你的code段的implement执行日志()函数。在示例应用程序是这样的:

 私人无效日志(最后弦乐味精){
    Log.d(TAG,味精);
    runOnUiThread(新的Runnable(){
        @覆盖
        公共无效的run(){
            mLogAdapter.add(MSG);
            mLogAdapter.notifyDataSetChanged();
        }
    });
}

这意味着onServiceUpdate不是UI线程,这意味着如果它崩溃你可能无法得到任何日志或访问断点上运行。

我的建议是你的3行添加到日志方法

 私人无效日志(最后弦乐味精){
    Log.d(TAG,味精);
    runOnUiThread(新的Runnable(){
        @覆盖
        公共无效的run(){
            mLogAdapter.add(MSG);
            mLogAdapter.notifyDataSetChanged();
            ImageView的imgPoint =(ImageView的)findViewById(R.id.imagePoint);
            imgPoint.setX(250);
            imgPoint.setY(200);
        }
    });
}

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

(一些重构将不得不作出之后,如果它的工作原理很明显,但它会给你一个很好的和快速测试)。

I cannot seem to change the position of an ImageView in my android application. It happens when I place my code in a specific method as explained below.

I am using the IndoorAtlas library in my android project. I downloaded the sample application and got it running on my phone. Works great.

This library has an onServiceUpdate method which is used to handle the location updates. My aim is to move an ImageView whenever the location is updated. Pretty simple.

Here is the original method as provided by the example (works fine for me):

/* 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());
}

Below is the code which I use to update my ImageView's location:

ImageView imgPoint = (ImageView) findViewById(R.id.imagePoint);
imgPoint.setX(250);
imgPoint.setY(200);

If I add these lines to the onServiceUpdate method above, the method doesn't work. Nothing in the onServiceUpdate method runs.

However, if I place these 3 lines in onCreate or any other method, it works great. The ImageView is able to move successfully.

I have also noticed that if I add a Toast or Alert in onServiceUpdate, the same thing happens. The onServiceUpdate method doesn't fire at all.

Toast.makeText(getApplicationContext(), "Hello!",
      Toast.LENGTH_LONG).show();

This is my 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>

解决方案

yummy kind of has a point. I downloaded the SDK and looked at the example application. Please pay attention to the log() function you're referring to in your code snippet whose impl. in the example app is this:

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

This means that the onServiceUpdate is not running on the UI thread which means that if it crashes you might not get any log or access to breakpoints.

What i suggest is to add your 3 lines to the log method

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

And see if it runs, if it does simply make the image view a private member along with the X and Y parameters; update X and Y on the onServiceUpdate and set their value to the ImageView in log on the UI thread.

(some refactoring will have to be made afterwards if it works obviously but it will give you a good and quick test).

这篇关于问题改变Android的一个ImageView的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆