2017-07-31 173 views
0

我已经拍摄的图像按钮用下面的代码:如何设置图像的图像按钮

skin = new Skin(Gdx.files.internal("glassy-ui.json")); 
     button2 = new ImageButton(skin); 
     myTexture = new Texture(Gdx.files.internal("PlayButton.png")); 
     myTextureRegion = new TextureRegion(myTexture); 
     myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion); 
     button2.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("PlayButton.png")))); 
     button2.addListener(new InputListener(){ 
      @Override 
      public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { 
       game.dispose(); 
       game.setScreen(new GameScreen(game)); 
       return true; 
      } 
     }); 
     stage.addActor(button2); 

我想在ImageButton的设置图像绘制,请参阅本link做进一步的了解。我怎样才能做到这一点?我还想要做的是对用户输入有任何可见的反应,如改变其大小等。

顺便说一下,图像是圆的。

回答

1
ImageButtonStyle imgBtnStyle=new ImageButtonStyle(); 
     imgBtnStyle.imageUp=new SpriteDrawable(spriteUp); 
     imgBtnStyle.imageDown=new SpriteDrawable(spriteDown); 
     ImageButton imgBtn=new ImageButton(imgBtnStyle); 

添加图像按钮阶段。在ImageButton Actor上做任何你想做的动作(动作)。

0

你可以像的ImageButton添加图像,

那就试试这个

Drawable img = getContext().getResources().getDrawable(R.drawable.smiley); 
yourimagebutton.setCompoundDrawablesWithIntrinsicBounds(img, null, null, null); 
+0

这是在xml中,不是吗?但我想在我的MenuScreen类中完成它。 – user8340536

+0

检查我的更新回答 –

+1

问题是针对libgdx – Lestat

0

为了填补按钮图像,设置缩放类型FitXY。

button2.setScaleType(ImageView.ScaleType.FIT_XY);

或为了把图像中的按钮的中心,设置秤的类型FitCenter。

button2.setScaleType(ImageView.ScaleType.FIT_CENTER);