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






From Project

Back to project page custom-touch-examples.

License

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
 */*from w  w w . j a va 2  s  .c  om*/
 * See the file license.txt for copying permission.
 */
package com.examples.customtouch.widget;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.TouchDelegate;
import android.widget.CheckBox;
import android.widget.FrameLayout;

public class TouchDelegateLayout extends FrameLayout {

  public TouchDelegateLayout(Context context) {
    super(context);
    init();
  }

  public TouchDelegateLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public TouchDelegateLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
  }
  
  private CheckBox mButton;
  private void init() {
    mButton = new CheckBox(getContext());
    mButton.setText("Click Anywhere On Screen");
    
    addView(mButton, new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER));
  }

  /*
   * TouchDelegate is applied to this view (parent) to delegate all touches
   * within the specified rectangle to the CheckBox (child).  Here, the rectangle
   * is the entire size of this parent view.
   * 
   * This must be done after the view has measured itself so we know how big to make the rect,
   * thus we've chosen to add the delegate in onMeasure()
   */
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    //Apply the whole area of this view as the delegate area
    Rect bounds = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
    TouchDelegate delegate = new TouchDelegate(bounds, mButton);
    setTouchDelegate(delegate);
  }
}




Java Source Code List

com.examples.customtouch.MainActivity.java
com.examples.customtouch.MoveLoggerActivity.java
com.examples.customtouch.MultitouchActivity.java
com.examples.customtouch.TouchDelegateActivity.java
com.examples.customtouch.TouchForwardActivity.java
com.examples.customtouch.TouchInterceptActivity.java
com.examples.customtouch.TouchListenerActivity.java
com.examples.customtouch.TwoDimensionGestureScrollActivity.java
com.examples.customtouch.TwoDimensionScrollActivity.java
com.examples.customtouch.widget.RotateZoomImageView.java
com.examples.customtouch.widget.TouchDelegateLayout.java
com.examples.customtouch.widget.TouchForwardLayout.java
com.examples.customtouch.widget.TwoDimensionGestureScrollView.java
com.examples.customtouch.widget.TwoDimensionScrollView.java