[Android] zero base to soaring | TouchListener PK OnTouchEvent + multi touch

Posted by yakoup46 on Mon, 17 Jan 2022 18:25:44 +0100

3.4 TouchListener PK OnTouchEvent + multi touch

classification Android basics tutorial

Introduction to this section:

For example, this section brings you the comparison between TouchListener and OnTouchEvent, as well as the knowledge points of multi touch! TouchListener is based on listening, while OnTouchEvent is based on callback! Here are two simple examples to deepen your understanding!

1. TouchListener based on monitoring

Code example:

Implementation effect diagram:

Implementation code: main xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    android:paddingBottom="@dimen/activity_vertical_margin"  
    tools:context=".MyActivity">  
</span><span class="tag">&lt;ImageView</span><span class="pln">  
    </span><span class="atn">android:layout_width</span><span class="pun">=</span><span class="atv">"wrap_content"</span><span class="pln">  
    </span><span class="atn">android:layout_height</span><span class="pun">=</span><span class="atv">"wrap_content"</span><span class="pln">  
    </span><span class="atn">android:id</span><span class="pun">=</span><span class="atv">"@+id/imgtouch"</span><span class="pln">  
    </span><span class="atn">android:background</span><span class="pun">=</span><span class="atv">"@drawable/touch"</span><span class="tag">/&gt;</span><span class="pln">  

</RelativeLayout>

MainAcitivity.java

public class MyActivity extends ActionBarActivity {  
</span><span class="kwd">private</span><span class="pln"> </span><span class="typ">ImageView</span><span class="pln"> imgtouch</span><span class="pun">;</span><span class="pln">  

</span><span class="lit">@Override</span><span class="pln">  
</span><span class="kwd">protected</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> onCreate</span><span class="pun">(</span><span class="typ">Bundle</span><span class="pln"> savedInstanceState</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">  
    </span><span class="kwd">super</span><span class="pun">.</span><span class="pln">onCreate</span><span class="pun">(</span><span class="pln">savedInstanceState</span><span class="pun">);</span><span class="pln">  
    setContentView</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">layout</span><span class="pun">.</span><span class="pln">activity_my</span><span class="pun">);</span><span class="pln">  

    imgtouch </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="typ">ImageView</span><span class="pun">)</span><span class="pln">findViewById</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">id</span><span class="pun">.</span><span class="pln">imgtouch</span><span class="pun">);</span><span class="pln">  
    imgtouch</span><span class="pun">.</span><span class="pln">setOnTouchListener</span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> </span><span class="typ">View</span><span class="pun">.</span><span class="typ">OnTouchListener</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">  
        </span><span class="lit">@Override</span><span class="pln">  
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">boolean</span><span class="pln"> onTouch</span><span class="pun">(</span><span class="typ">View</span><span class="pln"> v</span><span class="pun">,</span><span class="pln"> </span><span class="typ">MotionEvent</span><span class="pln"> </span><span class="kwd">event</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">  
            </span><span class="typ">Toast</span><span class="pun">.</span><span class="pln">makeText</span><span class="pun">(</span><span class="pln">getApplicationContext</span><span class="pun">(),</span><span class="str">"You pass listener mode:OnTouchListener Touched Lun's house~"</span><span class="pun">,</span><span class="typ">Toast</span><span class="pun">.</span><span class="pln">LENGTH_LONG</span><span class="pun">).</span><span class="pln">show</span><span class="pun">();</span><span class="pln">  
            </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span><span class="pln">  
        </span><span class="pun">}</span><span class="pln">  
    </span><span class="pun">});</span><span class="pln">  
</span><span class="pun">}</span><span class="pln">  

}

Code parsing:

Simply set an ImageView, setOnTouchListener, and override the onTouch method! It's very simple. In fact, there is an example in the section of frame layout. Remember the cute girl who moves with her fingers?

OnTouchListener related methods and properties:

onTouch(View v, MotionEvent event): the parameters in this sequence are the components that trigger the touch event. The touch event encapsulates the details of the trigger event, including the event type, trigger time and other information. For example, event getX(),event. getY()
We can also judge the action type of touch, using event Getaction() to judge; For example:
event.getAction == MotionEvent.ACTION_DOWN: press event
event.getAction == MotionEvent.ACTION_MOVE: move event
event.getAction == MotionEvent.ACTION_UP: pop up event

2. onTouchEvent() method based on callback

