简体   繁体   English

关于触摸事件的Android Imageview

[英]Android Imageview on touch event

I am creating a android application. 我正在创建一个Android应用程序。 Here I have a imageview . 我在这里有一个imageview。 my goal is to get next picture from database when move the finger on image 我的目标是在图像上移动手指时从数据库中获取下一张图片

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 图像应该像我们在图库中那样滑动

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 :) 让我知道事情的后续 :)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM