android–多點觸控的實現

來源:互聯網
上載者:User

Android多點觸控在本質上需要LCD驅動和程式本身設計上支援,目前市面上只要使用電容屏觸控原理的手機均可以支援多點觸控Multitouch技術,對於網頁縮放、手勢操作上有更好的使用者體驗。 在Android平台上事件均使用了MotionEvent對象方式處理,比如開始觸控時會觸發ACTION_DOWN而移動操作時為 ACTION_MOVE最終放開手指時觸發ACTION_UP事件。當然還有使用者無規則的操作可能觸發ACTION_CANCEL這個動作.

對於常規的控制項觸控操作在內部為View的setOnTouchListener()介面實現的 onTouchEvent()方法來處理。

下面用ImageView做示範:


                           

main.xml:

<?xml version="1.0" encoding="utf-8"?><br /><FrameLayout<br />xmlns:android="http://schemas.android.com/apk/res/android"<br />android:layout_width="fill_parent"<br />android:layout_height="fill_parent" ><br /><ImageView android:id="@+id/imageView"<br />android:layout_width="fill_parent"<br />android:layout_height="fill_parent"<br />android:src="@drawable/pic"<br />android:scaleType="matrix" ><br /></ImageView><br /></FrameLayout>


要注意的是 android:scaleType 的屬性必須設定為matrix,否則圖片無法改變大小。

java檔案:

package com.shao.muti;</p><p>import android.app.Activity;<br />import android.graphics.Matrix;<br />import android.graphics.PointF;<br />import android.os.Bundle;<br />import android.util.Log;<br />import android.view.MotionEvent;<br />import android.view.View;<br />import android.view.View.OnTouchListener;<br />import android.widget.ImageView;</p><p>public class MutiTouchActivity extends Activity implements OnTouchListener{<br /> /** Called when the activity is first created. */<br /> private static final String TAG="Touch";<br /> private static final int NONE= 0 ;<br /> private static final int DRAG = 1;<br /> private static final int ZOOM =2;<br /> int mode = NONE;<br /> Matrix matrix = new Matrix();<br /> Matrix savedMatrix = new Matrix();</p><p> PointF start = new PointF();<br /> PointF mid = new PointF();<br /> float oldDist = 1f;</p><p>@Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> ImageView view = (ImageView) findViewById(R.id.imageView);<br /> view.setOnTouchListener(this);<br />}</p><p>@Override<br />public boolean onTouch(View v, MotionEvent event) {<br />// TODO Auto-generated method stub<br />ImageView view = (ImageView) v;<br />printEventinfo(event);//測試輸出資訊<br />switch(event.getAction()&MotionEvent.ACTION_MASK)<br />{<br />case MotionEvent.ACTION_DOWN:<br />savedMatrix.set(matrix);<br /> //設置初始點位置<br />start.set(event.getX(),event.getY());<br />Log.d(TAG,"mode=DRAG");<br />mode =DRAG;<br />break;<br />case MotionEvent.ACTION_POINTER_1_DOWN:<br />oldDist= spacing(event);<br />Log.d(TAG,"lodDist="+oldDist);<br />if(oldDist>10f){<br />savedMatrix.set(matrix);<br />midPoint(mid,event);<br />mode = ZOOM;<br />Log.d(TAG,"mode=ZOOM");</p><p>}<br />break;<br />case MotionEvent.ACTION_UP:<br />case MotionEvent.ACTION_POINTER_1_UP:<br />mode =NONE;<br />Log.d(TAG,"mode=NONE");<br />break;<br />case MotionEvent.ACTION_MOVE:<br />if(mode==DRAG){<br />matrix.set(savedMatrix);<br />matrix.postTranslate(event.getX()-start.x, event.getY()-start.y);<br />}<br />else if(mode == ZOOM){<br /> float newDist = spacing(event);<br /> Log.d(TAG, "newDist=" + newDist);<br /> if (newDist > 10f) {<br /> matrix.set(savedMatrix);<br /> float scale = newDist / oldDist;<br /> matrix.postScale(scale, scale, mid.x, mid.y);<br /> }<br /> }<br />break;<br />}<br />view.setImageMatrix(matrix);<br />return true;</p><p>}<br />private void printEventinfo(MotionEvent event){<br />int num =0;<br />System.out.println("觸控動作---->"+event.getAction()); //擷取觸控動作比如ACTION_DOWN<br />System.out.println("取觸控點的數量--->"+event.getPointerCount()); //擷取觸控點的數量,比如2則可能是兩個手指同時按壓螢幕<br />num = event.getPointerCount();<br />for(int i=0;i<num;i++){<br />System.out.println("索引-->"+event.getPointerId(i)); //對於每個觸控的點的細節,我們可以通過一個迴圈執行getPointerId方法擷取索引<br />System.out.println("第i個觸控點的x位置-->"+event.getX(i)); //擷取第i個觸控點的x位置<br />System.out.println("第i個觸控點的y位置-->"+event.getY(i)); //擷取第i個點觸控的y位置<br />System.out.println("手指壓力-->"+event.getPressure(i)); //LCD可以感應出使用者的手指壓力,當然具體的層級由驅動和物理硬體決定的<br />}<br />System.out.println("開始時間"+event.getDownTime()); //按下開始時間<br />System.out.println(" 事件結束時間"+event.getEventTime()); // 事件結束時間<br />System.out.println("總共按下時花費時間"+(event.getEventTime()-event.getDownTime())); //總共按下時花費時間</p><p>}<br /> /** Determine the space between the first two fingers */<br />private float spacing(MotionEvent event){<br />float x = event.getX(0)-event.getY(1);<br />float y = event.getY(0)-event.getY(1);<br />return (float)Math.sqrt(x*x+y*y);<br />}<br />/** Calculate the mid point of the first two fingers */<br /> private void midPoint(PointF point, MotionEvent event) {<br /> float x = event.getX(0) + event.getX(1);<br /> float y = event.getY(0) + event.getY(1);<br /> point.set(x / 2, y / 2);<br /> }<br />}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.