Code
Code one: Customizing TextView for multi-touch support
http://download.csdn.net/detail/zhiyuan0932/9513852
What is multi-touch
A technique that allows computer users to control the graphical interface at the same time through multiple fingers
Multi-Touch Application scenarios
- Zoom in or out of pictures, text, or Web pages
- Multi-finger gestures manipulate custom controls and layouts
Important ways to touch events
-
Event.getactionmasked (); Get event Type
When using only one-finger operation, this method generally uses event.getaction () to get the event type, but for multiple fingers we use event.getactionmasked () or event.getaction () & Motionevent.action_mask To get event types that support multiple finger touches.
-
Motionevent.action_pointer_down Finger Press Event
This method can be used to get the state of multiple fingers pressed
-
MOTIONEVENT.ACTION_POINTER_UP Finger Lift Event
This method can get the state of multiple fingers lifted up
-
Motionevent.action_move Finger Move Event
-
Event.getpointercount () Get the number of fingers
This method is to get the number of current fingers
Case one: Font scaling with two-finger touch
- Paste the code directly here, first put out the layout code
<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"> <com. Example. Scaletextview. ZoomtextviewAndroid:id="@+id/textview"Android:layout_width="Match_parent"android:layout_height="Match_parent"Android:layout_centerinparent="true"android:gravity="Center"android:text="@string/hello_world"Android:textsize="30SP"/></relativelayout>
- Paste TextView code with custom support for multi-touch control
PackageCom.example.scaletextview;ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.util.FloatMath;ImportAndroid.util.Log;ImportAndroid.view.MotionEvent;ImportAndroid.widget.TextView; Public class zoomtextview extends TextView { Private Static FinalString TAG ="Zoomtextview";Private floatTextSize;Private intModePrivate floatOlddist; Public Zoomtextview(context context, AttributeSet attrs,intDefstyle) {Super(Context, attrs, Defstyle); } Public Zoomtextview(context context, AttributeSet attrs) {Super(context, attrs); } Public Zoomtextview(Context context) {Super(context); }/** * Handling TextView Touch Events * / @Override Public Boolean ontouchevent(Motionevent event) {//At the beginning, calculate the size of the current font if(TextSize = =0) {textSize = This. GetTextSize (); }//Gets the type of the touch event, if the single point is event.getaction (), when multi-touch is involved, use getactionmasked to get the touch event type Switch(Event.getactionmasked ()) { CaseMotionevent.action_pointer_down://When the finger is pressed, go to get the spacing of the current fingerolddist = Spacing (event); Break; CaseMotionevent.action_move://Get the number of current touch points if(Event.getpointercount () >=2) {//If touch point >=2 get the distance of the current two fingers, then zoom floatnewdist = Spacing (event); Zoom (newdist/olddist);//ResetOlddist = newdist; } Break; }return true; }/** * Constant Scaling * * @param F */ Private void Zoom(floatf) {textSize *= F; This. Settextsize (PX2SP (GetContext (), textSize)); }/** * Convert px value to SP value, keep text size unchanged * * @param pxvalue * @param Fontscale * (D Isplaymetrics property in Class scaleddensity) * @return * / Public Static int PX2SP(Context context,floatPxvalue) {floatFontscale = Context.getresources (). Getdisplaymetrics (). scaleddensity;return(int) (Pxvalue/fontscale +0.5f); }/** * Calculates the size of two fingers * * @param event * @return * * Private float spacing(Motionevent event) {//Gets the x-coordinate of the first point and the x-coordinate of the second point floatx = Event.getx (0)-Event.getx (1);//Get y-coordinate separately floaty = event.gety (0)-Event.gety (1);//Use Pythagorean theorem to calculate two-point distance returnFLOATMATH.SQRT (x * x + y * y); }}
Multi-Touch in Android