java - Flip Images in a viewpager -
i have viewpager fullscreen image slider horizontal swipe. trying put image behind every image on slider. meaning, user can click on 'flip' button reveal image.
below code have 2 issues:
- the first time click flip button, flips image reveal same image. however, when click second time works :).
- the flip button works on first image. when swipe right or left flip button doesn't work anymore. help?
note: using fullscreenactivity theme hides ui control until user clicks on image.
fullscreenactivity.java
package me.zamaaan.wallpaper; import android.support.v4.view.pageradapter; import android.support.v4.view.viewpager; import me.zamaaan.wallpaper.util.systemuihider; import android.annotation.targetapi; import android.app.activity; import android.content.intent; import android.os.build; import android.os.bundle; import android.os.handler; import android.os.message; import android.view.motionevent; import android.view.view; import android.widget.imageview; import android.widget.toast; import static me.zamaaan.wallpaper.heavylifter.fail; import static me.zamaaan.wallpaper.heavylifter.success; import android.view.view.onclicklistener; import android.view.animation.animation; import android.view.animation.animation.animationlistener; import android.view.animation.animationutils; import java.util.arraylist; import java.util.list; /** helper class heavy work of decoding images , setting wallpaper */ /** * example full-screen activity shows , hides system ui (i.e. * status bar , navigation/system bar) user interaction. * * @see systemuihider */ public class fullscreenactivity extends activity implements onclicklistener, animationlistener{ /** * whether or not system ui should auto-hidden after * {@link #auto_hide_delay_millis} milliseconds. */ private static final boolean auto_hide = true; private heavylifter chucknorris; private animation animation1; private animation animation2; private boolean isbackofcardshowing = true; /* array ids of images viewpager */ public integer[] mimageids = { r.drawable.background1, r.drawable.background2, r.drawable.background3, r.drawable.background4, r.drawable.background1, r.drawable.background2, r.drawable.background1, r.drawable.background2, r.drawable.background3, r.drawable.background4, r.drawable.background1, r.drawable.background2, r.drawable.background3 }; /* array ids of images flip of each image in viewpager */ public integer[] mbackids = { r.drawable.splash, r.drawable.splash, r.drawable.splash, r.drawable.splash, r.drawable.splash, r.drawable.splash, r.drawable.splash, r.drawable.splash, r.drawable.splash, r.drawable.splash, r.drawable.splash, r.drawable.splash, r.drawable.splash }; /** * if {@link #auto_hide} set, number of milliseconds wait after * user interaction before hiding system ui. */ private static final int auto_hide_delay_millis = 3000; /** * if set, toggle system ui visibility upon interaction. otherwise, * show system ui visibility upon interaction. */ private static final boolean toggle_on_click = true; /** * flags pass {@link systemuihider#getinstance}. */ private static final int hider_flags = systemuihider.flag_hide_navigation; /** * instance of {@link systemuihider} activity. */ private systemuihider msystemuihider; /** * pager widget, handles animation , allows swiping horizontally access previous * , next wizard steps. */ private viewpager mpager; /** * pager adapter, provides pages view pager widget. */ private pageradapter mpageradapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_fullscreen); animation1 = animationutils.loadanimation(this, r.anim.to_middle); animation1.setanimationlistener(this); animation2 = animationutils.loadanimation(this, r.anim.from_middle); animation2.setanimationlistener(this); findviewbyid(r.id.flip).setonclicklistener(this); // loop through ids create list of full screen image views imageadapter imageadapter = new imageadapter(this); list<imageview> images = new arraylist<imageview>(); (int = 0; < imageadapter.getcount(); i++) { imageview imageview = new imageview(this); imageview.setid(i); imageview.setimageresource(imageadapter.mthumbids[i]); imageview.setscaletype(imageview.scaletype.fit_xy); images.add(imageview); } final view controlsview = findviewbyid(r.id.fullscreen_content_controls); final view contentview = findviewbyid(r.id.view_pager); // set instance of systemuihider control system ui // activity. msystemuihider = systemuihider.getinstance(this, contentview, hider_flags); msystemuihider.setup(); msystemuihider.setonvisibilitychangelistener(new systemuihider.onvisibilitychangelistener() { // cached values. int mcontrolsheight; int mshortanimtime; @override @targetapi(build.version_codes.honeycomb_mr2) public void onvisibilitychange(boolean visible) { if (build.version.sdk_int >= build.version_codes.honeycomb_mr2) { // if viewpropertyanimator api available // (honeycomb mr2 , later), use animate // in-layout ui controls @ bottom of // screen. if (mcontrolsheight == 0) { mcontrolsheight = controlsview.getheight(); } if (mshortanimtime == 0) { mshortanimtime = getresources().getinteger( android.r.integer.config_shortanimtime); } controlsview .animate() .translationy(visible ? 0 : mcontrolsheight) .setduration(mshortanimtime); } else { // if viewpropertyanimator apis aren't // available, show or hide in-layout ui // controls. controlsview.setvisibility(visible ? view.visible : view.gone); } if (visible && auto_hide) { // schedule hide(). delayedhide(auto_hide_delay_millis); } } }); // set user interaction manually show or hide system ui. contentview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { if (toggle_on_click) { msystemuihider.toggle(); } else { msystemuihider.show(); } } }); // upon interacting ui controls, delay scheduled hide() // operations prevent jarring behavior of controls going away // while interacting ui. findviewbyid(r.id.btnsetaswallpaper).setontouchlistener( mdelayhidetouchlistener); findviewbyid(r.id.btnsavewallpaper).setontouchlistener( mdelayhidetouchlistener); // create adapter imagepageradapter imagepageradapter = new imagepageradapter(images); viewpager viewpager = (viewpager) findviewbyid(r.id.view_pager); viewpager.setadapter(imagepageradapter); // set viewpager point selected image previous activity // selected image id int position = getintent().getextras().getint("id"); viewpager.setcurrentitem(position); //set title of image this.settitle(imageadapter.mthumbtitles[position]); // load heavy lifter (goes , work on thread), response after lifters thread // has finished pass in handler notified when completes chucknorris = new heavylifter(this, chuckfinishedhandler); } @override public void onclick(view v) { v.setenabled(false); ((imageview)findviewbyid(getintent().getextras().getint("id"))).clearanimation(); ((imageview)findviewbyid(getintent().getextras().getint("id"))).setanimation(animation1); ((imageview)findviewbyid(getintent().getextras().getint("id"))).startanimation(animation1); } @override public void onanimationend(animation animation) { int = getintent().getextras().getint("id"); if (animation==animation1) { if (isbackofcardshowing) { ((imageview)findviewbyid(i)).setimageresource(mimageids[i]); } else { ((imageview)findviewbyid(i)).setimageresource(mbackids[i]); } ((imageview)findviewbyid(getintent().getextras().getint("id"))).clearanimation(); ((imageview)findviewbyid(getintent().getextras().getint("id"))).setanimation(animation2); ((imageview)findviewbyid(getintent().getextras().getint("id"))).startanimation(animation2); } else { isbackofcardshowing=!isbackofcardshowing; findviewbyid(r.id.flip).setenabled(true); } } @override public void onanimationrepeat(animation animation) { // todo auto-generated method stub } @override public void onanimationstart(animation animation) { // todo auto-generated method stub } @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); // trigger initial hide() shortly after activity has been // created, briefly hint user ui controls // available. delayedhide(100); } /** * touch listener use in-layout ui controls delay hiding * system ui. prevent jarring behavior of controls going away * while interacting activity ui. */ view.ontouchlistener mdelayhidetouchlistener = new view.ontouchlistener() { @override public boolean ontouch(view view, motionevent motionevent) { if (auto_hide) { delayedhide(auto_hide_delay_millis); } return false; } }; handler mhidehandler = new handler(); runnable mhiderunnable = new runnable() { @override public void run() { msystemuihider.hide(); } }; /** * schedules call hide() in [delay] milliseconds, canceling * scheduled calls. */ private void delayedhide(int delaymillis) { mhidehandler.removecallbacks(mhiderunnable); mhidehandler.postdelayed(mhiderunnable, delaymillis); } /** * called xml when save wallpaper button pressed * thie retrieves id of current image our list * asks chuck save wallpaper! * chuckhandler called when operation complete * @param v */ public void savewallpaper(view v) { // intent data intent = getintent(); // selected image id int position = i.getextras().getint("id"); imageadapter imageadapter = new imageadapter(this); // show toast message on successful save if(chucknorris.saveresourceaswallpaper(imageadapter.mthumbids[position])){ toast.maketext(fullscreenactivity.this, "image saved zamaaanwallpaper folder", toast.length_short).show(); } else{ toast.maketext(fullscreenactivity.this, "image couldn't saved, try again", toast.length_short).show(); } } /** * called xml when set wallpaper button pressed * thie retrieves id of current image our list * asks chuck set wallpaper! * chuckhandler called when operation complete * @param v */ public void setaswallpaper(view v) { // intent data intent = getintent(); // selected image id int position = i.getextras().getint("id"); imageadapter imageadapter = new imageadapter(this); chucknorris.setresourceaswallpaper(imageadapter.mthumbids[position]); } /** * handler notified when heavylifter finished doing operation */ private handler chuckfinishedhandler = new handler(){ @override public void handlemessage(message msg) { switch(msg.what){ case success: toast.maketext(fullscreenactivity.this, "wallpaper set", toast.length_short).show(); break; case fail: toast.maketext(fullscreenactivity.this, "wallper not set, try again", toast.length_short).show(); break; default: super.handlemessage(msg); } } }; }
i fixed issues. had :
- change
boolean isbackofcardshowing
array of boolean each page , intialize false. - use
viewpager.getcurrentitem()
position of current page instead of doing.
Comments
Post a Comment