Android question - how to prep 100 images to be shown via Fling/Swipe?
- by fooyee
I'm totally new to this, been tinkering around for a week.
Came up with a simple image viewer app for 2 images.
Feature:
Left and right swipes will switch the images. Dead simple.
What i'd like to do:
Have up to 100 images.
note:
All my images are in my res/drawable folder.
They're named image1.png to image100.png
I obviously don't want to do:
ImageView i = new ImageView(this);
i.setImageResource(R.drawable.image1);
viewFlipper.addView(i);
ImageView i2 = new ImageView(this);
i2.setImageResource(R.drawable.image2);
viewFlipper.addView(i2);
ImageView i3 = new ImageView(this);
i3.setImageResource(R.drawable.image3);
viewFlipper.addView(i3);
all the way to i100.
how do I make this into a loop, which is flexible and reads everything from the drawable folder ( and not be limited to 100 images)?
source:
public class ImageViewTest extends Activity {
private static final String LOGID = "CHECKTHISOUT";
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;
}
}
// This doesn't work
@Override
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)){
Log.v(LOGID,"screen touched");
return true;
}
else{
return false;
}
}
}