gpt4 book ai didi

android - 从 GridView 调用的全屏图像浏览器

转载 作者:行者123 更新时间:2023-11-29 16:19:41 26 4
gpt4 key购买 nike

我有一个 GridView Activity ,它由一个 GridView 组成,最多可显示来自 SD 卡位置的 100 个图像缩略图。我正在创建另一个 Activity ,当触摸网格中的任何一个缩略图时调用该 Activity 。这个其他 Activity 应该全屏显示所选图像(可以通过将图像 uri 传递给新 Activity 来实现)。但是,我希望能够从所选图像的全屏 View 向左/向右滑动以转到下一个,而不是返回到 gridview Activity 并选择另一个图像进行全屏显示。

如何使用滑动手势实现从 sdcard 位置提取图像以进行全屏浏览的图像浏览器 Activity ?

最佳答案

你可以使用 ViewFlipper

public class ImageViewTest extends Activity {

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;

private Animation slideLeftIn;
private Animation slideLeftOut;
private Animation slideRightIn;
private Animation slideRightOut;
private ViewFlipper viewFlipper;

String message = "Initial Message";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//Set up viewflipper
viewFlipper = new ViewFlipper(this);
ImageView i = new ImageView(this);
i.setImageResource(R.drawable.sample_1);
ImageView i2 = new ImageView(this);
i2.setImageResource(R.drawable.sample_2);
viewFlipper.addView(i);
viewFlipper.addView(i2);

//set up animations
slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);

//put up a brownie as a starter
setContentView(viewFlipper);

gestureDetector = new GestureDetector(new MyGestureDetector());
}

public class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.v(LOGID,"right to left swipe detected");
viewFlipper.setInAnimation(slideLeftIn);
viewFlipper.setOutAnimation(slideLeftOut);
viewFlipper.showNext();
setContentView(viewFlipper);

} // left to right swipe
else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.v(LOGID,"left to right swipe detected");
viewFlipper.setInAnimation(slideRightIn);
viewFlipper.setOutAnimation(slideRightOut);
viewFlipper.showPrevious();
setContentView(viewFlipper);

}
} catch (Exception e) {
// nothing
}
return false;
}
}

@Override
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event))
return true;
else
return false;
}

}

关于android - 从 GridView 调用的全屏图像浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7882296/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com