android - slideshow with many animation issue -
i have image slideshow app im trying let user choose many animation in setting activity used sharedpreferences achieve , when run app open show first image , exit app without force close , im new android development , , think im missing in code ,
so please fix appreciated,thanks.
mainactivity.java
public class mainactivity 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, }; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final handler mhandler = new handler(); // create runnable posting final runnable mupdateresults = new runnable() { public void run() { animateandslideshow(); } }; int delay = 1000; // delay 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); } public void onclick(view v) { finish(); android.os.process.killprocess(android.os.process.mypid()); } private void animateandslideshow() { sharedpreferences getprefs = preferencemanager .getdefaultsharedpreferences(getbasecontext()); boolean animation_two = getprefs.getboolean("animation_two", true); boolean animation = getprefs.getboolean("animation", false); boolean animation_one = getprefs.getboolean("animation_one", false); if (animation_two == true) { slidingimage = (imageview)findviewbyid(r.id.imageview3_left); slidingimage.setimageresource(image_ids[currentimageindex%image_ids.length]); currentimageindex++; animation rotateimage = animationutils.loadanimation(this, r.anim.custom_anim); slidingimage.startanimation(rotateimage); finish(); }else if(animation_one == true) { slidingimage = (imageview)findviewbyid(r.id.imageview3_left); slidingimage.setimageresource(image_ids[currentimageindex%image_ids.length]); currentimageindex++; animation rotateimage = animationutils.loadanimation(this, r.anim.fade_in); slidingimage.startanimation(rotateimage); finish(); }else if (animation == true) { slidingimage = (imageview)findviewbyid(r.id.imageview3_left); slidingimage.setimageresource(image_ids[currentimageindex%image_ids.length]); currentimageindex++; animation rotateimage = animationutils.loadanimation(this, r.anim.fade_in2); slidingimage.startanimation(rotateimage); finish(); } else if(animation_two == false && animation == false && animation_one == false){ slidingimage = (imageview)findviewbyid(r.id.imageview3_left); slidingimage.setimageresource(image_ids[currentimageindex%image_ids.length]); currentimageindex++; animation rotateimage = animationutils.loadanimation(this, r.anim.fade_in2); slidingimage.startanimation(rotateimage); finish(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, 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; } }
rearrange code , work fine below:
public class mainactivity 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, }; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final handler mhandler = new handler(); // create runnable posting final runnable mupdateresults = new runnable() { public void run() { animateandslideshow(); } }; int delay = 1000; // delay 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_two = getprefs.getboolean("animation_two", true); boolean animation = getprefs.getboolean("animation", false); boolean animation_one = getprefs.getboolean("animation_one", false); if (animation == true) { slidingimage = (imageview)findviewbyid(r.id.imageview_slide); slidingimage.setimageresource(image_ids[currentimageindex%image_ids.length]); currentimageindex++; animation rotateimage = animationutils.loadanimation(this, r.anim.custom_anim); slidingimage.startanimation(rotateimage); }else if(animation_one == true) { slidingimage = (imageview)findviewbyid(r.id.imageview_slide); 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.imageview_slide); slidingimage.setimageresource(image_ids[currentimageindex%image_ids.length]); currentimageindex++; animation rotateimage = animationutils.loadanimation(this, r.anim.fade_in2); slidingimage.startanimation(rotateimage); }else if(animation == false && animation_one == false && animation_two == false){ slidingimage = (imageview)findviewbyid(r.id.imageview_slide); slidingimage.setimageresource(image_ids[currentimageindex%image_ids.length]); currentimageindex++; animation rotateimage = animationutils.loadanimation(this, r.anim.fade_in2); slidingimage.startanimation(rotateimage); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, 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; } } hope
Comments
Post a Comment