为ImageView实现GestureDetector

时间:2013-11-20 10:57:54

标签: android imageview gesturedetector

良好的系泊:

我的Activity中有一个ImageView,我将OntOuchListner()设置为我的ImageView,这意味着我为我的ImageView实现了这样的界面:

public class mapActivity extends Activity
{

    //--------------------------------------
    private ImageView imageView;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.main);
        imageView = (ImageView) this.findViewById(R.id.imageView1);

        AddImageViewEvents();
    }
private void AddImageViewEvents() {


        imageView.setOnTouchListener(new ImageView.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                int action, pointerCount, i;
                action = event.getAction();
                pointerCount = event.getPointerCount();

                    switch (action) {
                        case MotionEvent.ACTION_DOWN:
                            //code here
                            break;

                        case MotionEvent.ACTION_MOVE:
                           //code here
                          break;

                        default:
                            break;
                    }

                return true;
            }

   }

我想以同样的方式将GestureDetector设置为我的ImageView,我不知道这是否可行。

感谢。

2 个答案:

答案 0 :(得分:3)

public class TestImageView extends ImageView  implements OnGestureListener{
    public TestImageView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub

        setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-ge`enter code here`nerated method stub
                return false;
            }
        });
    }

    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }
}

现在您可以在xml中使用TestImageView而不仅仅是ImageView。

答案 1 :(得分:1)

是的,你可以做到。让一个类扩展ImageView并实现OnGestureListener。