Code example for MotionEvent

Methods: getActionMaskedgetDownTimegetEventTime, copy

0
        } 
        public class MotionEventRecord extends Record { 
            public MotionEvent event;
            public MotionEventRecord(long when, MotionEvent event) {
                this.time = when;
                this.event = event.copy();
            } 
            String actionName(int action) {
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        return "down"; 
                    case MotionEvent.ACTION_UP:
                        return "up"; 
                    case MotionEvent.ACTION_MOVE:
                        return "move"; 
                    case MotionEvent.ACTION_CANCEL:
                        return "cancel"; 
                    default: 
                        return String.valueOf(action);
                } 
            } 
            public String toJson() {
                return String.format(
                        ("{\"type\":\"motion\", \"time\":%d, \"action\":\"%s\", " 
                            + "\"x\":%.2f, \"y\":%.2f, \"s\":%.2f, \"p\":%.2f}"), 
                        this.time,
                        actionName(this.event.getAction()),
                        this.event.getRawX(),
                        this.event.getRawY(),
                        this.event.getSize(),
                        this.event.getPressure()
                        ); 
            } 
        } 
        public class TagRecord extends Record { 
            public String tag, info;
            public TagRecord(long when, String tag, String info) {
                this.time = when;
                this.tag = tag;
                this.info = info;
            } 
            public String toJson() {
                return String.format("{\"type\":\"tag\", \"time\":%d, \"tag\":\"%s\", \"info\":\"%s\"}",
                        this.time,
                        this.tag,
                        this.info
                        ); 
            } 
        } 
        private LinkedList<Record> mRecords = new LinkedList<Record>();
        private HashSet<String> mTags = new HashSet<String>();
        long mDownTime = -1;
        boolean mComplete = false;
 
        public void add(MotionEvent ev) {
            mRecords.add(new MotionEventRecord(ev.getEventTime(), ev));
            if (mDownTime < 0) {
                mDownTime = ev.getDownTime();
            } else { 
                if (mDownTime != ev.getDownTime()) {
                    Slog.w(TAG, "Assertion failure in GestureRecorder: event downTime ("
                            +ev.getDownTime()+") does not match gesture downTime ("+mDownTime+")");
                } 
            } 
            switch (ev.getActionMasked()) {
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    mComplete = true;
            } 
        }