It is also a touch event, but onTouchEvent is more used for customized views. This method is rewritten in all view classes, and this touch event is based on callback, that is, if the value returned is false, the event will continue to propagate outward and be processed by the external container or Activity! Of course, it also involves gesture, which we will explain in detail later! onTouchEvent is actually similar to onTouchListener, but the processing mechanism is not used. The former is callback and the latter is listening mode!

Code example: define a simple view and draw a small blue circle that can follow your fingers

Implementation code: MyView java

public class MyView extends View{  
    public float X = 50;  
    public float Y = 50;  
</span><span class="com">//Create brush < / span > < span class = "PLN" >
</span><span class="typ">Paint</span><span class="pln"> paint </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Paint</span><span class="pun">();</span><span class="pln">  

</span><span class="kwd">public</span><span class="pln"> </span><span class="typ">MyView</span><span class="pun">(</span><span class="typ">Context</span><span class="pln"> context</span><span class="pun">,</span><span class="typ">AttributeSet</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">)</span><span class="pln">  
</span><span class="pun">{</span><span class="pln">  
    </span><span class="kwd">super</span><span class="pun">(</span><span class="pln">context</span><span class="pun">,</span><span class="kwd">set</span><span class="pun">);</span><span class="pln">  
</span><span class="pun">}</span><span class="pln">  

</span><span class="lit">@Override</span><span class="pln">  
</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> onDraw</span><span class="pun">(</span><span class="typ">Canvas</span><span class="pln"> canvas</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">  
    </span><span class="kwd">super</span><span class="pun">.</span><span class="pln">onDraw</span><span class="pun">(</span><span class="pln">canvas</span><span class="pun">);</span><span class="pln">  
    paint</span><span class="pun">.</span><span class="pln">setColor</span><span class="pun">(</span><span class="typ">Color</span><span class="pun">.</span><span class="pln">BLUE</span><span class="pun">);</span><span class="pln">  
    canvas</span><span class="pun">.</span><span class="pln">drawCircle</span><span class="pun">(</span><span class="pln">X</span><span class="pun">,</span><span class="pln">Y</span><span class="pun">,</span><span class="lit">30</span><span class="pun">,</span><span class="pln">paint</span><span class="pun">);</span><span class="pln">  
</span><span class="pun">}</span><span class="pln">  

</span><span class="lit">@Override</span><span class="pln">  
</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">boolean</span><span class="pln"> onTouchEvent</span><span class="pun">(</span><span class="typ">MotionEvent</span><span class="pln"> </span><span class="kwd">event</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">  
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">X </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">event</span><span class="pun">.</span><span class="pln">getX</span><span class="pun">();</span><span class="pln">  
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">Y </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">event</span><span class="pun">.</span><span class="pln">getY</span><span class="pun">();</span><span class="pln">  
    </span><span class="com">//Notify the component to redraw < / span > < span class = "PLN" >
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">invalidate</span><span class="pun">();</span><span class="pln">  
    </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span><span class="pln">  
</span><span class="pun">}</span><span class="pln">  

}

main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context=".MyActivity">  
    <example.jay.com.touch2.MyView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content" />  
</RelativeLayout> 

Implementation effect diagram:

Touch with your fingers to move~

3. Multi touch

Principles:

The so-called multi touch means that multiple fingers operate on the screen. It is estimated that the most used function is the zoom function. For example, many image browsers support zoom! In theory, the Android system itself can handle the touch of up to 256 fingers, which of course depends on the support of mobile phone hardware; However, mobile phones that support multi touch generally support 2-4 points, of course, some more! We found that motionevent is useful for the first two points. Motionevent represents a touch event; Before, we can according to event getAction() & MotionEvent. ACTION_ In addition to the three single point operations described above, there are two multi-point special operations:

  • MotionEvent.ACTION_POINTER_DOWN: triggered when a point on the screen is pressed and other points are pressed.
  • MotionEvent.ACTION_POINTER_UP: triggered when multiple points on the screen are pressed and released (that is, when the last point is not released).

The simple process is as follows:

  • When we touch the screen with one finger - > trigger ACTION_DOWN event
  • Then another finger also touches the screen - > trigger action_ POINTER_ The down event continues to be triggered if there are other finger touches
  • One finger leaves the screen - > trigger action_ POINTER_ The up event continues to be triggered when a finger leaves
  • When the last finger leaves the screen - > trigger ACTION_UP event
  • And throughout the process, action_ The move event is constantly triggered

