java实现菜单滑动效果

 更新时间:2015年03月30日 16:30:19   转载 投稿:hebedich  
这篇文章主要介绍了java实现菜单滑动效果,效果非常棒,这里推荐给大家,有需要的小伙伴可以参考下。

菜单滑动效果的实现

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;
                }
            });
        }
    }
}

以上所述就是本文的全部内容了,希望大家能够喜欢。

相关文章

  • Java实现银行ATM系统

    Java实现银行ATM系统

    这篇文章主要为大家详细介绍了Java实现银行ATM系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • java实现简单的给sql语句赋值的示例

    java实现简单的给sql语句赋值的示例

    这篇文章主要介绍了java实现简单的给sql语句赋值的示例,需要的朋友可以参考下
    2014-05-05
  • 使用ElasticSearch6.0快速实现全文搜索功能的示例代码

    使用ElasticSearch6.0快速实现全文搜索功能的示例代码

    本篇文章主要介绍了使用ElasticSearch6.0快速实现全文搜索功能,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • Spring batch批处理框架

    Spring batch批处理框架

    本文主要介绍了Spring batch批处理框架的相关知识。具有很好的参考价值,下面跟着小编一起来看下吧
    2017-04-04
  • Java Calendar日历与Date日期的相互转换详解

    Java Calendar日历与Date日期的相互转换详解

    这篇文章主要介绍了Java Calendar日历与Date日期的相互转换详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • hibernate4快速入门实例详解

    hibernate4快速入门实例详解

    Hibernate是一个轻量级的ORMapping框架,本文重点给大家介绍hibernate4 入门实例详细,需要的朋友参考下吧
    2017-09-09
  • 一篇文章弄懂Spring MVC的参数绑定

    一篇文章弄懂Spring MVC的参数绑定

    这篇文章主要给大家介绍了关于如何通过一篇文章弄懂Spring MVC的参数绑定,文中通过示例代码以及图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-02-02
  • spring+springmvc整合mabytis时mapper注入失败问题解决方法

    spring+springmvc整合mabytis时mapper注入失败问题解决方法

    这篇文章主要介绍了spring+springmvc整合mabytis时mapper注入失败问题解决方法 ,需要的朋友可以参考下
    2017-08-08
  • Java分布式锁的三种实现方案

    Java分布式锁的三种实现方案

    本文主要介绍了Java分布式锁的三种实现方案。具有一定的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • java打印当前方法名示例分享

    java打印当前方法名示例分享

    在C与C++中可以打印当前函数名,但在Java没有此说法,一切即对象,得从某个对象中去获取,下面介绍两种方式打印当前方法名
    2014-02-02

最新评论