Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How do I get raw touch screen information?

I'm working on a painting application for Android and I'd like to use raw data from the device's touch screen to adjust the user's paint brush as they draw. I've seen other apps for Android (iSteam, for example) where the size of the brush is based on the size of your fingerprint on the screen. As far as painting apps go, that would be a huge feature.

Is there a way to get this data? I've googled for quite a while, but I haven't found any source demonstrating it. I know it's possible, because Dolphin Browser adds multi-touch support to the Hero without any changes beneath the application level. You must be able to get a 2D matrix of raw data or something...

I'd really appreciate any help I can get!

like image 700
Ben Gotow Avatar asked Jan 14 '10 22:01

Ben Gotow


People also ask

How do I get touch coordinates on Android?

setOnTouchListener(handleTouch); This gives you the touch event coordinates relative to the view that has the touch listener assigned to it. The top left corner of the view is (0, 0) . If you move your finger above the view, then y will be negative.

What is touch interface in Android?

A touch screen device is used for direct manipulation of objects on the screen. Since the user is directly touching the screen, the system does not require any additional affordances to indicate the objects being manipulated.

Which method you should override to control your touch action in Android?

Android supports multiple pointers, e.g. fingers which are interacting with the screen. The base class for touch support is the MotionEvent class which is passed to Views via the onTouchEvent() method. you override the onTouchEvent() method.

Why is my touch screen not working on my Android phone?

However, it's often one of the most successful ways to fix an unresponsive touch screen on Android. Restarting your phone shuts down and refreshes all background services, which could have crashed and led to your issue. Press and hold the Power button to display the power menu, then tap Restart if you're able.


2 Answers

There are some properties in the Motion Event class. You can use the getSize() method to find the size of the object. The Motion Event class also gives access to pressure, coordinates etc...

like image 151
keyboardP Avatar answered Sep 20 '22 16:09

keyboardP


If you check the APIDemos in the SDK there's a simple paitning app called TouchPaint

package com.example.android.apis.graphics;

It uses the following to draw on the canvas

@Override public boolean onTouchEvent(MotionEvent event) {
            int action = event.getAction();
            mCurDown = action == MotionEvent.ACTION_DOWN
                    || action == MotionEvent.ACTION_MOVE;
            int N = event.getHistorySize();
            for (int i=0; i<N; i++) {
                //Log.i("TouchPaint", "Intermediate pointer #" + i);
                drawPoint(event.getHistoricalX(i), event.getHistoricalY(i),
                        event.getHistoricalPressure(i),
                        event.getHistoricalSize(i));
            }
            drawPoint(event.getX(), event.getY(), event.getPressure(),
                    event.getSize());
            return true;
        }

private void drawPoint(float x, float y, float pressure, float size) {
            //Log.i("TouchPaint", "Drawing: " + x + "x" + y + " p="
            //        + pressure + " s=" + size);
            mCurX = (int)x;
            mCurY = (int)y;
            mCurPressure = pressure;
            mCurSize = size;
            mCurWidth = (int)(mCurSize*(getWidth()/3));
            if (mCurWidth < 1) mCurWidth = 1;
            if (mCurDown && mBitmap != null) {
                int pressureLevel = (int)(mCurPressure*255);
                mPaint.setARGB(pressureLevel, 255, 255, 255);
                mCanvas.drawCircle(mCurX, mCurY, mCurWidth, mPaint);
                mRect.set(mCurX-mCurWidth-2, mCurY-mCurWidth-2,
                        mCurX+mCurWidth+2, mCurY+mCurWidth+2);
                invalidate(mRect);
            }
            mFadeSteps = 0;
        }

Hope that helps :)

like image 41
Ally Avatar answered Sep 20 '22 16:09

Ally