繁体   English   中英

关于触摸事件的Android Imageview

[英]Android Imageview on touch event

我正在创建一个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;
}

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

首先,在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;

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

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

您必须使用数据库中的图像数组,并在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