How can I implement onTouch function android?

寵の児 提交于 2019-12-30 07:01:08

问题


Hi I want to create aplication which loads large image, and simple gesture I can move across it. I have to image printed out but I can not implement onTouch so it remains stationary. Any help apreseated. Thanks

My code for drawing out the picture:

@Override protected void onDraw(Canvas canvas) {
          super.onDraw(canvas);
                   Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);

            // make the entire canvas white
            paint.setColor(Color.WHITE);
            canvas.drawPaint(paint);
paint.setStrokeWidth(1);
            paint.setPathEffect(null);
            paint.setColor(Color.GRAY);
            paint.setAntiAlias(true);


            for (int i=1; i < 100; i++){
                canvas.drawLine(0, i*1 , 600, i*20, paint);
                canvas.drawLine(i*1 ,0, i*20, 600, paint);      
            }
}

回答1:


Within your view, you need to create the "OnTouchListener" which should look something like this:

myView.setOnTouchListener(new View.OnTouchListnener(){
    @Override
    public boolean onTouch(View v, MotionEvent e){
        switch(e.getAction()){
        case MotionEvent.ACTION_DOWN:
        //and code will go here for putting the finger on the screen

I would have a look at MotionEvent and looking at the various levels. You'll want to pay attention to how it can pack several bits of movement information into one MotionEvent.




回答2:


As you're already writing a custom view, instead of setting a listener, you might want to incorporate a GestureDetector and listener inside your view, and above all avoid the switch(e.getAction()) thing, because OnGestureListener is on a higher level and will provide you with event already detected as a gesture (scroll, fling, long press...).

See an example here.



来源:https://stackoverflow.com/questions/5135740/how-can-i-implement-ontouch-function-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!