Why NinePatch dont work in LibGDX? - android

I have a backround for message item, I want to make it stretched depending on the text. But when i make this:
Drawable drawableButtonMessageUp = new TextureRegionDrawable(new TextureRegion(new Texture("data/message_my_g.9.png")));
ButtonStyle buttonStyleMessage = new ButtonStyle();
buttonStyleMessage.up = drawableButtonMessageUp;
Button buttonMessage = new Button(buttonStyleMessage);
Label labelMessage = new Label("", new LabelStyle(game.fonts[0], new Color(0, 0, 0, 1)));
buttonMessage.add(labelMessage);
Background does not NinePath, it strached like a normal picture:
My result:
I want (example):
Backround for item:
I checked the nine patch picture in Android SDK, and this work good, how i can do this in libGDX?

It's because you're not using a NinePatch, you're using a TextureRegion
you probably are going to want to change the line
Drawable drawableButtonMessageUp = new TextureRegionDrawable(new TextureRegion(new Texture("data/message_my_g.9.png")));
to something like
Drawable drawableButtonMessageUp = new NinePatchDrawable(new NinePatch(new Texture("data/message_my_g.9.png"), 1, 1, 1, 1));
Note that the number "1" here is a placeholder. You may need to play around with those numbers to get the ninepatch to look right.
For more information, please take a look at the documentation, particularly the section on instantiating ninepatches

Related

Android OpenGL ES image filters

I found this example and I am making some filters on realtime camera. When I use GlassSphereFilter or SphereRefractionFilter I get sphere on black background, and I want to achieve something like this. Can anyone help me?
Here is part of the code:
view = new FastImageProcessingView(this);
pipeline = new FastImageProcessingPipeline();
view.setPipeline(pipeline);
setContentView(view);
input = new CameraPreviewInput(view);
filter = new SphereRefractionFilter(new PointF(0.43f, 0.5f), 0.25f, 0.71f, 0.5f);
screen = new ScreenEndpoint(pipeline);
input.addTarget(screen);
filter.addTarget(screen);
pipeline.addRootRenderer(input);
pipeline.startRendering();
Is there a way ti show two surfaceViews with camera in same time? One to show normal image and other to show sphere?
I think you should use this library :
https://github.com/CyberAgent/android-gpuimage

AnimatedSprite displays with wrong size

Sorry if this problem was asked before. I have searched around but I didn't find any thread about this, So I post the question here.
I am really new on Andengine. I am trying to load a Tiled Sprite and create an animation with it.
Here are my codes:
public void loadGameResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/player/");
mSapoTextureAtlas = new BitmapTextureAtlas(mActivity.getTextureManager(),256,178,TextureOptions.DEFAULT);
mPlayerDownITiledTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mPlayerTextureAtlas, mActivity.getAssets(), "player.png", 0,0,3,1);
mPlayerTextureAtlas.load();
}
What I expect is that the player can do some actions like walking but I don't. Please see the attached screenshots to see the real result. I think that my codes split the original texture into 3 parts rather than just split 3 sprites at first row.
Please take a look and help me to fix this. Thanks a lot!
And here is how I create the animation:
AnimatedSprite player= new AnimatedSprite(100,100,40,40,mResourceManager.mPlayerDownITiledTextureRegion,mVertexBufferObjectManager);
player.animate(500);
player.setZIndex(100);
attachChild(sapo);
What I understood is that you want to animate the player in each direction properly. For that
According to library method
BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(pBitmapTextureAtlas, pAssetManager, pAssetPath, pTextureX, pTextureY, pTileColumns, pTileRows);
Your code will change as below
mSapoTextureAtlas = new BitmapTextureAtlas(mActivity.getTextureManager(),256,256,TextureOptions.DEFAULT);
mPlayerDownITiledTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mPlayerTextureAtlas, mActivity.getAssets(), "player.png", 0,0,3,4);
To animate player in different directions use this technique
define an array as
long[] ANIMATE_DURATION = new long[] { 200, 200, 200 };
AnimatedSprite player = new AnimatedSprite(x, y, this.mPlayerTextureRegion,this.getVertexBufferObjectManager());
// Down
player.animate(ANIMATE_DURATION, 0, 2, true);
// Up
player.animate(ANIMATE_DURATION, 9, 11, true);
// Right
player.animate(ANIMATE_DURATION, 6, 8, true);
// Left
player.animate(ANIMATE_DURATION, 3, 5, true);
look this Example for more information.
If you have doubt ask me. Hope this helped!
First: Atlas should be in power of two so change it size to 256x256.
Second:
mPlayerDownITiledTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mPlayerTextureAtlas, mActivity.getAssets(), "player.png", 0,0,3,1);
Last two digits say how many rows and columns you have. You declared 3 columns and 1 row.
Three: Zindex at 100 is needed only if you have so many layers. If not you dont need it.

