固定手势ImageView上图像按钮的位置

时间:2015-03-30 11:23:15

标签: java android multithreading

我有一个项目可以将精确定位添加到地图中。对于地图,我们使用GestureImageView使其可滚动,并使用按钮ImageButton。

但是下面的问题正在发生:每当我们移动地图时,我们必须确保精确定位。

这个问题是我们从GestureImageView中的另一个线程调用ImageButton的翻译函数,并且不允许你调用该视图。

E/AndroidRuntime﹕ FATAL EXCEPTION: GestureImageViewAnimator
Process: nl.example.android, PID: 17024
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6915)
    at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1089)
    at android.view.ViewGroup.invalidateChildFast(ViewGroup.java:4639)
    at android.view.View.invalidateViewProperty(View.java:11748)
    at android.view.View.setTranslationY(View.java:11102)
    at nl.example.android.GestureImageView.setPosition(GestureImageView.java:455)
    at nl.example.android.GestureImageViewTouchListener.handleDrag(GestureImageViewTouchListener.java:449)
    at nl.example.android.GestureImageViewTouchListener$1.onMove(GestureImageViewTouchListener.java:122)
    at nl.example.android.FlingAnimation.update(FlingAnimation.java:49)
    at nl.example.android.Animator.run(Animator.java:45)

我现在尝试的内容:活动上下文的类

public class MyActivity extends Activity {
    private static Activity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = this;
    }
}

GestureImageView中的代码:

final float transX = x;
MyActivity myActivity = new MyActivity();
myActivity.runOnUiThread(new Runnable() {
    public void run() {
        setTranslationY(transX);
    }
});

MainActivity中的代码:

MyActivity myActivity = new MyActivity();
Runnable runnable = new Runnable() {
    @Override
    public void run() {
        pin1.setTranslationY(3);
        synchronized(this){
            this.notify();
        }
    }
};

synchronized(runnable) {
    myActivity.runOnUiThread(runnable);
    runnable.wait() ; // unlocks myRunable while waiting
}

1 个答案:

答案 0 :(得分:0)

在文件GestureImageView中,第455行尝试:

activity.runOnUiThread(new Runnable(){
    public void run(){
        setTranslationZ(x);
    }
});

您无法从其他人调用视图功能。你必须在main上调用它。如果要等待结束执行runOnUiThread,请使用:

    Runnable r = new Runnable(){public void run(){
        setTranslationZ(...);
        synchronized(this){
            this.notify();
        }
    }};
    synchronized( myRunnable ) {
        activity.runOnUiThread(myRunnable);
        myRunnable.wait() ; // unlocks myRunable while waiting
    }

为那些不知道如何使用runOnUiThread的人编辑:

class GestureImageView extends View{
    private long thread;
    public GestureImageView(Context c){
        super(c);
        thread=Thread.currentThread().getId();
    }
    public void setTranslationZ(final int n){
        if(thread==Thread.currentThread().getId()){ //this is UI thread
            super.setTranslationZ(n); 
        } else {
            MainActivity.instance.runOnUiThread(new Runnable(){
                    public void run() {
                        setTranslationZ(n);
                    }
                });
        }
    }
}
public class MainActivity extends Activity
{
public static Activity instance;
public MainActivity(){
    instance=this;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

}
}