ViewPager.PageTransformer Android -
i want following,
create grid of images using gridlayout inside scrollview inside viewpager, let's have pages of pictures, page 1 animals, page2 plants, etc. , want able scroll , down view images, of animals on page 1, , scroll right , left change page animation using pagetransformer. , need see pages on left , right described here. done there conflict in touch events can't resolve , need help.
now when use pager.setpagetransformer(true, new zoomoutpagetransformer());
lose the scrollview , down scroll , cool animation. , when don't use need except animation, how can them all?
this main activity,
public class mainactivity extends activity { pagercontainer mcontainer; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mcontainer = (pagercontainer) findviewbyid(r.id.pager_container); final viewpager pager = mcontainer.getviewpager(); pageradapter adapter = new mypageradapter(); pager.setadapter(adapter); //necessary or pager have 1 page show // make @ least many pages can see pager.setoffscreenpagelimit(adapter.getcount()); //a little space between pages pager.setpagemargin(20); //if hardware acceleration enabled, should remove // clipping on pager children. pager.setclipchildren(false); pager.setpagetransformer(true, new zoomoutpagetransformer()); } //nothing special adapter, throwing colored views demo private class mypageradapter extends pageradapter { @override public object instantiateitem(viewgroup container, int position) { linearlayout layout = new linearlayout(container.getcontext()); layout.setorientation(linearlayout.vertical); layout.setlayoutparams(new linearlayout.layoutparams(relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content)); scrollview sv=new scrollview(container.getcontext()); gridlayout gl = new gridlayout(container.getcontext()); gl.setlayoutparams(new viewgroup.layoutparams ( viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent)); gl.setorientation(gridlayout.horizontal); gl.setcolumncount(3); gl.setrowcount(3); for(int i=0;i<9;i++) { imageview imgview = new imageview(container.getcontext()); imgview.setimageresource(r.drawable.ic_launcher); gl.addview( imgview); } sv.setverticalscrollbarenabled(false); sv.addview(gl); container.addview(sv); return sv; } @override public void destroyitem(viewgroup container, int position, object object) { container.removeview((view)object); } @override public int getcount() { return 5; } @override public boolean isviewfromobject(view view, object object) { return (view == object); } } }
my page transformer this,
public class zoomoutpagetransformer implements viewpager.pagetransformer { private static final float min_scale = 0.85f; private static final float min_alpha = 0.5f; public void transformpage(view view, float position) { int pagewidth = view.getwidth(); int pageheight = view.getheight(); if (position < -1) { // [-infinity,-1) // page way off-screen left. view.setalpha(min_alpha); view.setscalex(min_scale); view.setscaley(min_scale); } else if (position <= 1) { // [-1,1] // modify default slide transition shrink page float scalefactor = math.max(min_scale, 1 - math.abs(position)); float vertmargin = pageheight * (1 - scalefactor) / 2; float horzmargin = pagewidth * (1 - scalefactor) / 2; if (position < 0) { view.settranslationx(horzmargin - vertmargin / 2); } else { view.settranslationx(-horzmargin + vertmargin / 2); } // scale page down (between min_scale , 1) view.setscalex(scalefactor); view.setscaley(scalefactor); // fade page relative size. view.setalpha(min_alpha + (scalefactor - min_scale) / (1 - min_scale) * (1 - min_alpha)); view.clearanimation(); } else { // (1,+infinity] // page way off-screen right. view.setalpha(min_alpha); view.setscalex(min_scale); view.setscaley(min_scale); } } }
and page container this,
public class pagercontainer extends framelayout implements viewpager.onpagechangelistener { private viewpager mpager; boolean mneedsredraw = false; public pagercontainer(context context) { super(context); init(); } public pagercontainer(context context, attributeset attrs) { super(context, attrs); init(); } public pagercontainer(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); init(); } private void init() { //disable clipping of children non-selected pages visible setclipchildren(false); //child clipping doesn't work hardware acceleration in android 3.x/4.x //you need set value here if using hardware acceleration in // application targeted @ these releases. setlayertype(view.layer_type_software, null); } @override protected void onfinishinflate() { try { mpager = (viewpager) getchildat(0); mpager.setonpagechangelistener(this); } catch (exception e) { throw new illegalstateexception("the root child of pagercontainer must viewpager"); } } public viewpager getviewpager() { return mpager; } private point mcenter = new point(); private point minitialtouch = new point(); @override protected void onsizechanged(int w, int h, int oldw, int oldh) { mcenter.x = w / 2; mcenter.y = h / 2; } @override public boolean ontouchevent(motionevent ev) { //we capture touches not handled viewpager // implement scrolling touch outside pager bounds. switch (ev.getaction()) { case motionevent.action_down: minitialtouch.x = (int)ev.getx(); minitialtouch.y = (int)ev.gety(); default: ev.offsetlocation(mcenter.x - minitialtouch.x, mcenter.y - minitialtouch.y); break; } return mpager.dispatchtouchevent(ev); } @override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { //force container redraw on scrolling. //without outer pages render , stay static if (mneedsredraw) invalidate(); } @override public void onpageselected(int position) { } @override public void onpagescrollstatechanged(int state) { mneedsredraw = (state != viewpager.scroll_state_idle); } }
i figured out after looking @ this, idea don't need use pager.setpagetransformer(true, new zoomoutpagetransformer());
, , copy stuff link mentioned earlier, , put zoomoutpagetransformer
function in container transformation inside it, final code below.
public class pagercontainer extends framelayout implements viewpager.onpagechangelistener { private viewpager mpager; boolean mneedsredraw = false; public pagercontainer(context context) { super(context); init(); } public pagercontainer(context context, attributeset attrs) { super(context, attrs); init(); } public pagercontainer(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); init(); } private void init() { //disable clipping of children non-selected pages visible setclipchildren(false); //child clipping doesn't work hardware acceleration in android 3.x/4.x //you need set value here if using hardware acceleration in // application targeted @ these releases. setlayertype(view.layer_type_software, null); } @override protected void onfinishinflate() { try { mpager = (viewpager) getchildat(0); mpager.setonpagechangelistener(this); } catch (exception e) { throw new illegalstateexception("the root child of pagercontainer must viewpager"); } } public viewpager getviewpager() { return mpager; } private point mcenter = new point(); private point minitialtouch = new point(); @override protected void onsizechanged(int w, int h, int oldw, int oldh) { mcenter.x = w / 2; mcenter.y = h / 2; } @override public boolean ontouchevent(motionevent ev) { //we capture touches not handled viewpager // implement scrolling touch outside pager bounds. switch (ev.getaction()) { case motionevent.action_down: minitialtouch.x = (int)ev.getx(); minitialtouch.y = (int)ev.gety(); default: ev.offsetlocation(mcenter.x - minitialtouch.x, mcenter.y - minitialtouch.y); break; } return mpager.dispatchtouchevent(ev); } @override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { //force container redraw on scrolling. //without outer pages render , stay static final int scrollx = mpager.getscrollx(); final int childcount = mpager.getchildcount(); (int = 0; < childcount; i++) { final view child = mpager.getchildat(i); final viewpager.layoutparams lp = (viewpager.layoutparams) child.getlayoutparams(); if (lp.isdecor) continue; final float transformpos = (float) (child.getleft() - scrollx) / child.getwidth(); transformpage(child, transformpos); } if (mneedsredraw) invalidate(); } @override public void onpageselected(int position) { // mpager. getchildat(3).setalpha(05f); } @override public void onpagescrollstatechanged(int state) { mneedsredraw = (state != viewpager.scroll_state_idle); } private static final float min_scale = 0.85f; private static final float min_alpha = 0.5f; public void transformpage(view view, float position) { int pagewidth = view.getwidth(); int pageheight = view.getheight(); if (position < -1) { // [-infinity,-1) // page way off-screen left. // view.setalpha(min_alpha); view.setscalex(min_scale); view.setscaley(min_scale); } else if (position <= 1) { // [-1,1] // modify default slide transition shrink page float scalefactor = math.max(min_scale, 1 - math.abs(position)); float vertmargin = pageheight * (1 - scalefactor) / 2; float horzmargin = pagewidth * (1 - scalefactor) / 2; if (position < 0) { view.settranslationx(horzmargin - vertmargin / 2); } else { view.settranslationx(-horzmargin + vertmargin / 2); } // scale page down (between min_scale , 1) view.setscalex(scalefactor); view.setscaley(scalefactor); // fade page relative size. // view.setalpha(min_alpha + (scalefactor - min_scale) / (1 - min_scale) * (1 - min_alpha)); view.clearanimation(); } else { // (1,+infinity] // page way off-screen right. // view.setalpha(min_alpha); view.setscalex(min_scale); view.setscaley(min_scale); } } }
use pagercontainer , mainactivity above , don't forger disable setpagetransformer on , that's it.
Comments
Post a Comment