关于触摸事件的Android Imageview

时间:2012-08-23 07:20:00

标签: 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;
}

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

2 个答案:

答案 0 :(得分:11)

首先在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" />

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

私人ImageView image1;

on onCreate:

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

下一步是检测图像是否“被点击”。然后,您可以使用在线数据库或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
            }
            }
        });

我希望这会有所帮助。让我知道它是怎么回事:))

答案 1 :(得分:0)

您必须使用数据库中的图像数组,并根据Touch ...设置它...

img.setOnTouchListener(new OnTouchListener() 
        {

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