我正在创建一个安卓应用程序。 这里我有一个 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" />
然后在您的 Activity 中初始化组件:
私有(private)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
}
}
});
希望对您有所帮助。让我知道进展如何:)
关于触摸事件上的Android Imageview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12086616/