I wanted to create a simple slideshow. I've gotten reasonably far, but now I'm stuck, I'm sure my issues sound very simple to you guys, hence why I'm here looking for help :-) I have made an effort, source code below.
Changes I'm stuck on
1. I want the images of the slideshow to fill the screen
2. I also don't want the user to have to drag the image across, I just want him to tap the image and it goes to the next one.
3. When the slideshow ends (reaches last item, a toast message tells user its finished)
I have tried to do the above, but like I said. I'm abit new at all this
ImagePager:
public class ImagePager extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImagePagerAdapter adapter = new ImagePagerAdapter(this, imageArra ); ViewPager myPager = (ViewPager) findViewById(R.id.myimagepager); myPager.setAdapter(adapter); myPager.setCurrentItem(0); } private int imageArra[] = { R.drawable.one, R.drawable.two, R.drawable.three, R.drawable.four, R.drawable.five,R.drawable.six, R.drawable.seven, R.drawable.eight, R.drawable.nine,R.drawable.ten,R.drawable.eleven, R.drawable.twelve, }; public class ImagePagerAdapter extends PagerAdapter { Activity activity; int imageArray[]; public ImagePagerAdapter(Activity act, int[] imgArra) { imageArray = imgArra; activity = act; } public int getCount() { return imageArray.length;} public Object instantiateItem(View collection, int position) { LayoutInflater inflater = (LayoutInflater)collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.custom_pager, null); ImageView im=(ImageView) layout.findViewById(R.id.myimage); im.setImageResource(imageArray[position]); ((ViewPager) collection).addView(layout, 0); return layout; } @Override public void destroyItem(View arg0, int arg1, Object arg2) { ((ViewPager) arg0).removeView((View) arg2); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == ((View) arg1); } @Override public Parcelable saveState() { return null; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.slide, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.action_settings: Intent p = new Intent("com.test.demo.SETTING"); startActivity(p); break; } return false; } }
Slide:
public class Slide extends Activity { public int currentimageindex=0; Timer timer; TimerTask task; ImageView slidingimage; private int[] IMAGE_IDS = { R.drawable.one, R.drawable.two, R.drawable.three, R.drawable.four, R.drawable.five, R.drawable.six, R.drawable.seven, R.drawable.eight, R.drawable.nine, R.drawable.ten, R.drawable.eleven, R.drawable.twelve, }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Handler mHandler = new Handler(); // Create runnable for posting final Runnable mUpdateResults = new Runnable() { public void run() { AnimateandSlideShow(); } }; int delay = 1000; // delay for 1 sec. int period = 8000; // repeat every 4 sec. Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { public void run() { mHandler.post(mUpdateResults); } }, delay, period); } private void AnimateandSlideShow() { SharedPreferences getPrefs = PreferenceManager .getDefaultSharedPreferences(getBaseContext()); boolean animation_one = getPrefs.getBoolean("animation_one", false); boolean animation_two = getPrefs.getBoolean("animation_two", false); boolean animation_three = getPrefs.getBoolean("animation_three", false); boolean animation_four = getPrefs.getBoolean("animation_four", false); boolean animation_five = getPrefs.getBoolean("animation_five", false); if (animation_one == true) { slidingimage = (ImageView)findViewById(R.id.slide_image); slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]); currentimageindex++; Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_in); slidingimage.startAnimation(rotateimage); }else if (animation_two == true) { slidingimage = (ImageView)findViewById(R.id.slide_image); slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]); currentimageindex++; Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_out); slidingimage.startAnimation(rotateimage); }else if (animation_three == true) { slidingimage = (ImageView)findViewById(R.id.slide_image); slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]); currentimageindex++; Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.bounce); slidingimage.startAnimation(rotateimage); }else if(animation_four == true) { slidingimage = (ImageView)findViewById(R.id.slide_image); slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]); currentimageindex++; Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.move); slidingimage.startAnimation(rotateimage); }else if (animation_five == true) { slidingimage = (ImageView)findViewById(R.id.slide_image); slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]); currentimageindex++; Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.slide_down); slidingimage.startAnimation(rotateimage); }else if(animation_one == false && animation_two == false && animation_three == false && animation_four == false && animation_five == false){ Intent intent = new Intent(Slide.this, ImagePager.class); startActivity(intent); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.slide, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.action_settings: Intent p = new Intent("com.test.demo.SETTING"); startActivity(p); break; } return false; } }
Setting:
public class Setting extends PreferenceActivity { CheckBoxPreference animation_one; CheckBoxPreference animation_two; CheckBoxPreference animation_three; CheckBoxPreference animation_four; CheckBoxPreference animation_five; @SuppressWarnings("deprecation") protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.setting); animation_one = (CheckBoxPreference) findPreference("animation_one"); animation_two = (CheckBoxPreference) findPreference("animation_two"); animation_three = (CheckBoxPreference) findPreference("animation_three"); animation_four = (CheckBoxPreference) findPreference("animation_four"); animation_five = (CheckBoxPreference) findPreference("animation_five"); animation_one.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { // TODO Auto-generated method stub animation_one.setChecked(true); animation_two.setChecked(false); animation_three.setChecked(false); animation_four.setChecked(false); animation_five.setChecked(false); return true; } }); animation_two.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { // TODO Auto-generated method stub animation_one.setChecked(false); animation_two.setChecked(true); animation_three.setChecked(false); animation_four.setChecked(false); animation_five.setChecked(false); return true; } }); ///// animation_three.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { // TODO Auto-generated method stub animation_one.setChecked(false); animation_two.setChecked(false); animation_three.setChecked(true); animation_four.setChecked(false); animation_five.setChecked(false); return true; } }); animation_four.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { // TODO Auto-generated method stub animation_one.setChecked(false); animation_two.setChecked(false); animation_three.setChecked(false); animation_four.setChecked(true); animation_five.setChecked(false); return true; } }); animation_five.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { // TODO Auto-generated method stub animation_one.setChecked(false); animation_two.setChecked(false); animation_three.setChecked(false); animation_four.setChecked(false); animation_five.setChecked(true); return true; } }); } }