Android开发:多点触控测试代码 PointerLocation


PointerLocation这个多点触控测试程序,在Android的源码中有,只包括下面的两个文件,

android\development\apps\Development\src\com\android\development\PointerLocation.java

android\frameworks\base\core\java\com\android\internal\widget\PointerLocationView.java

新建个工程,只要将这两个文件拷贝出来,修改下包名,就可以运行起来,真好用。

本文工程源码下载地址:

免费下载地址在 http://linux.bkjia.com/

用户名与密码都是www.bkjia.com

具体下载目录在 /pub/Android源码集锦/2011年/10月/Android开发:多点触控测试代码 PointerLocation/

 

PointerLocation.java

  1. package com.ckl.PointerLocation;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.WindowManager;  
  6.   
  7. /** 
  8.  * Demonstrates wrapping a layout in a ScrollView. 
  9.  * 
  10.  */  
  11. public class PointerLocation extends Activity {  
  12.     @Override  
  13.     protected void onCreate(Bundle icicle) {  
  14.         super.onCreate(icicle);  
  15.         setContentView(new PointerLocationView(this));  
  16.           
  17.         // Make the screen full bright for this activity.   
  18.         WindowManager.LayoutParams lp = getWindow().getAttributes();  
  19.         lp.screenBrightness = 1.0f;  
  20.         getWindow().setAttributes(lp);  
  21.     }  
  22. }  