Sprites attached to scene without intention

I have three TextureRegions that are created like this:
mBackgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuTextureAtlas, pActivity, "menu_background.png", 0, 0);
mPlayBtnTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuTextureAtlas, pActivity, "play_btn.png", 50, 100);
mExitBtnTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuTextureAtlas, pActivity, "exit_btn.png", 50, 200);
I also have corresponding sprites for each TextureRegion which are created like this:
mBackgroundSprite = new Sprite(0, 0, mBackgroundTextureRegion, mBufferObjectManager);
mPlayBtnSprite = new Sprite(0, 0, mPlayBtnTextureRegion, mBufferObjectManager);
mExitBtnSprite = new Sprite(0, 0, mExitBtnTextureRegion, mBufferObjectManager);
When I attach mBackgroundSprite to a scene like this:
mScene.attachChild(mBackgroundSprite);
I would expect only mBackgroundTextureRegion to appear on the scene (i.e. "menu_background.png") but in reality all three TextureRegions appear (i.e. mPlayBtnTextureRegion and mExitBtnTextureRegion appear too in their location as well, (50,100) and (50,200) respectively).
My questions are:
A. Why does this happen? (does it have something to do with using the same Texture for all TextureRegions?)
B. What is the correct way to make only one TextureRegion appear on a scene?
No this has nothing to do with the texture atlas. This is the correct way of doing it more or less. Nothing should be shown on the screen unless they are attached as a child to something (scene or engine). If you need more straightforward help please upload the part of your code that you are loading the sprite.

How to create a layout like in picture in android?

I follow many blogs but didn't get the proper answer. Main problem is to create the circular progress which is showing with the help of some graphics. However not able to move towards success.
Path p = new Path();
p.moveTo(70, 10);
p.lineTo(25, 100);
p.lineTo(100, 50);
p.lineTo(0, 50);
p.lineTo(75, 100);
p.lineTo(50, 0);
// chk
// //complete code
ShapeDrawable progress1 = new ShapeDrawable(new ArcShape(180, -45));
progress1.setIntrinsicHeight(50);
progress1.setIntrinsicWidth(50);
progress1.getPaint().setColor(Color.BLUE);
progress1.getPaint().setStyle(Style.STROKE);
progress1.getPaint().setStrokeWidth(5);
ShapeDrawable progress2 = new ShapeDrawable(new ArcShape(0, 180));
progress2.setIntrinsicHeight(50);
progress2.setIntrinsicWidth(50);
progress2.getPaint().setARGB(50, 200, 54, 54);
progress2.getPaint().setStyle(Style.STROKE);
progress2.getPaint().setStrokeWidth(5);
iView.setImageDrawable(progress2);
iView1.setImageDrawable(progress1);
Without knowing which part you're stuck on, what you'd probably do is make a custom view where you define the drawing code in its onDraw method.
You can probably get away with drawing arced lines using a thick stroke in the corresponding target color. To make the ends of the arc rounded, configure the Paint object's properties to suit your needs, iirc the right method to use is Paint#setStrokeCap

How to use two BitmapTextureAtlases in AndEngine?

I have to work with big textures in my project so I can't put all the textures into the one BitmapTextureAtlas. I tried to put them into two atlases:
textureAtlas = new BitmapTextureAtlas(2048, 2048, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
textureAtlas2 = new BitmapTextureAtlas(1024, 2048, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
foo = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textureAtlas ,this,"foo.png",0,0);
bar = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textureAtlas2,this,"bar.png",0,0);
But when I try to use bar
Sprite sBar = new Sprite(0,0,bar);
scene.attachChild(sBar);
the only thing I can see is the white rectangle instead of my image. And I have no idea what is wrong here.
OK, I got it. I just forgot about this:
getEngine().getTextureManager().loadTexture(textureAtlas2);

Categories

Resources