We can use event GeTx (int) or event Gety (int) to get the location of different touch points: such as event GeTx (0) can obtain the X coordinate of the first contact point, event GeTx (1) get the X coordinate of the second contact point, so In addition, we can also determine how many fingers are currently touching by calling the getPointerCount() method of the MotionEvent object~

Code example:

Well, let's write an example of the most common one finger drag picture and two finger zoom picture:

Implementation effect diagram:

Implementation code:

<?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" >

    <ImageView
        android:id="@+id/img_test"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="matrix"
        android:src="@drawable/pic1" />

</RelativeLayout>

MainActivity.java

package com.jay.example.edittextdemo;

import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnTouchListener {

</span><span class="kwd">private</span><span class="pln"> </span><span class="typ">ImageView</span><span class="pln"> img_test</span><span class="pun">;</span><span class="pln">

</span><span class="com">// Zoom control < / span > < span class = "PLN" >
</span><span class="kwd">private</span><span class="pln"> </span><span class="typ">Matrix</span><span class="pln"> matrix </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Matrix</span><span class="pun">();</span><span class="pln">
</span><span class="kwd">private</span><span class="pln"> </span><span class="typ">Matrix</span><span class="pln"> savedMatrix </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Matrix</span><span class="pun">();</span><span class="pln">

</span><span class="com">// Representation of different states: < / span > < span class = "PLN" >
</span><span class="kwd">private</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> NONE </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">private</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> DRAG </span><span class="pun">=</span><span class="pln"> </span><span class="lit">1</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">private</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> ZOOM </span><span class="pun">=</span><span class="pln"> </span><span class="lit">2</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">private</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> mode </span><span class="pun">=</span><span class="pln"> NONE</span><span class="pun">;</span><span class="pln">

</span><span class="com">// Define the first pressing point, the focus of the two contact points, and the pressing distance between the two fingers of the accident: < / span > < span class = "PLN" >
</span><span class="kwd">private</span><span class="pln"> </span><span class="typ">PointF</span><span class="pln"> startPoint </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">PointF</span><span class="pun">();</span><span class="pln">
</span><span class="kwd">private</span><span class="pln"> </span><span class="typ">PointF</span><span class="pln"> midPoint </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">PointF</span><span class="pun">();</span><span class="pln">
</span><span class="kwd">private</span><span class="pln"> </span><span class="kwd">float</span><span class="pln"> oriDis </span><span class="pun">=</span><span class="pln"> </span><span class="lit">1f</span><span class="pun">;</span><span class="pln">

</span><span class="com">/*
 * (non-Javadoc)
 * 
 * @see android.app.Activity#onCreate(android.os.Bundle)
 */</span><span class="pln">
</span><span class="lit">@Override</span><span class="pln">
</span><span class="kwd">protected</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> onCreate</span><span class="pun">(</span><span class="typ">Bundle</span><span class="pln"> savedInstanceState</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">super</span><span class="pun">.</span><span class="pln">onCreate</span><span class="pun">(</span><span class="pln">savedInstanceState</span><span class="pun">);</span><span class="pln">
    setContentView</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">layout</span><span class="pun">.</span><span class="pln">activity_main</span><span class="pun">);</span><span class="pln">
    img_test </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="typ">ImageView</span><span class="pun">)</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">findViewById</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">id</span><span class="pun">.</span><span class="pln">img_test</span><span class="pun">);</span><span class="pln">
    img_test</span><span class="pun">.</span><span class="pln">setOnTouchListener</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="lit">@Override</span><span class="pln">