PointerLocationView.java
  1. /* 
  2.  * Copyright (C) 2010 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16.   
  17. package com.ckl.PointerLocation;  
  18.   
  19. import android.content.Context;  
  20. import android.graphics.Canvas;  
  21. import android.graphics.Paint;  
  22. import android.graphics.Paint.FontMetricsInt;  
  23. import android.util.Log;  
  24. import android.view.MotionEvent;  
  25. import android.view.VelocityTracker;  
  26. import android.view.View;  
  27. import android.view.ViewConfiguration;  
  28.   
  29. import java.util.ArrayList;  
  30.   
  31. public class PointerLocationView extends View {  
  32.     public static class PointerState {  
  33.         private final ArrayList<Float> mXs = new ArrayList<Float>();  
  34.         private final ArrayList<Float> mYs = new ArrayList<Float>();  
  35.         private boolean mCurDown;  
  36.         private int mCurX;  
  37.         private int mCurY;  
  38.         private float mCurPressure;  
  39.         private float mCurSize;  
  40.         private int mCurWidth;  
  41.         private VelocityTracker mVelocity;  
  42.     }  
  43.   
  44.     private final ViewConfiguration mVC;  
  45.     private final Paint mTextPaint;  
  46.     private final Paint mTextBackgroundPaint;  
  47.     private final Paint mTextLevelPaint;  
  48.     private final Paint mPaint;  
  49.     private final Paint mTargetPaint;  
  50.     private final Paint mPathPaint;  
  51.     private final FontMetricsInt mTextMetrics = new FontMetricsInt();  
  52.     private int mHeaderBottom;  
  53.     private boolean mCurDown;  
  54.     private int mCurNumPointers;  
  55.     private int mMaxNumPointers;  
  56.     private final ArrayList<PointerState> mPointers  
  57.              = new ArrayList<PointerState>();  
  58.       
  59.     private boolean mPrintCoords = true;  
  60.       
  61.     public PointerLocationView(Context c) {  
  62.         super(c);  
  63.         setFocusable(true);  
  64.         mVC = ViewConfiguration.get(c);  
  65.         mTextPaint = new Paint();  
  66.         mTextPaint.setAntiAlias(true);  
  67.         mTextPaint.setTextSize(10  
  68.                 * getResources().getDisplayMetrics().density);  
  69.         mTextPaint.setARGB(255000);  
  70.         mTextBackgroundPaint = new Paint();  
  71.         mTextBackgroundPaint.setAntiAlias(false);  
  72.         mTextBackgroundPaint.setARGB(128255255255);  
  73.         mTextLevelPaint = new Paint();  
  74.         mTextLevelPaint.setAntiAlias(false);  
  75.         mTextLevelPaint.setARGB(19225500);  
  76.         mPaint = new Paint();  
  77.         mPaint.setAntiAlias(true);  
  78.         mPaint.setARGB(255255255255);  
  79.         mPaint.setStyle(Paint.Style.STROKE);  
  80.         mPaint.setStrokeWidth(2);  
  81.         mTargetPaint = new Paint();  
  82.         mTargetPaint.setAntiAlias(false);  
  83.         mTargetPaint.setARGB(25500192);  
  84.         mPathPaint = new Paint();  
  85.         mPathPaint.setAntiAlias(false);  
  86.         mPathPaint.setARGB(255096255);  
  87.         mPaint.setStyle(Paint.Style.STROKE);  
  88.         mPaint.setStrokeWidth(1);  
  89.           
  90.         PointerState ps = new PointerState();  
  91.         ps.mVelocity = VelocityTracker.obtain();  
  92.         mPointers.add(ps);  
  93.     }  
  94.   
  95.     public void setPrintCoords(boolean state) {  
  96.         mPrintCoords = state;  
  97.     }  
  98.       
  99.     @Override  
  100.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  101.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  102.         mTextPaint.getFontMetricsInt(mTextMetrics);  
  103.         mHeaderBottom = -mTextMetrics.ascent+mTextMetrics.descent+2;  
  104.         if (false) {  
  105.             Log.i("foo""Metrics: ascent=" + mTextMetrics.ascent  
  106.                     + " descent=" + mTextMetrics.descent  
  107.                     + " leading=" + mTextMetrics.leading  
  108.                     + " top=" + mTextMetrics.top  
  109.                     + " bottom=" + mTextMetrics.bottom);  
  110.         }  
  111.     }  
  112.   
  113.     @Override  
  114.     protected void onDraw(Canvas canvas) {  
  115.         synchronized (mPointers) {  
  116.             final int w = getWidth();  
  117.             final int itemW = w/7;  
  118.             final int base = -mTextMetrics.ascent+1;  
  119.             final int bottom = mHeaderBottom;  
  120.               
  121.             final int NP = mPointers.size();  
  122.               
  123.             if (NP > 0) {  
  124.                 final PointerState ps = mPointers.get(0);  
  125.                 canvas.drawRect(00, itemW-1, bottom,mTextBackgroundPaint);  
  126.                 canvas.drawText("P: " + mCurNumPointers + " / " + mMaxNumPointers,  
  127.                         1, base, mTextPaint);  
  128.                   
  129.                 final int N = ps.mXs.size();  
  130.                 if ((mCurDown && ps.mCurDown) || N == 0) {  
  131.                     canvas.drawRect(itemW, 0, (itemW * 2) - 1, bottom, mTextBackgroundPaint);  
  132.                     canvas.drawText("X: " + ps.mCurX, 1 + itemW, base, mTextPaint);  
  133.                     canvas.drawRect(itemW * 20, (itemW * 3) - 1, bottom, mTextBackgroundPaint);  
  134.                     canvas.drawText("Y: " + ps.mCurY, 1 + itemW * 2, base, mTextPaint);  
  135.                 } else {  
  136.                     float dx = ps.mXs.get(N-1) - ps.mXs.get(0);  
  137.                     float dy = ps.mYs.get(N-1) - ps.mYs.get(0);  
  138.                     canvas.drawRect(itemW, 0, (itemW * 2) - 1, bottom,  
  139.                             Math.abs(dx) < mVC.getScaledTouchSlop()  
  140.                             ? mTextBackgroundPaint : mTextLevelPaint);  
  141.                     canvas.drawText("dX: " + String.format("%.1f", dx), 1 + itemW, base, mTextPaint);  
  142.                     canvas.drawRect(itemW * 20, (itemW * 3) - 1, bottom,  
  143.                             Math.abs(dy) < mVC.getScaledTouchSlop()  
  144.                             ? mTextBackgroundPaint : mTextLevelPaint);  
  145.                     canvas.drawText("dY: " + String.format("%.1f", dy), 1 + itemW * 2, base, mTextPaint);  
  146.                 }  
  147.                   
  148.                 canvas.drawRect(itemW * 30, (itemW * 4) - 1, bottom, mTextBackgroundPaint);  
  149.                 int velocity = ps.mVelocity == null ? 0 : (int) (ps.mVelocity.getXVelocity() * 1000);  
  150.                 canvas.drawText("Xv: " + velocity, 1 + itemW * 3, base, mTextPaint);  
  151.                   
  152.                 canvas.drawRect(itemW * 40, (itemW * 5) - 1, bottom, mTextBackgroundPaint);  
  153.                 velocity = ps.mVelocity == null ? 0 : (int) (ps.mVelocity.getYVelocity() * 1000);  
  154.                 canvas.drawText("Yv: " + velocity, 1 + itemW * 4, base, mTextPaint);  
  155.                   
  156.                 canvas.drawRect(itemW * 50, (itemW * 6) - 1, bottom, mTextBackgroundPaint);  
  157.                 canvas.drawRect(itemW * 50, (itemW * 5) + (ps.mCurPressure * itemW) - 1,  
  158.                         bottom, mTextLevelPaint);  
  159.                 canvas.drawText("Prs: " + String.format("%.2f", ps.mCurPressure), 1 + itemW * 5,  
  160.                         base, mTextPaint);  
  161.                   
  162.                 canvas.drawRect(itemW * 60, w, bottom, mTextBackgroundPaint);  
  163.                 canvas.drawRect(itemW * 60, (itemW * 6) + (ps.mCurSize * itemW) - 1,  
  164.                         bottom, mTextLevelPaint);  
  165.                 canvas.drawText("Size: " + String.format("%.2f", ps.mCurSize), 1 + itemW * 6,  
  166.                         base, mTextPaint);  
  167.             }  
  168.               
  169.             for (int p=0; p<NP; p++) {  
  170.                 final PointerState ps = mPointers.get(p);  
  171.                   
  172.                 if (mCurDown && ps.mCurDown) {  
  173.                     canvas.drawLine(0, (int)ps.mCurY, getWidth(), (int)ps.mCurY, mTargetPaint);  
  174.                     canvas.drawLine((int)ps.mCurX, 0, (int)ps.mCurX, getHeight(), mTargetPaint);  
  175.                     int pressureLevel = (int)(ps.mCurPressure*255);  
  176.                     mPaint.setARGB(255, pressureLevel, 128255-pressureLevel);  
  177.                     canvas.drawPoint(ps.mCurX, ps.mCurY, mPaint);  
  178.                     canvas.drawCircle(ps.mCurX, ps.mCurY, ps.mCurWidth, mPaint);  
  179.                 }  
  180.             }  
  181.               
  182.             for (int p=0; p<NP; p++) {  
  183.                 final PointerState ps = mPointers.get(p);  
  184.                   
  185.                 final int N = ps.mXs.size();  
  186.                 float lastX=0, lastY=0;  
  187.                 boolean haveLast = false;  
  188.                 boolean drawn = false;  
  189.                 mPaint.setARGB(255128255255);  
  190.                 for (int i=0; i<N; i++) {  
  191.                     float x = ps.mXs.get(i);  
  192.                     float y = ps.mYs.get(i);  
  193.                     if (Float.isNaN(x)) {  
  194.                         haveLast = false;  
  195.                         continue;  
  196.                     }  
  197.                     if (haveLast) {  
  198.                         canvas.drawLine(lastX, lastY, x, y, mPathPaint);  
  199.                         canvas.drawPoint(lastX, lastY, mPaint);  
  200.                         drawn = true;  
  201.                     }  
  202.                     lastX = x;  
  203.                     lastY = y;  
  204.                     haveLast = true;  
  205.                 }  
  206.                   
  207.                 if (drawn) {  
  208.                     if (ps.mVelocity != null) {  
  209.                         mPaint.setARGB(25525564128);  
  210.                         float xVel = ps.mVelocity.getXVelocity() * (1000/60);  
  211.                         float yVel = ps.mVelocity.getYVelocity() * (1000/60);  
  212.                         canvas.drawLine(lastX, lastY, lastX+xVel, lastY+yVel, mPaint);  
  213.                     } else {  
  214.                         canvas.drawPoint(lastX, lastY, mPaint);  
  215.                     }  
  216.                 }  
  217.             }  
  218.         }  
  219.     }  
  220.   
  221.     public void addTouchEvent(MotionEvent event) {  
  222.         synchronized (mPointers) {  
  223.             int action = event.getAction();  
  224.               
  225.             //Log.i("Pointer", "Motion: action=0x" + Integer.toHexString(action)   
  226.             //        + " pointers=" + event.getPointerCount());   
  227.               
  228.             int NP = mPointers.size();  
  229.               
  230.             //mRect.set(0, 0, getWidth(), mHeaderBottom+1);   
  231.             //invalidate(mRect);   
  232.             //if (mCurDown) {   
  233.             //    mRect.set(mCurX-mCurWidth-3, mCurY-mCurWidth-3,   
  234.             //            mCurX+mCurWidth+3, mCurY+mCurWidth+3);   
  235.             //} else {   
  236.             //    mRect.setEmpty();   
  237.             //}   
  238.             if (action == MotionEvent.ACTION_DOWN) {  
  239.                 for (int p=0; p<NP; p++) {  
  240.                     final PointerState ps = mPointers.get(p);  
  241.                     ps.mXs.clear();  
  242.                     ps.mYs.clear();  
  243.                     ps.mVelocity = VelocityTracker.obtain();  
  244.                     ps.mCurDown = false;  
  245.                 }  
  246.                 mPointers.get(0).mCurDown = true;  
  247.                 mMaxNumPointers = 0;  
  248.                 if (mPrintCoords) {  
  249.                     Log.i("Pointer""Pointer 1: DOWN");  
  250.                 }  
  251.             }  
  252.               
  253.             if ((action&MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_DOWN) {  
  254.                 final int index = (action&MotionEvent.ACTION_POINTER_INDEX_MASK)  
  255.                         >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;  
  256.                 final int id = event.getPointerId(index);  
  257.                 while (NP <= id) {  
  258.                     PointerState ps = new PointerState();  
  259.                     ps.mVelocity = VelocityTracker.obtain();  
  260.                     mPointers.add(ps);  
  261.                     NP++;  
  262.                 }  
  263.                 final PointerState ps = mPointers.get(id);  
  264.                 ps.mVelocity = VelocityTracker.obtain();  
  265.                 ps.mCurDown = true;  
  266.                 if (mPrintCoords) {  
  267.                     Log.i("Pointer""Pointer " + (id+1) + ": DOWN");  
  268.                 }  
  269.             }  
  270.               
  271.             final int NI = event.getPointerCount();  
  272.               
  273.             mCurDown = action != MotionEvent.ACTION_UP  
  274.                     && action != MotionEvent.ACTION_CANCEL;  
  275.             mCurNumPointers = mCurDown ? NI : 0;  
  276.             if (mMaxNumPointers < mCurNumPointers) {  
  277.                 mMaxNumPointers = mCurNumPointers;  
  278.             }  
  279.               
  280.             for (int i=0; i<NI; i++) {  
  281.                 final int id = event.getPointerId(i);  
  282.                 final PointerState ps = mPointers.get(id);  
  283.                 ps.mVelocity.addMovement(event);  
  284.                 ps.mVelocity.computeCurrentVelocity(1);  
  285.                 final int N = event.getHistorySize();  
  286.                 for (int j=0; j<N; j++) {  
  287.                     if (mPrintCoords) {  
  288.                         Log.i("Pointer""Pointer " + (id+1) + ": ("  
  289.                                 + event.getHistoricalX(i, j)  
  290.                                 + ", " + event.getHistoricalY(i, j) + ")"  
  291.                                 + " Prs=" + event.getHistoricalPressure(i, j)  
  292.                                 + " Size=" + event.getHistoricalSize(i, j));  
  293.                     }  
  294.                     ps.mXs.add(event.getHistoricalX(i, j));  
  295.                     ps.mYs.add(event.getHistoricalY(i, j));  
  296.                 }  
  297.                 if (mPrintCoords) {  
  298.                     Log.i("Pointer""Pointer " + (id+1) + ": ("  
  299.                             + event.getX(i) + ", " + event.getY(i) + ")"  
  300.                             + " Prs=" + event.getPressure(i)  
  301.                             + " Size=" + event.getSize(i));  
  302.                 }  
  303.                 ps.mXs.add(event.getX(i));  
  304.                 ps.mYs.add(event.getY(i));  
  305.                 ps.mCurX = (int)event.getX(i);  
  306.                 ps.mCurY = (int)event.getY(i);  
  307.                 //Log.i("Pointer", "Pointer #" + p + ": (" + ps.mCurX   
  308.                 //        + "," + ps.mCurY + ")");   
  309.                 ps.mCurPressure = event.getPressure(i);  
  310.                 ps.mCurSize = event.getSize(i);  
  311.                 ps.mCurWidth = (int)(ps.mCurSize*(getWidth()/3));  
  312.             }  
  313.               
  314.             if ((action&MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_UP) {  
  315.                 final int index = (action&MotionEvent.ACTION_POINTER_INDEX_MASK)  
  316.                         >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;  
  317.                 final int id = event.getPointerId(index);  
  318.                 final PointerState ps = mPointers.get(id);  
  319.                 ps.mXs.add(Float.NaN);  
  320.                 ps.mYs.add(Float.NaN);  
  321.                 ps.mCurDown = false;  
  322.                 if (mPrintCoords) {  
  323.                     Log.i("Pointer""Pointer " + (id+1) + ": UP");  
  324.                 }  
  325.             }  
  326.               
  327.             if (action == MotionEvent.ACTION_UP) {  
  328.                 for (int i=0; i<NI; i++) {  
  329.                     final int id = event.getPointerId(i);  
  330.                     final PointerState ps = mPointers.get(id);  
  331.                     if (ps.mCurDown) {  
  332.                         ps.mCurDown = false;  
  333.                         if (mPrintCoords) {  
  334.                             Log.i("Pointer""Pointer " + (id+1) + ": UP");  
  335.                         }  
  336.                     }  
  337.                 }  
  338.             }  
  339.               
  340.             //if (mCurDown) {   
  341.             //    mRect.union(mCurX-mCurWidth-3, mCurY-mCurWidth-3,   
  342.             //            mCurX+mCurWidth+3, mCurY+mCurWidth+3);   
  343.             //}   
  344.             //invalidate(mRect);   
  345.             postInvalidate();  
  346.         }  
  347.     }  
  348.       
  349.     @Override  
  350.     public boolean onTouchEvent(MotionEvent event) {  
  351.         addTouchEvent(event);  
  352.         return true;  
  353.     }  
  354.   
  355.     @Override  
  356.     public boolean onTrackballEvent(MotionEvent event) {  
  357.         Log.i("Pointer""Trackball: " + event);  
  358.         return super.onTrackballEvent(event);  
  359.     }  
  360.       
  361. }  

相关内容