没有的Andr​​oid触摸事件帆布翻译后 [英] Android No Touch Events after Translating Canvas

查看:286
本文介绍了没有的Andr​​oid触摸事件帆布翻译后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经冲刷在寻找答案的网站到已被窃听我好几天的问题。我想提出一个插件,它允许用户通过拖曳平移整个插件(仅围绕Y轴)的观察区域内。然后,我有这里面,用户还可以将一个较小的视图(如按钮,听onTouchEvent)。我们的想法是,用户会向上或向下拖动这个观点,如果他们想要去高或低,他们可以出锅了整个部件,然后继续拖动查看更高或更低。现在,我有这个两个问题。首先,拖动主视图(整个部件),我想再往前仍驻留在它曾经是翻译整个画布前区的看法后。

I have scoured the web in search of an answer to a question that has been bugging me for days. I am making a widget that allows a user to pan the entire widget by dragging (around the Y axis only) within the viewing area. I then have a smaller view (like a button, listens to onTouchEvent) inside here that the user can also drag. The idea is that the user will drag this view up or down and if they want to go higher or lower they can pan the entire widget and then continue dragging the view higher or lower. Now, I'm having two issues with this. First off, after dragging the main view (whole widget) the view that I would like to then move still resides in the area it once was before translating the entire canvas.

因此​​,举例来说,如果我50在Y位置移动整个画布,然后我要移动的小看法,但在旧的位置,而不是抵消一(50)接触。所以它看起来像我不要点击小图,其中它是现在,但它曾经是(我希望这是有道理的)。

So for example, if I move the entire canvas by 50 in the Y position, I then want to move the smaller view but have to touch in the old position rather than the offsetted one (by 50). So it looks like I don't click the smaller view where it is now, but where it used to be (I hope this makes sense).

接下来的问题,当我移动小视图。我不能够触发了触摸事件。我猜想这是因为它被移到它的父(这仍然是可见的,因为我设置clipChildren为false)和它的Z轴顺序比在布局其他意见较低之外。有没有办法,始终这个位置之上?我使用Android的API 17.就算我设置较小的观点为主要布局的最后一个子(触摸时,并触发事件ACTION_MOVE,我移动),我需要能够拖动它离开这里(我能),并继续拖累它在一个新的onTouchEvent。任何帮助是极大AP preciated。我加了下面的一些布局xml和code片段提供帮助。

Next issue, after I move the smaller view. I am not able to trigger anymore touch events. I'm assuming that's because it was moved outside of its parent (which is still viewable since I set clipChildren to false) and its "z-order" is lower than the other views in the layout. Is there a way to always position this on top? I am using Android API 17. Even if I set this smaller view (that I am moving when touched and ACTION_MOVE event is triggered) as the last child in the main layout, I need to be able to drag it out of here (which I can) and continue to drag it on a new onTouchEvent. ANY help is greatly appreciated. I added some layout xml and code snippets below to help.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipChildren="false"
            android:clipToPadding="false">

<RelativeLayout
        android:id="@+id/main_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipChildren="false"
        android:clipToPadding="false">

    <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:clipChildren="false"
            android:clipToPadding="false">

        <ImageView
                android:id="@+id/ref_image"
                android:layout_width="wrap_content"
                android:layout_height="216dp"
                android:src="@drawable/my_ref_image"
                android:layout_centerInParent="true"
                android:clipChildren="false"/>
        <RelativeLayout
                android:id="@+id/selection_view"
                style="@style/MyStyle"
                android:layout_alignTop="@id/ref_image"
                android:layout_marginTop="116dp"
                android:clipChildren="false"
                android:clipToPadding="false">
            <RelativeLayout
                    android:id="@+id/selection_area"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:clipChildren="false"
                    android:clipToPadding="false">
                <ImageView
                        android:id="@+id/underlay_image"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/translucent_image"
                        android:layout_centerInParent="true"/>
                <ImageView
                        android:id="@+id/point_area"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/point_image"
                        android:layout_centerInParent="true"/>
            </RelativeLayout>
        </RelativeLayout>
    </RelativeLayout>        
