java实现菜单滑动效果
程序员文章站
2024-03-04 08:50:05
菜单滑动效果的实现
public class menuscrolleractivity extends basegameactivity implements i...
菜单滑动效果的实现
public class menuscrolleractivity extends basegameactivity implements iscrolldetectorlistener, ionscenetouchlistener, iclickdetectorlistener { // =========================================================== // constants // =========================================================== protected static int camera_width = 480; protected static int camera_height = 320; protected static int font_size = 24; protected static int padding = 50; protected static int menuitems = 7; // =========================================================== // fields // =========================================================== private scene mscene; private camera mcamera; private font mfont; private bitmaptextureatlas mfonttexture; private bitmaptextureatlas mmenutextureatlas; private textureregion mmenulefttextureregion; private textureregion mmenurighttextureregion; private sprite menuleft; private sprite menuright; // scrolling private surfacescrolldetector mscrolldetector; private clickdetector mclickdetector; private float mminx = 0; private float mmaxx = 0; private float mcurrentx = 0; private int iitemclicked = -1; private rectangle scrollbar; private list<textureregion> columns = new arraylist<textureregion>(); // =========================================================== // constructors // =========================================================== // =========================================================== // getter & setter // =========================================================== // =========================================================== // methods for/from superclass/interfaces // =========================================================== @override public void onloadresources() { // paths fontfactory.setassetbasepath("font/"); bitmaptextureatlastextureregionfactory.setassetbasepath("gfx/"); // font this.mfonttexture = new bitmaptextureatlas(256, 256); this.mfont = fontfactory.createfromasset(this.mfonttexture, this, "plok.ttf", font_size, true, color.black); this.mengine.gettexturemanager().loadtextures(this.mfonttexture); this.mengine.getfontmanager().loadfonts(this.mfont); //images for the menu for (int i = 0; i < menuitems; i++) { bitmaptextureatlas mmenubitmaptextureatlas = new bitmaptextureatlas(256,256, textureoptions.bilinear_premultiplyalpha); textureregion mmenutextureregion = bitmaptextureatlastextureregionfactory.createfromasset(mmenubitmaptextureatlas, this, "menu"+i+".png", 0, 0); this.mengine.gettexturemanager().loadtexture(mmenubitmaptextureatlas); columns.add(mmenutextureregion); } //textures for menu arrows this.mmenutextureatlas = new bitmaptextureatlas(128,128, textureoptions.bilinear_premultiplyalpha); this.mmenulefttextureregion = bitmaptextureatlastextureregionfactory.createfromasset(mmenutextureatlas, this, "menu_left.png", 0, 0); this.mmenurighttextureregion = bitmaptextureatlastextureregionfactory.createfromasset(mmenutextureatlas, this, "menu_right.png",64, 0); this.mengine.gettexturemanager().loadtexture(mmenutextureatlas); } @override public engine onloadengine() { this.mcamera = new camera(0, 0, camera_width, camera_height); final engineoptions engineoptions = new engineoptions(true, screenorientation.landscape, new fillresolutionpolicy(), this.mcamera); engineoptions.gettouchoptions().setrunonupdatethread(true); final engine engine = new engine(engineoptions); return engine; } @override public scene onloadscene() { this.mengine.registerupdatehandler(new fpslogger()); this.mscene = new scene(); this.mscene.setbackground(new colorbackground(0, 0, 0)); this.mscrolldetector = new surfacescrolldetector(this); this.mclickdetector = new clickdetector(this); this.mscene.setonscenetouchlistener(this); this.mscene.settouchareabindingenabled(true); this.mscene.setonscenetouchlistenerbindingenabled(true); createmenuboxes(); return this.mscene; } @override public boolean onscenetouchevent(final scene pscene, final touchevent pscenetouchevent) { this.mclickdetector.ontouchevent(pscenetouchevent); this.mscrolldetector.ontouchevent(pscenetouchevent); return true; } @override public void onscroll(final scrolldetector pscolldetector, final touchevent ptouchevent, final float pdistancex, final float pdistancey) { //disable the menu arrows left and right (15px padding) if(mcamera.getminx()<=15) menuleft.setvisible(false); else menuleft.setvisible(true); if(mcamera.getminx()>mmaxx-15) menuright.setvisible(false); else menuright.setvisible(true); //return if ends are reached if ( ((mcurrentx - pdistancex) < mminx) ){ return; }else if((mcurrentx - pdistancex) > mmaxx){ return; } //center camera to the current point this.mcamera.offsetcenter(-pdistancex,0 ); mcurrentx -= pdistancex; //set the scrollbar with the camera float tempx =mcamera.getcenterx()-camera_width/2; // add the % part to the position tempx+= (tempx/(mmaxx+camera_width))*camera_width; //set the position scrollbar.setposition(tempx, scrollbar.gety()); //set the arrows for left and right menuright.setposition(mcamera.getcenterx()+camera_width/2-menuright.getwidth(),menuright.gety()); menuleft.setposition(mcamera.getcenterx()-camera_width/2,menuleft.gety()); //because camera can have negativ x values, so set to 0 if(this.mcamera.getminx()<0){ this.mcamera.offsetcenter(0,0 ); mcurrentx=0; } } @override public void onclick(clickdetector pclickdetector, touchevent ptouchevent) { loadlevel(iitemclicked); }; // =========================================================== // methods // =========================================================== private void createmenuboxes() { int spritex = padding; int spritey = padding; //current item counter int iitem = 1; for (int x = 0; x < columns.size(); x++) { //on touch, save the clicked item in case it's a click and not a scroll. final int itemtoload = iitem; sprite sprite = new sprite(spritex,spritey,columns.get(x)){ public boolean onareatouched(final touchevent pscenetouchevent, final float ptoucharealocalx, final float ptoucharealocaly) { iitemclicked = itemtoload; return false; } }; iitem++; this.mscene.attachchild(sprite); this.mscene.registertoucharea(sprite); spritex += 20 + padding+sprite.getwidth(); } mmaxx = spritex - camera_width; //set the size of the scrollbar float scrollbarsize = camera_width/((mmaxx+camera_width)/camera_width); scrollbar = new rectangle(0,camera_height-20,scrollbarsize, 20); scrollbar.setcolor(1,0,0); this.mscene.attachchild(scrollbar); menuleft = new sprite(0,camera_height/2-mmenulefttextureregion.getheight()/2,mmenulefttextureregion); menuright = new sprite(camera_width-mmenurighttextureregion.getwidth(),camera_height/2-mmenurighttextureregion.getheight()/2,mmenurighttextureregion); this.mscene.attachchild(menuright); menuleft.setvisible(false); this.mscene.attachchild(menuleft); } @override public void onloadcomplete() { } //here is where you call the item load. private void loadlevel(final int ilevel) { if (ilevel != -1) { runonuithread(new runnable() { @override public void run() { toast.maketext(menuscrolleractivity.this, "load item" + string.valueof(ilevel), toast.length_short).show(); iitemclicked = -1; } }); } } }
以上所述就是本文的全部内容了,希望大家能够喜欢。
下一篇: 深入浅出分析Python装饰器用法