为 ImageView 实现 GestureDetector

implement GestureDetector for an ImageView(为 ImageView 实现 GestureDetector)
本文介绍了为 ImageView 实现 GestureDetector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早安:

我的 Activity 中有一个 ImageView,我将 OntOuchListner()设置到我的 ImageView,这意味着我为我的 ImageView 实现了该接口,如下所示:

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

   }

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

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

谢谢.

推荐答案

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.

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

这篇关于为 ImageView 实现 GestureDetector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)