关于触摸事件的Android Imageview

use*_*951 5 android

我正在创建一个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;
}
Run Code Online (Sandbox Code Playgroud)

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

Ush*_*doo 13

首先,在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" />
Run Code Online (Sandbox Code Playgroud)

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

私人ImageView image1;

在onCreate:

this.image1= (ImageView)this.findViewById(R.id.imageview1);
this.image1.setImageResource(R.drawable.NAMEOFIMAGE);
Run Code Online (Sandbox Code Playgroud)

下一步是检测图像是否"被点击".然后,您可以使用在线数据库或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
            }
            }
        });
Run Code Online (Sandbox Code Playgroud)

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