首页 > 代码库 > libgdx学习记录23——图片移动选择

libgdx学习记录23——图片移动选择

模拟移动选择图片,采用相机实现。

  1 package com.fxb.newtest;  2   3 import com.badlogic.gdx.ApplicationAdapter;  4 import com.badlogic.gdx.Gdx;  5 import com.badlogic.gdx.graphics.Color;  6 import com.badlogic.gdx.graphics.GL10;  7 import com.badlogic.gdx.graphics.Texture;  8 import com.badlogic.gdx.graphics.glutils.ShapeRenderer;  9 import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; 10 import com.badlogic.gdx.input.GestureDetector; 11 import com.badlogic.gdx.input.GestureDetector.GestureAdapter; 12 import com.badlogic.gdx.scenes.scene2d.Action; 13 import com.badlogic.gdx.scenes.scene2d.Stage; 14 import com.badlogic.gdx.scenes.scene2d.actions.Actions; 15 import com.badlogic.gdx.scenes.scene2d.ui.Image; 16  17 public class Lib025_PicChange extends ApplicationAdapter{ 18      19     GestureAdapter gestureAdapter = new GestureAdapter(){ 20         @Override 21         public boolean fling(float velocityX, float velocityY, int button) { 22             // TODO Auto-generated method stub 23 /*            if( velocityX > 0 ){ 24                 System.out.println( "fling right" ); 25                 stage.getCamera().translate( -stage.getWidth(), 0, 0 ); 26             } 27             else{ 28                 System.out.println( "fling left" ); 29                 stage.getCamera().translate( stage.getWidth(), 0, 0 ); 30             }*/ 31              32             return super.fling(velocityX, velocityY, button); 33         } 34  35          36          37         @Override 38         public boolean pan(float x, float y, float deltaX, float deltaY) { 39             // TODO Auto-generated method stub 40             System.out.println( "pan" ); 41             if( index>0 && deltaX>0 || index<imgs.length-1 && deltaX<0 ){ 42                 stage.getCamera().translate( -deltaX, 0, 0 ); 43                 add = deltaX > 0? -1: 1;  44             }             45              46             return super.pan(x, y, deltaX, deltaY); 47         } 48  49  50  51         @Override 52         public boolean panStop(float x, float y, int pointer, int button) { 53             // TODO Auto-generated method stub 54             System.out.println( "pan stop" ); 55             if( index>0 && add==-1 || index<imgs.length-1 && add==1 ){ 56                 index += add; 57                 stage.getCamera().position.set( index*500+stage.getWidth()/2, stage.getHeight()/2, 0 );                 58             } 59             return super.panStop(x, y, pointer, button); 60         }     61          62          63          64          65     }; 66     GestureDetector detector = new GestureDetector( gestureAdapter ); 67      68     Stage stage; 69     Image img1, img2, img3, img4; 70     Image[] imgs; 71     int index; 72     int add = 0; 73     ShapeRenderer rend; 74      75     @Override 76     public void create() { 77         // TODO Auto-generated method stub 78         super.create(); 79         Gdx.input.setInputProcessor( detector ); 80          81         img1 = new Image( new Texture( Gdx.files.internal( "data/pal4_0.jpg" ) ) ); 82         img2 = new Image( new Texture( Gdx.files.internal( "data/pal4_1.jpg" ) ) ); 83         img3 = new Image( new Texture( Gdx.files.internal( "data/pal4_2.jpg" ) ) ); 84         img4 = new Image( new Texture( Gdx.files.internal( "data/pal4_3.jpg" ) ) ); 85          86         stage = new Stage(); 87         stage.addActor( img1 ); 88         stage.addActor( img2 ); 89         stage.addActor( img3 ); 90         stage.addActor( img4 ); 91          92         imgs = new Image[]{ img1, img2, img3, img4 }; 93          94         for( int i=0; i<imgs.length; ++i ){ 95             imgs[i].setSize( 400, 240 ); 96             imgs[i].setPosition( i*500 + stage.getWidth()/2-imgs[i].getWidth()/2, stage.getHeight()/2-imgs[i].getHeight()/2 ); 97         } 98              99         //imgs[1].setVisible( false );100         //imgs[2].setVisible( false );101         index = 0;102         rend = new ShapeRenderer();103     }104 105     @Override106     public void render() {107         // TODO Auto-generated method stub108         super.render();109         Gdx.gl.glClearColor( 1, 1, 1, 1 );110         Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT );111         stage.act();112         stage.draw();113         114         rend.begin( ShapeType.Filled );115         rend.setColor( Color.LIGHT_GRAY );116         rend.rect( 0, 0, 100, 480 );117         rend.rect( 700, 0, 100, 480 );118         rend.rect( 100, 0, 700, 60 );119         rend.rect( 100, 420, 700, 60 );120         rend.end();121         122     }123 124     @Override125     public void dispose() {126         // TODO Auto-generated method stub127         rend.dispose();128         stage.dispose();129         super.dispose();130     }131 132 }

运行结果: