implement GestureDetector for an ImageView

旧巷老猫 提交于 2019-11-30 18:34:02

问题


Good moorning :

I have an ImageView in my Activity and I setted the OntOuchListner() to my ImageView, which means that I implemented that interface for my ImageView like this :

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;
            }

   }

I want by the same way set the GestureDetector to my ImageView, I don't know if this is possible.

Thanks.


回答1:


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;
    }
}

Now you can use TestImageView in your xml instead of just ImageView.




回答2:


Yes you can do it. Have a class extends ImageView and implements OnGestureListener.



来源:https://stackoverflow.com/questions/20094017/implement-gesturedetector-for-an-imageview

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