</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">boolean</span><span class="pln"> onTouch</span><span class="pun">(</span><span class="typ">View</span><span class="pln"> v</span><span class="pun">,</span><span class="pln"> </span><span class="typ">MotionEvent</span><span class="pln"> </span><span class="kwd">event</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="typ">ImageView</span><span class="pln"> view </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="typ">ImageView</span><span class="pun">)</span><span class="pln"> v</span><span class="pun">;</span><span class="pln">
    </span><span class="kwd">switch</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">event</span><span class="pun">.</span><span class="pln">getAction</span><span class="pun">()</span><span class="pln"> </span><span class="pun">&amp;</span><span class="pln"> </span><span class="typ">MotionEvent</span><span class="pun">.</span><span class="pln">ACTION_MASK</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="com">// Single finger < / span > < span class = "PLN" >
    </span><span class="kwd">case</span><span class="pln"> </span><span class="typ">MotionEvent</span><span class="pun">.</span><span class="pln">ACTION_DOWN</span><span class="pun">:</span><span class="pln">
        matrix</span><span class="pun">.</span><span class="kwd">set</span><span class="pun">(</span><span class="pln">view</span><span class="pun">.</span><span class="pln">getImageMatrix</span><span class="pun">());</span><span class="pln">
        savedMatrix</span><span class="pun">.</span><span class="kwd">set</span><span class="pun">(</span><span class="pln">matrix</span><span class="pun">);</span><span class="pln">
        startPoint</span><span class="pun">.</span><span class="kwd">set</span><span class="pun">(</span><span class="kwd">event</span><span class="pun">.</span><span class="pln">getX</span><span class="pun">(),</span><span class="pln"> </span><span class="kwd">event</span><span class="pun">.</span><span class="pln">getY</span><span class="pun">());</span><span class="pln">
        mode </span><span class="pun">=</span><span class="pln"> DRAG</span><span class="pun">;</span><span class="pln">
        </span><span class="kwd">break</span><span class="pun">;</span><span class="pln">
    </span><span class="com">// Double finger < / span > < span class = "PLN" >
    </span><span class="kwd">case</span><span class="pln"> </span><span class="typ">MotionEvent</span><span class="pun">.</span><span class="pln">ACTION_POINTER_DOWN</span><span class="pun">:</span><span class="pln">
        oriDis </span><span class="pun">=</span><span class="pln"> distance</span><span class="pun">(</span><span class="kwd">event</span><span class="pun">);</span><span class="pln">
        </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">oriDis </span><span class="pun">&gt;</span><span class="pln"> </span><span class="lit">10f</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
            savedMatrix</span><span class="pun">.</span><span class="kwd">set</span><span class="pun">(</span><span class="pln">matrix</span><span class="pun">);</span><span class="pln">
            midPoint </span><span class="pun">=</span><span class="pln"> middle</span><span class="pun">(</span><span class="kwd">event</span><span class="pun">);</span><span class="pln">
            mode </span><span class="pun">=</span><span class="pln"> ZOOM</span><span class="pun">;</span><span class="pln">
        </span><span class="pun">}</span><span class="pln">
        </span><span class="kwd">break</span><span class="pun">;</span><span class="pln">
    </span><span class="com">// Finger release < / span > < span class = "PLN" >
    </span><span class="kwd">case</span><span class="pln"> </span><span class="typ">MotionEvent</span><span class="pun">.</span><span class="pln">ACTION_UP</span><span class="pun">:</span><span class="pln">
    </span><span class="kwd">case</span><span class="pln"> </span><span class="typ">MotionEvent</span><span class="pun">.</span><span class="pln">ACTION_POINTER_UP</span><span class="pun">:</span><span class="pln">
        mode </span><span class="pun">=</span><span class="pln"> NONE</span><span class="pun">;</span><span class="pln">
        </span><span class="kwd">break</span><span class="pun">;</span><span class="pln">
    </span><span class="com">// Single finger sliding event < / span > < span class = "PLN" >
    </span><span class="kwd">case</span><span class="pln"> </span><span class="typ">MotionEvent</span><span class="pun">.</span><span class="pln">ACTION_MOVE</span><span class="pun">:</span><span class="pln">
        </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">mode </span><span class="pun">==</span><span class="pln"> DRAG</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
            </span><span class="com">// Is a finger dragging < / span > < span class = "PLN" >
            matrix</span><span class="pun">.</span><span class="kwd">set</span><span class="pun">(</span><span class="pln">savedMatrix</span><span class="pun">);</span><span class="pln">
            matrix</span><span class="pun">.</span><span class="pln">postTranslate</span><span class="pun">(</span><span class="kwd">event</span><span class="pun">.</span><span class="pln">getX</span><span class="pun">()</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> startPoint</span><span class="pun">.</span><span class="pln">x</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">event</span><span class="pun">.</span><span class="pln">getY</span><span class="pun">()</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> startPoint</span><span class="pun">.</span><span class="pln">y</span><span class="pun">);</span><span class="pln">
        </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">mode </span><span class="pun">==</span><span class="pln"> ZOOM</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
            </span><span class="com">// Slide two fingers < / span > < span class = "PLN" >
            </span><span class="kwd">float</span><span class="pln"> newDist </span><span class="pun">=</span><span class="pln"> distance</span><span class="pun">(</span><span class="kwd">event</span><span class="pun">);</span><span class="pln">
            </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">newDist </span><span class="pun">&gt;</span><span class="pln"> </span><span class="lit">10f</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
                matrix</span><span class="pun">.</span><span class="kwd">set</span><span class="pun">(</span><span class="pln">savedMatrix</span><span class="pun">);</span><span class="pln">
                </span><span class="kwd">float</span><span class="pln"> scale </span><span class="pun">=</span><span class="pln"> newDist </span><span class="pun">/</span><span class="pln"> oriDis</span><span class="pun">;</span><span class="pln">
                matrix</span><span class="pun">.</span><span class="pln">postScale</span><span class="pun">(</span><span class="pln">scale</span><span class="pun">,</span><span class="pln"> scale</span><span class="pun">,</span><span class="pln"> midPoint</span><span class="pun">.</span><span class="pln">x</span><span class="pun">,</span><span class="pln"> midPoint</span><span class="pun">.</span><span class="pln">y</span><span class="pun">);</span><span class="pln">
            </span><span class="pun">}</span><span class="pln">
        </span><span class="pun">}</span><span class="pln">
        </span><span class="kwd">break</span><span class="pun">;</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
    </span><span class="com">// Set the matrix of ImageView < / span > < span class = "PLN" >
    view</span><span class="pun">.</span><span class="pln">setImageMatrix</span><span class="pun">(</span><span class="pln">matrix</span><span class="pun">);</span><span class="pln">
    </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="com">// Calculate the distance between two touch points < / span > < span class = "PLN" >