</RelativeLayout>

所以,code的片段,感兴趣:

So the snippets of code that is of interest:

@Override
protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  if (DEBUG) {
     Log.d(TAG, TAG + "::onDraw: ");
  }

  canvas.translate(0, backgroundPositionY);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
  final int action = ev.getAction();
  Log.d(TAG, TAG + "::onTouchEvent: parent touch event");
  switch (action) {
  case MotionEvent.ACTION_DOWN: {
     final float y = ev.getY();

     // Remember where we started
     lastTouchY = y;

     if (DEBUG) {
        Log.d(TAG, TAG + "::onTouchEvent: parent ACTION_DOWN");
     }
     return true;
  }

  case MotionEvent.ACTION_MOVE: {
     final float y = ev.getY();

     // Calculate the distance moved
     final float dy = y - lastTouchY;

     backgroundPositionY += dy

     // Remember this touch position for the next move event
     lastTouchY = y;

     // Invalidate to request a redraw
     invalidate();
     break;
  }
  case MotionEvent.ACTION_UP: {         
     lastTouchY = ev.getY();
  }
  }
  return false;
}
// called upon initialization (selectionArea refs view id = selection_area)
private void initPointListener() {
  this.setClipChildren(false);
  this.setClipToPadding(false);

  if (selectionArea != null) {
     selectionArea .setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

           RelativeLayout.LayoutParams pointParams = (RelativeLayout.LayoutParams) selectionArea.getLayoutParams();
           final int action = motionEvent.getAction();
           switch (action) {
           case MotionEvent.ACTION_DOWN: {
              pointLastTouchY = motionEvent.getY();
              }
              break;
           }

           case MotionEvent.ACTION_MOVE: {
              float moveY = motionEvent.getY();
              final float dy = moveY - pointLastTouchY;
              pointPosY += dy;

              selectionArea.setY(pointPosY);
              break;
           }
           }

           return true;
        }
     });
  }
}

请注意,我已经尝试设置填充和我所引用的,没有良好的效果使上如下:

Note that I have tried setting the padding and I have referenced the following on SO with no good results:

<一个href=\"http://stackoverflow.com/questions/23761135/how-to-translate-the-canvas-and-still-get-the-touch-events-on-the-correct-places?noredirect=1#comment50300356_23761135\">How翻译画布,仍然可以得到在正确的地方,触摸事件

<一个href=\"http://stackoverflow.com/questions/6851322/android-click-touch-event-not-working-after-canvas-translate\">Android :点击/帆布后,触摸事件不灵翻译

推荐答案

好吧,好吧......我发现周围的工作。基本上,我父视图(小部件本身)总能看到触摸事件。但是,孩子们也没有给我上面提到的情况。所以,我所做的就是跟踪x和子视图的y位置我拖着相对于可视面积。然后,每当我收到一个触摸事件我会检查,看看是否我摸了摸孩子的区域或没有。如果我碰巧触发onTouchEvent为孩子当它在其原来的位置,我会忽略它,如果它没有通过此项检查。它的不拘小节,但我能得到它的工作的唯一途径。现在我有一个可平移视图和拖动对象

Alright, well... I found a work around. Basically, my parent view (the widget itself) could always see touch events. But the children could not given the circumstances I mentioned above. So, what I did was keep track of the x and y positions of the child view I was dragging relative to the viewing area. Then whenever I received a touch event I would check to see if I touched the child area or not. If I happened to trigger the onTouchEvent for the child when it was in its old location, I would ignore it if it didn't pass this check. Its sloppy, but its the only way I could get it to work. Now I have a pannable view and a draggable object

这篇关于没有的Andr​​oid触摸事件帆布翻译后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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