我想为gridview图像创建fling(滑动)动作。我使用此图像enter image description here实现了网格视图图像,当我点击网格视图图像时,它将转到全屏图像。现在显示完整图像然后用手指触摸从左到右滑动图像。这里不使用视图鳍状肢,因为这里有更多图像。点击哪个图像显示并向右或向左滑动。感谢
android image gridview swipe
答案 0 :(得分:1)
试试此代码
public class MainActivity extends Activity {
private Integer[] mImageIds = { R.drawable.img1, R.drawable.img2,R.drawable.img3 };
private static final String DEBUG_TAG = "MainActivity ";
ImageView imageView;
float startXValue = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.gallery);
imageView = (ImageView) findViewById(R.id.image_place_holder);
imageView.setImageResource(mImageIds[num]);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float endXValue = 0;
float x1 = event.getAxisValue(MotionEvent.AXIS_X);
int action = MotionEventCompat.getActionMasked(event);
switch (action) {
case (MotionEvent.ACTION_DOWN):
startXValue = event.getAxisValue(MotionEvent.AXIS_X);
return true;
case (MotionEvent.ACTION_UP):
endXValue = event.getAxisValue(MotionEvent.AXIS_X);
if (endXValue > startXValue) {
if (endXValue - startXValue > 100) {
System.out.println("Left-Right");
imageView.setImageResource(mImageIds[2]);
}
}else {
if (startXValue -endXValue> 100) {
System.out.println("Right-Left");
imageView.setImageResource(mImageIds[0]);
}
}
return true;
default:
return super.onTouchEvent(event);
}
}
}