Android Open Source - custom-touch-examples Touch Forward Layout

From Project

Back to project page custom-touch-examples.


The source code is released under:

Copyright (c) 2012 Wireless Designs, LLC Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in ...

If you think the Android project custom-touch-examples listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

 * Copyright (c) 2012 Wireless Designs, LLC
 *//  w ww.  ja v a  2s.  c o m
 * See the file license.txt for copying permission.
package com.examples.customtouch.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

public class TouchForwardLayout extends FrameLayout {

    private Point mTouchOffsetPoint = new Point();

  public TouchForwardLayout(Context context) {

  public TouchForwardLayout(Context context, AttributeSet attrs) {
    super(context, attrs);

  public TouchForwardLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

  public boolean onInterceptTouchEvent(MotionEvent ev) {
    //Don't let any touches be passed down to the children automatically
    return true;
   * In onTouchEvent(), we pass all the touches we receive directly to the
   * first child by calling its dispatchTouchEvent() method.
   * Note that, we modify the event so the initial location of the touch will
   * will report as being centered in the view we are forwarding to.  Each event after
   * this will be offset by the same amount, which creates the effect that the finger. This may have consequences depending on the reasoning for forwarding the event.
   * MotionEvent.setLocation() can be used to modify the x/y before forwarding.
  public boolean onTouchEvent(MotionEvent event) {
    //Forward all touch events to the first child
    View child = getChildAt(0);
    if(child == null) {
      return false;

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            //Update this as the offset point
            mTouchOffsetPoint.x = (int)event.getX();
            mTouchOffsetPoint.y = (int)event.getY();

        //Massage the event to be offset from the first touch
        event.offsetLocation(-mTouchOffsetPoint.x + child.getWidth() / 2,
                -mTouchOffsetPoint.y + child.getHeight() / 2);
    return child.dispatchTouchEvent(event);    

Java Source Code List