How to translate the canvas and still get the touch events on the correct places

三世轮回 提交于 2020-01-03 15:54:19

问题


I'm trying to create a custom AbsListView (overriding the same stuff on ListView, GridView and HeaderGridView) that will relocate all its drawing and touching events based on an external factor (other stuff that moves on the layout).

Paddings are not an option here, because then the only way to try to control the AbsListView position is using the super crappy method smoothScrollBy(int distance, int duration) and that is giving me a whole level of different issues that I'm trying to overcome by just all together drawing in a different area of the screen.

At the moment I found out that draw on a different area of the screen is as easy as:

   @Override
   protected void onDraw(Canvas canvas) {
      canvas.translate(0, 400);
      super.onDraw(canvas);
   }

but the touch events don't get translated, and all the touches are 400 pixels up. So I've tried to translate the touch as well with:

   @Override
   public boolean dispatchTouchEvent(MotionEvent ev) {
      ev.offsetLocation(0, -400);
      return super.dispatchTouchEvent(ev);
   }

it does relatively work, to the point that 1) on item click get's called and 2) the view changes color based on the theme android:state_pressed="true", but the Up or Cancel touch evens never get called and the view does not refresh the drawing back to the default color.

I'll still further analyse, but I'm not sure if complex custom adapter with buttons and gesture detector, if that would work or not.

So the question: How can I translate the drawing and still properly get touch events?

来源:https://stackoverflow.com/questions/23761135/how-to-translate-the-canvas-and-still-get-the-touch-events-on-the-correct-places

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!