배경이 검은 색상이고 작성자까지 옮기기 어렵다보니 핵심 내용만 대충 옮겨 놓으니
세부 내용은 원문을 참고하세요.
출처 : http://www.andengine.org/forums/development/loading-texture-from-internet-source-t2431.html
Loading texture from internet source?
In the game that I am creating, the client wants to be able to sell ad space inside the game. This isn't the typical ad bar that shows up on some games (angry birds) but instead a single image that gets replaced with the logo of whatever company inside the game itself. is this possible? does andEngine have any way of downloading images from a url and using them as a texture?
Thanks in advance for any help
Re: Loading texture from internet source?
You could just download the image and then turn it into a texture.
I figured this out.
First you need a texture source. So make a new class that implements ITextureSource:
- public class BitmapTextureSource implements ITextureSource {
- private int mHeight;
- private int mWidth;
- private Bitmap mBitmap;
- public BitmapTextureSource()
- {
- mBitmap = ImageFactory.getRandomImage();
- if(mBitmap!= null)
- {
- mHeight = mBitmap.getHeight();
- mWidth = mBitmap.getWidth();
- }
- else
- {
- mHeight = 0;
- mWidth = 0;
- }
- }
- @Override
- public BitmapTextureSource clone()
- {
- return new BitmapTextureSource();
- }
- @Override
- public int getHeight() {
- // TODO Auto-generated method stub
- return mHeight;
- }
- @Override
- public int getWidth() {
- // TODO Auto-generated method stub
- return mWidth;
- }
- @Override
- public Bitmap onLoadBitmap() {
- // TODO Auto-generated method stub
- return mBitmap;
- }
- }
The ImageFactory just returns an image from the internet:
- public class ImageFactory
- {
- public static Bitmap getRandomImage()
- {
- //take into account preferences
- Bitmap img = null;
- URL imgUrl = null;
- try {
- imgUrl= new URL("image url goes here");
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- try {
- HttpURLConnection conn= (HttpURLConnection)imgUrl.openConnection();
- conn.setDoInput(true);
- conn.connect();
- InputStream is = conn.getInputStream();
- img = BitmapFactory.decodeStream(is);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return img;
- }
- }
Now in the OnLoadScene method in your game:
- mBackground = new Texture(512, 1024, TextureOptions.BILINEAR);
- mEngine.getTextureManager().loadTexture(mBackground);
- mBackgroundReg = TextureRegionFactory.createFromSource(mBackground,
- new BitmapTextureSource(),
- 0,
- 0);
- mBackgroundSprite = new Sprite(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, mBackgroundReg);
- mScene.getLayer(0).addEntity(mBackgroundSprite);
As you can tell I use the picture from the internet for the background of my game. You can probably set up a small area where your ad shows up.
Have fun.
You need this permission in your manifest:
- <uses-permission android:name="android.permission.INTERNET"></uses-permission>
You also need to bear-in-mind that most devices have patchy or even non-existant connectivity much of the time they're in-use.
That means you need to be doing this stuff completely transparently/asynchronously and have a way of handling the (frequent) likelihood you'll get nothing back.
Last thing you want is your game pausing for huge periods of time whilst it tries to get something over the lamentable heap of shit which is most people's mobile data connections!!
Tasty Android Snacks - http://www.somewhatdog.com
'Android' 카테고리의 다른 글
[펌][LIB]Chat bubbles in Android - 체팅창의 말풍선 구현 방법 (0) | 2014.11.23 |
---|---|
[펌][Android][AndEngine] AndEngine Guides (0) | 2014.11.23 |
[펌][Android][AndEngine] perle-development의 AndEngine Tutorial (0) | 2014.11.23 |
[Android] 안드로이드 4.3버전의 개발자 옵션 활성화 하기 (0) | 2014.11.23 |
[버섯] Google Play Game Services (구글 플레이 게임 서비스) (0) | 2014.11.23 |