</span><span class="kwd">private</span><span class="pln"> </span><span class="kwd">float</span><span class="pln"> distance</span><span class="pun">(</span><span class="typ">MotionEvent</span><span class="pln"> </span><span class="kwd">event</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">float</span><span class="pln"> x </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">event</span><span class="pun">.</span><span class="pln">getX</span><span class="pun">(</span><span class="lit">0</span><span class="pun">)</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> </span><span class="kwd">event</span><span class="pun">.</span><span class="pln">getX</span><span class="pun">(</span><span class="lit">1</span><span class="pun">);</span><span class="pln">
    </span><span class="kwd">float</span><span class="pln"> y </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">event</span><span class="pun">.</span><span class="pln">getY</span><span class="pun">(</span><span class="lit">0</span><span class="pun">)</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> </span><span class="kwd">event</span><span class="pun">.</span><span class="pln">getY</span><span class="pun">(</span><span class="lit">1</span><span class="pun">);</span><span class="pln">
    </span><span class="kwd">return</span><span class="pln"> </span><span class="typ">FloatMath</span><span class="pun">.</span><span class="pln">sqrt</span><span class="pun">(</span><span class="pln">x </span><span class="pun">*</span><span class="pln"> x </span><span class="pun">+</span><span class="pln"> y </span><span class="pun">*</span><span class="pln"> y</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="com">// Calculate the midpoint of two touch points < / span > < span class = "PLN" >
</span><span class="kwd">private</span><span class="pln"> </span><span class="typ">PointF</span><span class="pln"> middle</span><span class="pun">(</span><span class="typ">MotionEvent</span><span class="pln"> </span><span class="kwd">event</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">float</span><span class="pln"> x </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">event</span><span class="pun">.</span><span class="pln">getX</span><span class="pun">(</span><span class="lit">0</span><span class="pun">)</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="kwd">event</span><span class="pun">.</span><span class="pln">getX</span><span class="pun">(</span><span class="lit">1</span><span class="pun">);</span><span class="pln">
    </span><span class="kwd">float</span><span class="pln"> y </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">event</span><span class="pun">.</span><span class="pln">getY</span><span class="pun">(</span><span class="lit">0</span><span class="pun">)</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="kwd">event</span><span class="pun">.</span><span class="pln">getY</span><span class="pun">(</span><span class="lit">1</span><span class="pun">);</span><span class="pln">
    </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">PointF</span><span class="pun">(</span><span class="pln">x </span><span class="pun">/</span><span class="pln"> </span><span class="lit">2</span><span class="pun">,</span><span class="pln"> y </span><span class="pun">/</span><span class="pln"> </span><span class="lit">2</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

}

Summary of this section:

OK, that's all about TouchListener, OnTouchEvent and multi touch~

Topics: Android