Home | History | Annotate | Download | only in touch
      1 /*
      2  * Copyright (C) 2014 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.android.cts.verifier.projection.touch;
     18 
     19 import android.content.Context;
     20 import android.graphics.Canvas;
     21 import android.graphics.Color;
     22 import android.graphics.Paint;
     23 import android.graphics.Point;
     24 import android.util.AttributeSet;
     25 import android.view.MotionEvent;
     26 import android.view.View;
     27 
     28 import java.util.ArrayList;
     29 import java.util.List;
     30 /*
     31  * Simple view that draws a circle where a touch is registered
     32  */
     33 public class TouchPointView extends View {
     34     @SuppressWarnings("unused")
     35     private static final String TAG = TouchPointView.class.getSimpleName();
     36 
     37     private final int[] mColors = {
     38             Color.RED,
     39             Color.GREEN,
     40             Color.BLUE,
     41             Color.YELLOW,
     42             Color.MAGENTA,
     43             Color.BLACK,
     44             Color.DKGRAY
     45     };
     46     List<Finger> mFingers;
     47     Paint mPaint;
     48 
     49     public TouchPointView(Context context, AttributeSet attrs) {
     50         this(context, attrs, 0);
     51     }
     52 
     53     public TouchPointView(Context context, AttributeSet attrs, int defStyle) {
     54         super(context, attrs, defStyle);
     55         mFingers = new ArrayList<Finger>();
     56 
     57         mPaint = new Paint();
     58         mPaint.setStyle(Paint.Style.FILL);
     59     }
     60 
     61     @Override
     62     public boolean onTouchEvent(MotionEvent event) {
     63         mFingers.clear();
     64         if (event.getActionMasked() == MotionEvent.ACTION_UP) {
     65             invalidate();
     66             return true;
     67         }
     68         for (int i = 0; i < event.getPointerCount(); i++) {
     69             int pointerId = event.getPointerId(i);
     70             int pointerIndex = event.findPointerIndex(pointerId);
     71             Finger finger = new Finger();
     72             finger.point =  new Point((int)event.getX(pointerIndex), (int)event.getY(pointerIndex));
     73             finger.pointerId = pointerId;
     74 
     75             mFingers.add(finger);
     76         }
     77         invalidate();
     78         return true;
     79     }
     80 
     81     @Override
     82     public void onDraw(Canvas canvas) {
     83         int radius = canvas.getWidth() / 20;
     84         for (int i = 0; i < mFingers.size(); i++) {
     85             Finger finger = mFingers.get(i);
     86             Point point = finger.point;
     87             int color = mColors[finger.pointerId % mColors.length];
     88             mPaint.setColor(color);
     89             canvas.drawCircle(point.x, point.y, radius, mPaint);
     90         }
     91     }
     92 
     93     private class Finger {
     94         public Point point;
     95         public int pointerId;
     96     }
     97 }
     98 
     99