开发者

Android - Hijack clicks [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 4 years ago.

Improve this question

In one of my projects i want to collect somethi开发者_如何学编程ng like a click heatmap. Is there any way to do this in Android?

I was thinking about adding a transparent layout above the original layout and "hijack" clicks there and then pass it to the underlying layout. But I can't figure it out.

So, any ideas on how to do this?


i just found the solution myself. It lies in the onInterceptTouchEvent() function. You can override it to intercept all touch events before being handed over to the child views. So i created a layout extending FrameLayout. Surrounded my layout with this layout and thats it :)


Do you just want to capture how many times a specific view is clicked? Or are you wanting the precise pixel coordinates of the touch? If the former, you could just override the onTouchListener for each of them, and increment a counter for that view.

For the latter, I played around a bit with an onTouchListener, and was able to get it to work, but you'll probably have to set an onTouchListener to every view, which shouldn't be a big deal, but just something to keep in mind unless someone has a better way.

Vector2D.java

public class Vector2D {
    private float x;
    private float y;

    public Vector2D(float x, float y) {
        this.x = x;
        this.y = y;
    }

    public void setX(float x) {
        this.x = x;
    }

    public float getX() {
        return this.x;
    }

    public void setY(float y) {
        this.y = y;
    }

    public float getY() {
        return this.y;
    }
}

Main.java

public class Main extends Activity implements OnTouchListener {
    /** Called when the activity is first created. 
     * @return */

    //defined as class variable so it's accessible from onTouch()
    List<Vector2D> points;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        //adds a new coordinate to the list, 
        //with the X and Y values of the touch
        points.add(new Vector2D(event.getX(), event.getY()));
        Log.d("TOUCH", "X:" + event.getX() + " Y:" + event.getY());
        return true;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);     

        points = new ArrayList<Vector2D>();       
        final LinearLayout ll = (LinearLayout)findViewById(R.id.linearlayout);
        final TextView pointsList = (TextView)findViewById(R.id.points_list);
        ll.setOnTouchListener(this);
        final Button listPoints = (Button)findViewById(R.id.list_points);
        listPoints.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuilder sb = new StringBuilder();
                for (Vector2D vector : points) {
                    sb.append(
                        "X:" +vector.getX() + " " + 
                        "Y:" + vector.getY() + "\n");
                }
                pointsList.setText(sb.toString());
            }
        });
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