触摸事件上的Android Imageview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12086616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:41:32  来源:igfitidea点击:

Android Imageview on touch event

android

提问by user1618951

I am creating a android application. Here I have a imageview . my goal is to get next picture from database when move the finger on image

我正在创建一个 android 应用程序。在这里,我有一个 imageview 。我的目标是在图像上移动手指时从数据库中获取下一张图片

float nPicturePositionM = 0;

public boolean onTouch(View v, MotionEvent event) {
    boolean aHandledL = false;
    if (event.getAction() == MotionEvent.ACTION_MOVE) {
        float nNewPicturePosL = event.getX();
        if (nPicturePositionM < nNewPicturePosL) {
            nPicturePositionM = nNewPicturePosL;
            GetPictures(true);
        } else {
            nPicturePositionM = nNewPicturePosL;
            GetPictures(false);
        }
        aHandledL = true;
    }

    return aHandledL;

    //return false;
}

How can i handle it using touch event? Image should slide as in we do it in gallery

我如何使用触摸事件处理它?图像应该像我们在画廊中那样滑动

回答by Ushal Naidoo

Firstly you declare an imageview in your xml :

首先,您在 xml 中声明一个 imageview :

<ImageView
    android:id="@+id/imageview1"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="45dp"
    android:layout_centerHorizontal="true" />

Then in your activity you initialize the component :

然后在您的活动中初始化组件:

private ImageView image1;

私有 ImageView image1;

In onCreate :

在 onCreate 中:

this.image1= (ImageView)this.findViewById(R.id.imageview1);
this.image1.setImageResource(R.drawable.NAMEOFIMAGE);

The next step is detecting if the image is "clicked". You can then use an online database or an SQLite database to save the images or the path to the images, the database part of it is a large part to teach from scratch depending which route you want to go with:

下一步是检测图像是否被“点击”。然后,您可以使用在线数据库或 SQLite 数据库来保存图像或图像的路径,其中的数据库部分是从头开始教的很大一部分,具体取决于您要使用的路线:

       this.image1.setOnClickListener(new View.OnClickListener() {        
        @Override
           public void onClick(View view) {
            if (view == findViewById(R.id.imageview1)) {     
                         //PUT IN CODE HERE TO GET NEXT IMAGE
            }
            }
        });

I hope this helps. Let me know how it goes :)

我希望这有帮助。让我知道事情的后续 :)

回答by Mehul Ranpara

You have to use the array of image from your database and set that on the basis of Touch...like..

您必须使用数据库中的图像数组,并在 Touch 的基础上进行设置...例如...

img.setOnTouchListener(new OnTouchListener() 
        {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) 
            {
                    img.setImageURI(uri[pos]);
                    pos++;
                    return false;
            }
        });